diff --git a/pkg/cache/bigcache.go b/pkg/cache/bigcache.go deleted file mode 100644 index 1287595..0000000 --- a/pkg/cache/bigcache.go +++ /dev/null @@ -1,65 +0,0 @@ -package cache - -import ( - "context" - "github.com/allegro/bigcache/v3" - "github.com/chenyahui/gin-cache/persist" - "go.uber.org/zap" - "time" -) - -// BigCacheStore local memory cache store -type BigCacheStore struct { - Cache *bigcache.BigCache -} - -// NewBigCacheStore allocate a local memory store with default expiration -func NewBigCacheStore(defaultExpiration time.Duration, logger *zap.Logger) *BigCacheStore { - config := bigcache.Config{ - Shards: 1024, - LifeWindow: defaultExpiration, - CleanWindow: 3 * time.Second, - MaxEntriesInWindow: 10000, - MaxEntrySize: 1500, - StatsEnabled: false, - Verbose: true, - HardMaxCacheSize: 256, // memory limit, value in MB - Logger: NewZapLogger(logger), - } - cache, err := bigcache.New(context.Background(), config) - if err != nil { - panic(err) - } - return &BigCacheStore{ - Cache: cache, - } -} - -// Set put key value pair to memory store, and expire after expireDuration -func (c *BigCacheStore) Set(key string, value interface{}, expireDuration time.Duration) error { - _ = expireDuration - payload, err := persist.Serialize(value) - if err != nil { - return err - } - return c.Cache.Set(key, payload) -} - -// Delete remove key in memory store, do nothing if key doesn't exist -func (c *BigCacheStore) Delete(key string) error { - return c.Cache.Delete(key) -} - -// Get get key in memory store, if key doesn't exist, return ErrCacheMiss -func (c *BigCacheStore) Get(key string, value interface{}) error { - payload, err := c.Cache.Get(key) - - if err == bigcache.ErrEntryNotFound { - return persist.ErrCacheMiss - } - - if err != nil { - return err - } - return persist.Deserialize(payload, value) -} diff --git a/pkg/cache/logger.go b/pkg/cache/logger.go deleted file mode 100644 index ebf53c6..0000000 --- a/pkg/cache/logger.go +++ /dev/null @@ -1,18 +0,0 @@ -package cache - -import ( - "fmt" - "go.uber.org/zap" -) - -type ZapLogger struct { - Zap *zap.Logger -} - -func NewZapLogger(zap *zap.Logger) *ZapLogger { - return &ZapLogger{zap} -} - -func (c *ZapLogger) Printf(format string, v ...interface{}) { - c.Zap.Log(2, fmt.Sprintf(format, v...)) -}