Rename mode constants
This commit is contained in:
+4
-4
@@ -22,7 +22,7 @@ type LRUCache struct {
|
|||||||
psNamePrefix string
|
psNamePrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLRUCache creates a new LRUCache. mode is either PrepareMode or DescribeMode. cap is the maximum size of the cache.
|
// 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 {
|
func NewLRUCache(conn *pgconn.PgConn, mode int, cap int) *LRUCache {
|
||||||
mustBeValidMode(mode)
|
mustBeValidMode(mode)
|
||||||
mustBeValidCap(cap)
|
mustBeValidCap(cap)
|
||||||
@@ -86,14 +86,14 @@ func (c *LRUCache) Cap() int {
|
|||||||
return c.cap
|
return c.cap
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mode returns the mode of the cache (PrepareMode or DescribeMode)
|
// Mode returns the mode of the cache (ModePrepare or ModeDescribe)
|
||||||
func (c *LRUCache) Mode() int {
|
func (c *LRUCache) Mode() int {
|
||||||
return c.mode
|
return c.mode
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) {
|
func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) {
|
||||||
var name string
|
var name string
|
||||||
if c.mode == PrepareMode {
|
if c.mode == ModePrepare {
|
||||||
name = fmt.Sprintf("%s_%d", c.psNamePrefix, c.prepareCount)
|
name = fmt.Sprintf("%s_%d", c.psNamePrefix, c.prepareCount)
|
||||||
c.prepareCount += 1
|
c.prepareCount += 1
|
||||||
}
|
}
|
||||||
@@ -104,7 +104,7 @@ func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedSta
|
|||||||
func (c *LRUCache) removeOldest(ctx context.Context) error {
|
func (c *LRUCache) removeOldest(ctx context.Context) error {
|
||||||
oldest := c.l.Back()
|
oldest := c.l.Back()
|
||||||
c.l.Remove(oldest)
|
c.l.Remove(oldest)
|
||||||
if c.mode == PrepareMode {
|
if c.mode == ModePrepare {
|
||||||
return c.conn.Exec(ctx, fmt.Sprintf("deallocate %s", oldest.Value.(*pgconn.PreparedStatementDescription).Name)).Close()
|
return c.conn.Exec(ctx, fmt.Sprintf("deallocate %s", oldest.Value.(*pgconn.PreparedStatementDescription).Name)).Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLRUCachePrepareMode(t *testing.T) {
|
func TestLRUCacheModePrepare(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,10 +22,10 @@ func TestLRUCachePrepareMode(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close(ctx)
|
defer conn.Close(ctx)
|
||||||
|
|
||||||
cache := pscache.NewLRUCache(conn, pscache.PrepareMode, 2)
|
cache := pscache.NewLRUCache(conn, pscache.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, pscache.PrepareMode, cache.Mode())
|
require.EqualValues(t, pscache.ModePrepare, cache.Mode())
|
||||||
|
|
||||||
psd, err := cache.Get(ctx, "select 1")
|
psd, err := cache.Get(ctx, "select 1")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -57,7 +57,7 @@ func TestLRUCachePrepareMode(t *testing.T) {
|
|||||||
require.Empty(t, fetchServerStatements(t, ctx, conn))
|
require.Empty(t, fetchServerStatements(t, ctx, conn))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLRUCacheDescribeMode(t *testing.T) {
|
func TestLRUCacheModeDescribe(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,10 +67,10 @@ func TestLRUCacheDescribeMode(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer conn.Close(ctx)
|
defer conn.Close(ctx)
|
||||||
|
|
||||||
cache := pscache.NewLRUCache(conn, pscache.DescribeMode, 2)
|
cache := pscache.NewLRUCache(conn, pscache.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, pscache.DescribeMode, cache.Mode())
|
require.EqualValues(t, pscache.ModeDescribe, cache.Mode())
|
||||||
|
|
||||||
psd, err := cache.Get(ctx, "select 1")
|
psd, err := cache.Get(ctx, "select 1")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
+6
-6
@@ -8,8 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PrepareMode = iota // Cache should prepare named statements.
|
ModePrepare = iota // Cache should prepare named statements.
|
||||||
DescribeMode // Cache should prepare the anonymous prepared statement to only fetch the description of the statement.
|
ModeDescribe // Cache should prepare the anonymous prepared statement to only fetch the description of the statement.
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cache prepares and caches prepared statement descriptions.
|
// Cache prepares and caches prepared statement descriptions.
|
||||||
@@ -26,11 +26,11 @@ type Cache interface {
|
|||||||
// Cap returns the maximum number of cached prepared statement descriptions.
|
// Cap returns the maximum number of cached prepared statement descriptions.
|
||||||
Cap() int
|
Cap() int
|
||||||
|
|
||||||
// Mode returns the mode of the cache (PrepareMode or DescribeMode)
|
// Mode returns the mode of the cache (ModePrepare or ModeDescribe)
|
||||||
Mode() int
|
Mode() int
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns the preferred cache implementation for mode and cap. mode is either PrepareMode or DescribeMode. cap is
|
// New returns the preferred cache implementation for mode and cap. mode is either ModePrepare or ModeDescribe. cap is
|
||||||
// the maximum size of the cache.
|
// the maximum size of the cache.
|
||||||
func New(conn *pgconn.PgConn, mode int, cap int) Cache {
|
func New(conn *pgconn.PgConn, mode int, cap int) Cache {
|
||||||
mustBeValidMode(mode)
|
mustBeValidMode(mode)
|
||||||
@@ -40,8 +40,8 @@ func New(conn *pgconn.PgConn, mode int, cap int) Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mustBeValidMode(mode int) {
|
func mustBeValidMode(mode int) {
|
||||||
if mode != PrepareMode && mode != DescribeMode {
|
if mode != ModePrepare && mode != ModeDescribe {
|
||||||
panic("mode must be PrepareMode or DescribeMode")
|
panic("mode must be ModePrepare or ModeDescribe")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user