From 109543b4069e5be8affeea4c3f4c9f91ba9a7af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Wed, 18 May 2022 23:53:06 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- big.go | 16 ++++++++-------- big_cache.go | 4 ++-- go.go | 32 ++++++++++++++++++++------------ go_cache.go | 14 +++++++++++--- go_test.go | 11 +++++++++-- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/big.go b/big.go index 148bb24..336e581 100644 --- a/big.go +++ b/big.go @@ -20,14 +20,14 @@ type Big struct { // NewBig 实例化 func NewBig(config *BigConfig) *Big { - b := &Big{} - b.DefaultExpiration = config.DefaultExpiration - b.db, _ = bigcache.NewBigCache(bigcache.DefaultConfig(b.DefaultExpiration)) - return b + app := &Big{} + app.DefaultExpiration = config.DefaultExpiration + app.db, _ = bigcache.NewBigCache(bigcache.DefaultConfig(app.DefaultExpiration)) + return app } // Set 插入数据 将只显示给定结构的导出字段 序列化并存储 -func (b *Big) Set(key string, value interface{}) error { +func (c *Big) Set(key string, value interface{}) error { // 将 value 序列化为 bytes valueBytes, err := serialize(value) @@ -35,14 +35,14 @@ func (b *Big) Set(key string, value interface{}) error { return err } - return b.db.Set(key, valueBytes) + return c.db.Set(key, valueBytes) } // Get 获取单个数据 -func (b *Big) Get(key string) (interface{}, error) { +func (c *Big) Get(key string) (interface{}, error) { // 获取以 bytes 格式存储的 value - valueBytes, err := b.db.Get(key) + valueBytes, err := c.db.Get(key) if err != nil { return nil, err } diff --git a/big_cache.go b/big_cache.go index b8f1765..9b4a02e 100644 --- a/big_cache.go +++ b/big_cache.go @@ -11,8 +11,8 @@ type BigCache struct { } // NewCache 实例化组件 -func (b *Big) NewCache() *BigCache { - return &BigCache{db: b} +func (c *Big) NewCache() *BigCache { + return &BigCache{db: c} } // GetInterface 缓存操作 diff --git a/go.go b/go.go index 52b07b1..5b03afd 100644 --- a/go.go +++ b/go.go @@ -5,30 +5,38 @@ import ( "time" ) +// GoConfig 配置 +type GoConfig struct { + DefaultExpiration time.Duration // 默认过期时间 + DefaultClear time.Duration // 清理过期数据 +} + // Go https://github.com/patrickmn/go-cache type Go struct { - db *cache.Cache // 驱动 - expiration time.Duration // 默认过期时间 - clear time.Duration // 清理过期数据 + GoConfig + db *cache.Cache // 驱动 } // NewGo 实例化 -func NewGo(expiration, clear time.Duration) *Go { - c := cache.New(expiration, clear) - return &Go{db: c, expiration: expiration, clear: clear} +func NewGo(config *GoConfig) *Go { + app := &Go{} + app.DefaultExpiration = config.DefaultExpiration + app.DefaultClear = config.DefaultClear + app.db = cache.New(app.DefaultExpiration, app.DefaultClear) + return app } // Set 插入数据 并设置过期时间 -func (g *Go) Set(key string, value interface{}, expirationTime time.Duration) { - g.db.Set(key, value, expirationTime) +func (c *Go) Set(key string, value interface{}, expirationTime time.Duration) { + c.db.Set(key, value, expirationTime) } // Get 获取单个数据 -func (g *Go) Get(key string) (interface{}, bool) { - return g.db.Get(key) +func (c *Go) Get(key string) (interface{}, bool) { + return c.db.Get(key) } // SetDefault 插入数据 并设置为默认过期时间 -func (g *Go) SetDefault(key string, value interface{}) { - g.db.Set(key, value, g.expiration) +func (c *Go) SetDefault(key string, value interface{}) { + c.db.Set(key, value, c.DefaultExpiration) } diff --git a/go_cache.go b/go_cache.go index 2372f8a..a4090c0 100644 --- a/go_cache.go +++ b/go_cache.go @@ -4,16 +4,24 @@ import ( "time" ) +// GoCacheConfig 配置 +type GoCacheConfig struct { + expiration time.Duration // 过期时间 +} + // GoCache https://github.com/patrickmn/go-cache type GoCache struct { + GoCacheConfig db *Go // 驱动 - expiration time.Duration // 默认过期时间 GetterInterface GttInterfaceFunc // 不存在的操作 } // NewCache 实例化 -func (g *Go) NewCache(expiration time.Duration) *GoCache { - return &GoCache{db: g, expiration: expiration} +func (c *Go) NewCache(config *GoCacheConfig) *GoCache { + app := &GoCache{} + app.expiration = config.expiration + app.db = c + return app } // GetInterface 缓存操作 diff --git a/go_test.go b/go_test.go index 55609c9..e7c3530 100644 --- a/go_test.go +++ b/go_test.go @@ -5,9 +5,16 @@ import ( "time" ) +func logGo() *Go { + return NewGo(&GoConfig{ + DefaultExpiration: time.Minute * 5, + DefaultClear: time.Minute * 10, + }) +} + func TestGo(t *testing.T) { - newCache := NewGo(5*time.Minute, 10*time.Minute) + newCache := logGo() // 字符串 newCache.SetDefault("key1", "测试Go插入数据 1") @@ -24,7 +31,7 @@ func TestGo(t *testing.T) { t.Logf("key2:%+v", key2.(name)) // 缓存组件 - newCacheCache := newCache.NewCache(5 * time.Minute) + newCacheCache := newCache.NewCache(&GoCacheConfig{expiration: 5 * time.Minute}) newCacheCache.GetterInterface = func() interface{} { return name{"测试Go插入数据 3"} }