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

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

@ -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 插入数据 将只显示给定结构的导出字段 序列化并存储

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

@ -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))
}

Loading…
Cancel
Save