From 78abbdf1d7eef6b2aa78831c31141057876537f6 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 24 Aug 2019 19:48:43 -0500 Subject: [PATCH] Rename LRUCache to LRU --- stmtcache/{lrucache.go => lru.go} | 28 ++++++++++----------- stmtcache/{lrucache_test.go => lru_test.go} | 8 +++--- stmtcache/stmtcache.go | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) rename stmtcache/{lrucache.go => lru.go} (70%) rename stmtcache/{lrucache_test.go => lru_test.go} (93%) diff --git a/stmtcache/lrucache.go b/stmtcache/lru.go similarity index 70% rename from stmtcache/lrucache.go rename to stmtcache/lru.go index 9c4d046d..432a70b4 100644 --- a/stmtcache/lrucache.go +++ b/stmtcache/lru.go @@ -9,10 +9,10 @@ import ( "github.com/jackc/pgconn" ) -var lruCacheCount uint64 +var lruCount uint64 -// LRUCache implements cache with a Least Recently Used (LRU) cache. -type LRUCache struct { +// LRU implements Cache with a Least Recently Used (LRU) cache. +type LRU struct { conn *pgconn.PgConn mode int cap int @@ -22,14 +22,14 @@ type LRUCache struct { psNamePrefix string } -// NewLRUCache creates a new LRUCache. mode is either ModePrepare or ModeDescribe. cap is the maximum size of the cache. -func NewLRUCache(conn *pgconn.PgConn, mode int, cap int) *LRUCache { +// NewLRU creates a new LRU. mode is either ModePrepare or ModeDescribe. cap is the maximum size of the cache. +func NewLRU(conn *pgconn.PgConn, mode int, cap int) *LRU { mustBeValidMode(mode) mustBeValidCap(cap) - n := atomic.AddUint64(&lruCacheCount, 1) + n := atomic.AddUint64(&lruCount, 1) - return &LRUCache{ + return &LRU{ conn: conn, mode: mode, 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. -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 { c.l.MoveToFront(el) 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. -func (c *LRUCache) Clear(ctx context.Context) error { +func (c *LRU) Clear(ctx context.Context) error { for c.l.Len() > 0 { err := c.removeOldest(ctx) if err != nil { @@ -77,21 +77,21 @@ func (c *LRUCache) Clear(ctx context.Context) error { } // Len returns the number of cached prepared statement descriptions. -func (c *LRUCache) Len() int { +func (c *LRU) Len() int { return c.l.Len() } // Cap returns the maximum number of cached prepared statement descriptions. -func (c *LRUCache) Cap() int { +func (c *LRU) Cap() int { return c.cap } // Mode returns the mode of the cache (ModePrepare or ModeDescribe) -func (c *LRUCache) Mode() int { +func (c *LRU) Mode() int { 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 if c.mode == ModePrepare { 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) } -func (c *LRUCache) removeOldest(ctx context.Context) error { +func (c *LRU) removeOldest(ctx context.Context) error { oldest := c.l.Back() c.l.Remove(oldest) if c.mode == ModePrepare { diff --git a/stmtcache/lrucache_test.go b/stmtcache/lru_test.go similarity index 93% rename from stmtcache/lrucache_test.go rename to stmtcache/lru_test.go index ed8ebdc3..b518364e 100644 --- a/stmtcache/lrucache_test.go +++ b/stmtcache/lru_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLRUCacheModePrepare(t *testing.T) { +func TestLRUModePrepare(t *testing.T) { t.Parallel() ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) @@ -22,7 +22,7 @@ func TestLRUCacheModePrepare(t *testing.T) { require.NoError(t, err) 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, 2, cache.Cap()) require.EqualValues(t, stmtcache.ModePrepare, cache.Mode()) @@ -57,7 +57,7 @@ func TestLRUCacheModePrepare(t *testing.T) { require.Empty(t, fetchServerStatements(t, ctx, conn)) } -func TestLRUCacheModeDescribe(t *testing.T) { +func TestLRUModeDescribe(t *testing.T) { t.Parallel() ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) @@ -67,7 +67,7 @@ func TestLRUCacheModeDescribe(t *testing.T) { require.NoError(t, err) 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, 2, cache.Cap()) require.EqualValues(t, stmtcache.ModeDescribe, cache.Mode()) diff --git a/stmtcache/stmtcache.go b/stmtcache/stmtcache.go index d70f277b..9bedf549 100644 --- a/stmtcache/stmtcache.go +++ b/stmtcache/stmtcache.go @@ -36,7 +36,7 @@ func New(conn *pgconn.PgConn, mode int, cap int) Cache { mustBeValidMode(mode) mustBeValidCap(cap) - return NewLRUCache(conn, mode, cap) + return NewLRU(conn, mode, cap) } func mustBeValidMode(mode int) {