diff --git a/big.go b/big.go index 7743cdc..148bb24 100644 --- a/big.go +++ b/big.go @@ -7,16 +7,23 @@ import ( "time" ) +// BigConfig 配置 +type BigConfig struct { + DefaultExpiration time.Duration // 默认过期时间 +} + // Big https://github.com/allegro/bigcache type Big struct { - db *bigcache.BigCache // 驱动 - expiration time.Duration // 默认过期时间 + BigConfig + db *bigcache.BigCache // 驱动 } // NewBig 实例化 -func NewBig(expiration time.Duration) *Big { - c, _ := bigcache.NewBigCache(bigcache.DefaultConfig(expiration)) - return &Big{db: c, expiration: expiration} +func NewBig(config *BigConfig) *Big { + b := &Big{} + b.DefaultExpiration = config.DefaultExpiration + b.db, _ = bigcache.NewBigCache(bigcache.DefaultConfig(b.DefaultExpiration)) + return b } // Set 插入数据 将只显示给定结构的导出字段 序列化并存储 diff --git a/big_cache.go b/big_cache.go index aca0609..b8f1765 100644 --- a/big_cache.go +++ b/big_cache.go @@ -10,7 +10,7 @@ type BigCache struct { GetterInterface GttInterfaceFunc // 不存在的操作 } -// NewCache 实例化 +// NewCache 实例化组件 func (b *Big) NewCache() *BigCache { return &BigCache{db: b} } diff --git a/big_test.go b/big_test.go index b621b35..bf92eae 100644 --- a/big_test.go +++ b/big_test.go @@ -5,31 +5,38 @@ import ( "time" ) +func logBig() *Big { + return NewBig(&BigConfig{ + DefaultExpiration: time.Minute * 30, + }) +} + func TestBig(t *testing.T) { - newCache := NewBig(time.Minute * 30) + newCache := logBig() - // 字符串 - newCache.Set("key1", "测试Big插入数据 1") + // 设置字符串 + t.Logf("设置字符串:%+v", newCache.Set("key1", "测试Big插入数据 1")) key1, _ := newCache.Get("key1") - t.Logf("key1:%+v", key1) + t.Logf("读取字符串:%+v", key1) - // 结构体 + // 设置结构体 type name struct { Test string `json:"test"` } - newCache.Set("key2", name{"测试Big插入数据 2"}) + t.Logf("设置结构体:%+v", newCache.Set("key2", name{"测试Big插入数据 2"})) key2, _ := newCache.Get("key2") - t.Logf("key2:%+v", key2) - t.Logf("key2:%+v", key2.(name)) + t.Logf("读取结构体:%+v", key2) + t.Logf("读取结构体结果:%+v", key2.(name)) // 缓存组件 newCacheCache := newCache.NewCache() newCacheCache.GetterInterface = func() interface{} { return name{"测试Big插入数据 3"} } + //key3Result := &name{} key3 := newCacheCache.GetInterface("key3") - t.Logf("key3:%+v", key3) - t.Logf("key3:%+v", key3.(name)) + t.Logf("读取缓存组件:%T", key3) + t.Logf("读取缓存组件:%+v", key3.(name)) }