Rename mode constants
This commit is contained in:
+4
-4
@@ -22,7 +22,7 @@ type LRUCache struct {
|
||||
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 {
|
||||
mustBeValidMode(mode)
|
||||
mustBeValidCap(cap)
|
||||
@@ -86,14 +86,14 @@ func (c *LRUCache) Cap() int {
|
||||
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 {
|
||||
return c.mode
|
||||
}
|
||||
|
||||
func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) {
|
||||
var name string
|
||||
if c.mode == PrepareMode {
|
||||
if c.mode == ModePrepare {
|
||||
name = fmt.Sprintf("%s_%d", c.psNamePrefix, c.prepareCount)
|
||||
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 {
|
||||
oldest := c.l.Back()
|
||||
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 nil
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLRUCachePrepareMode(t *testing.T) {
|
||||
func TestLRUCacheModePrepare(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
@@ -22,10 +22,10 @@ func TestLRUCachePrepareMode(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
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, 2, cache.Cap())
|
||||
require.EqualValues(t, pscache.PrepareMode, cache.Mode())
|
||||
require.EqualValues(t, pscache.ModePrepare, cache.Mode())
|
||||
|
||||
psd, err := cache.Get(ctx, "select 1")
|
||||
require.NoError(t, err)
|
||||
@@ -57,7 +57,7 @@ func TestLRUCachePrepareMode(t *testing.T) {
|
||||
require.Empty(t, fetchServerStatements(t, ctx, conn))
|
||||
}
|
||||
|
||||
func TestLRUCacheDescribeMode(t *testing.T) {
|
||||
func TestLRUCacheModeDescribe(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
@@ -67,10 +67,10 @@ func TestLRUCacheDescribeMode(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
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, 2, cache.Cap())
|
||||
require.EqualValues(t, pscache.DescribeMode, cache.Mode())
|
||||
require.EqualValues(t, pscache.ModeDescribe, cache.Mode())
|
||||
|
||||
psd, err := cache.Get(ctx, "select 1")
|
||||
require.NoError(t, err)
|
||||
|
||||
+6
-6
@@ -8,8 +8,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
PrepareMode = iota // Cache should prepare named statements.
|
||||
DescribeMode // Cache should prepare the anonymous prepared statement to only fetch the description of the statement.
|
||||
ModePrepare = iota // Cache should prepare named statements.
|
||||
ModeDescribe // Cache should prepare the anonymous prepared statement to only fetch the description of the statement.
|
||||
)
|
||||
|
||||
// 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() int
|
||||
|
||||
// Mode returns the mode of the cache (PrepareMode or DescribeMode)
|
||||
// Mode returns the mode of the cache (ModePrepare or ModeDescribe)
|
||||
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.
|
||||
func New(conn *pgconn.PgConn, mode int, cap int) Cache {
|
||||
mustBeValidMode(mode)
|
||||
@@ -40,8 +40,8 @@ func New(conn *pgconn.PgConn, mode int, cap int) Cache {
|
||||
}
|
||||
|
||||
func mustBeValidMode(mode int) {
|
||||
if mode != PrepareMode && mode != DescribeMode {
|
||||
panic("mode must be PrepareMode or DescribeMode")
|
||||
if mode != ModePrepare && mode != ModeDescribe {
|
||||
panic("mode must be ModePrepare or ModeDescribe")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user