update big
continuous-integration/drone/push Build is passing Details

master
李光春 2 years ago
parent 6e11644d0a
commit 8344f4fc6b

@ -7,16 +7,23 @@ import (
"time" "time"
) )
// BigConfig 配置
type BigConfig struct {
DefaultExpiration time.Duration // 默认过期时间
}
// Big https://github.com/allegro/bigcache // Big https://github.com/allegro/bigcache
type Big struct { type Big struct {
db *bigcache.BigCache // 驱动 BigConfig
expiration time.Duration // 默认过期时间 db *bigcache.BigCache // 驱动
} }
// NewBig 实例化 // NewBig 实例化
func NewBig(expiration time.Duration) *Big { func NewBig(config *BigConfig) *Big {
c, _ := bigcache.NewBigCache(bigcache.DefaultConfig(expiration)) b := &Big{}
return &Big{db: c, expiration: expiration} b.DefaultExpiration = config.DefaultExpiration
b.db, _ = bigcache.NewBigCache(bigcache.DefaultConfig(b.DefaultExpiration))
return b
} }
// Set 插入数据 将只显示给定结构的导出字段 序列化并存储 // Set 插入数据 将只显示给定结构的导出字段 序列化并存储

@ -10,7 +10,7 @@ type BigCache struct {
GetterInterface GttInterfaceFunc // 不存在的操作 GetterInterface GttInterfaceFunc // 不存在的操作
} }
// NewCache 实例化 // NewCache 实例化组件
func (b *Big) NewCache() *BigCache { func (b *Big) NewCache() *BigCache {
return &BigCache{db: b} return &BigCache{db: b}
} }

@ -5,31 +5,38 @@ import (
"time" "time"
) )
func logBig() *Big {
return NewBig(&BigConfig{
DefaultExpiration: time.Minute * 30,
})
}
func TestBig(t *testing.T) { 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") key1, _ := newCache.Get("key1")
t.Logf("key1%+v", key1) t.Logf("读取字符串%+v", key1)
// 结构体 // 设置结构体
type name struct { type name struct {
Test string `json:"test"` Test string `json:"test"`
} }
newCache.Set("key2", name{"测试Big插入数据 2"}) t.Logf("设置结构体:%+v", newCache.Set("key2", name{"测试Big插入数据 2"}))
key2, _ := newCache.Get("key2") key2, _ := newCache.Get("key2")
t.Logf("key2%+v", key2) t.Logf("读取结构体%+v", key2)
t.Logf("key2%+v", key2.(name)) t.Logf("读取结构体结果%+v", key2.(name))
// 缓存组件 // 缓存组件
newCacheCache := newCache.NewCache() newCacheCache := newCache.NewCache()
newCacheCache.GetterInterface = func() interface{} { newCacheCache.GetterInterface = func() interface{} {
return name{"测试Big插入数据 3"} return name{"测试Big插入数据 3"}
} }
//key3Result := &name{}
key3 := newCacheCache.GetInterface("key3") key3 := newCacheCache.GetInterface("key3")
t.Logf("key3%+v", key3) t.Logf("读取缓存组件:%T", key3)
t.Logf("key3%+v", key3.(name)) t.Logf("读取缓存组件%+v", key3.(name))
} }

Loading…
Cancel
Save