Rename LRUCache to LRU
This commit is contained in:
@@ -9,10 +9,10 @@ import (
|
|||||||
"github.com/jackc/pgconn"
|
"github.com/jackc/pgconn"
|
||||||
)
|
)
|
||||||
|
|
||||||
var lruCacheCount uint64
|
var lruCount uint64
|
||||||
|
|
||||||
// LRUCache implements cache with a Least Recently Used (LRU) cache.
|
// LRU implements Cache with a Least Recently Used (LRU) cache.
|
||||||
type LRUCache struct {
|
type LRU struct {
|
||||||
conn *pgconn.PgConn
|
conn *pgconn.PgConn
|
||||||
mode int
|
mode int
|
||||||
cap int
|
cap int
|
||||||
@@ -22,14 +22,14 @@ type LRUCache struct {
|
|||||||
psNamePrefix string
|
psNamePrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLRUCache creates a new LRUCache. mode is either ModePrepare or ModeDescribe. cap is the maximum size of the cache.
|
// NewLRU creates a new LRU. mode is either ModePrepare or ModeDescribe. cap is the maximum size of the cache.
|
||||||
func NewLRUCache(conn *pgconn.PgConn, mode int, cap int) *LRUCache {
|
func NewLRU(conn *pgconn.PgConn, mode int, cap int) *LRU {
|
||||||
mustBeValidMode(mode)
|
mustBeValidMode(mode)
|
||||||
mustBeValidCap(cap)
|
mustBeValidCap(cap)
|
||||||
|
|
||||||
n := atomic.AddUint64(&lruCacheCount, 1)
|
n := atomic.AddUint64(&lruCount, 1)
|
||||||
|
|
||||||
return &LRUCache{
|
return &LRU{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
cap: cap,
|
cap: cap,
|
||||||
@@ -40,7 +40,7 @@ func NewLRUCache(conn *pgconn.PgConn, mode int, cap int) *LRUCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the prepared statement description for sql preparing or describing the sql on the server as needed.
|
// Get returns the prepared statement description for sql preparing or describing the sql on the server as needed.
|
||||||
func (c *LRUCache) Get(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) {
|
func (c *LRU) Get(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) {
|
||||||
if el, ok := c.m[sql]; ok {
|
if el, ok := c.m[sql]; ok {
|
||||||
c.l.MoveToFront(el)
|
c.l.MoveToFront(el)
|
||||||
return el.Value.(*pgconn.PreparedStatementDescription), nil
|
return el.Value.(*pgconn.PreparedStatementDescription), nil
|
||||||
@@ -65,7 +65,7 @@ func (c *LRUCache) Get(ctx context.Context, sql string) (*pgconn.PreparedStateme
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clear removes all entries in the cache. Any prepared statements will be deallocated from the PostgreSQL session.
|
// Clear removes all entries in the cache. Any prepared statements will be deallocated from the PostgreSQL session.
|
||||||
func (c *LRUCache) Clear(ctx context.Context) error {
|
func (c *LRU) Clear(ctx context.Context) error {
|
||||||
for c.l.Len() > 0 {
|
for c.l.Len() > 0 {
|
||||||
err := c.removeOldest(ctx)
|
err := c.removeOldest(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -77,21 +77,21 @@ func (c *LRUCache) Clear(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Len returns the number of cached prepared statement descriptions.
|
// Len returns the number of cached prepared statement descriptions.
|
||||||
func (c *LRUCache) Len() int {
|
func (c *LRU) Len() int {
|
||||||
return c.l.Len()
|
return c.l.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cap returns the maximum number of cached prepared statement descriptions.
|
// Cap returns the maximum number of cached prepared statement descriptions.
|
||||||
func (c *LRUCache) Cap() int {
|
func (c *LRU) Cap() int {
|
||||||
return c.cap
|
return c.cap
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mode returns the mode of the cache (ModePrepare or ModeDescribe)
|
// Mode returns the mode of the cache (ModePrepare or ModeDescribe)
|
||||||
func (c *LRUCache) Mode() int {
|
func (c *LRU) Mode() int {
|
||||||
return c.mode
|
return c.mode
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) {
|
func (c *LRU) prepare(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) {
|
||||||
var name string
|
var name string
|
||||||
if c.mode == ModePrepare {
|
if c.mode == ModePrepare {
|
||||||
name = fmt.Sprintf("%s_%d", c.psNamePrefix, c.prepareCount)
|
name = fmt.Sprintf("%s_%d", c.psNamePrefix, c.prepareCount)
|
||||||
@@ -101,7 +101,7 @@ func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedSta
|
|||||||
return c.conn.Prepare(ctx, name, sql, nil)
|
return c.conn.Prepare(ctx, name, sql, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LRUCache) removeOldest(ctx context.Context) error {
|
func (c *LRU) removeOldest(ctx context.Context) error {
|
||||||
oldest := c.l.Back()
|
oldest := c.l.Back()
|
||||||
c.l.Remove(oldest)
|
c.l.Remove(oldest)
|
||||||
if c.mode == ModePrepare {
|
if c.mode == ModePrepare {
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLRUCacheModePrepare(t *testing.T) {
|
func TestLRUModePrepare(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
@@ -22,7 +22,7 @@ func TestLRUCacheModePrepare(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close(ctx)
|
defer conn.Close(ctx)
|
||||||
|
|
||||||
cache := stmtcache.NewLRUCache(conn, stmtcache.ModePrepare, 2)
|
cache := stmtcache.NewLRU(conn, stmtcache.ModePrepare, 2)
|
||||||
require.EqualValues(t, 0, cache.Len())
|
require.EqualValues(t, 0, cache.Len())
|
||||||
require.EqualValues(t, 2, cache.Cap())
|
require.EqualValues(t, 2, cache.Cap())
|
||||||
require.EqualValues(t, stmtcache.ModePrepare, cache.Mode())
|
require.EqualValues(t, stmtcache.ModePrepare, cache.Mode())
|
||||||
@@ -57,7 +57,7 @@ func TestLRUCacheModePrepare(t *testing.T) {
|
|||||||
require.Empty(t, fetchServerStatements(t, ctx, conn))
|
require.Empty(t, fetchServerStatements(t, ctx, conn))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLRUCacheModeDescribe(t *testing.T) {
|
func TestLRUModeDescribe(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
@@ -67,7 +67,7 @@ func TestLRUCacheModeDescribe(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close(ctx)
|
defer conn.Close(ctx)
|
||||||
|
|
||||||
cache := stmtcache.NewLRUCache(conn, stmtcache.ModeDescribe, 2)
|
cache := stmtcache.NewLRU(conn, stmtcache.ModeDescribe, 2)
|
||||||
require.EqualValues(t, 0, cache.Len())
|
require.EqualValues(t, 0, cache.Len())
|
||||||
require.EqualValues(t, 2, cache.Cap())
|
require.EqualValues(t, 2, cache.Cap())
|
||||||
require.EqualValues(t, stmtcache.ModeDescribe, cache.Mode())
|
require.EqualValues(t, stmtcache.ModeDescribe, cache.Mode())
|
||||||
@@ -36,7 +36,7 @@ func New(conn *pgconn.PgConn, mode int, cap int) Cache {
|
|||||||
mustBeValidMode(mode)
|
mustBeValidMode(mode)
|
||||||
mustBeValidCap(cap)
|
mustBeValidCap(cap)
|
||||||
|
|
||||||
return NewLRUCache(conn, mode, cap)
|
return NewLRU(conn, mode, cap)
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustBeValidMode(mode int) {
|
func mustBeValidMode(mode int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user