master
李光春 2 years ago
parent 8344f4fc6b
commit 109543b406

@ -20,14 +20,14 @@ type Big struct {
// NewBig 实例化 // NewBig 实例化
func NewBig(config *BigConfig) *Big { func NewBig(config *BigConfig) *Big {
b := &Big{} app := &Big{}
b.DefaultExpiration = config.DefaultExpiration app.DefaultExpiration = config.DefaultExpiration
b.db, _ = bigcache.NewBigCache(bigcache.DefaultConfig(b.DefaultExpiration)) app.db, _ = bigcache.NewBigCache(bigcache.DefaultConfig(app.DefaultExpiration))
return b return app
} }
// Set 插入数据 将只显示给定结构的导出字段 序列化并存储 // Set 插入数据 将只显示给定结构的导出字段 序列化并存储
func (b *Big) Set(key string, value interface{}) error { func (c *Big) Set(key string, value interface{}) error {
// 将 value 序列化为 bytes // 将 value 序列化为 bytes
valueBytes, err := serialize(value) valueBytes, err := serialize(value)
@ -35,14 +35,14 @@ func (b *Big) Set(key string, value interface{}) error {
return err return err
} }
return b.db.Set(key, valueBytes) return c.db.Set(key, valueBytes)
} }
// Get 获取单个数据 // Get 获取单个数据
func (b *Big) Get(key string) (interface{}, error) { func (c *Big) Get(key string) (interface{}, error) {
// 获取以 bytes 格式存储的 value // 获取以 bytes 格式存储的 value
valueBytes, err := b.db.Get(key) valueBytes, err := c.db.Get(key)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -11,8 +11,8 @@ type BigCache struct {
} }
// NewCache 实例化组件 // NewCache 实例化组件
func (b *Big) NewCache() *BigCache { func (c *Big) NewCache() *BigCache {
return &BigCache{db: b} return &BigCache{db: c}
} }
// GetInterface 缓存操作 // GetInterface 缓存操作

32
go.go

@ -5,30 +5,38 @@ import (
"time" "time"
) )
// GoConfig 配置
type GoConfig struct {
DefaultExpiration time.Duration // 默认过期时间
DefaultClear time.Duration // 清理过期数据
}
// Go https://github.com/patrickmn/go-cache // Go https://github.com/patrickmn/go-cache
type Go struct { type Go struct {
db *cache.Cache // 驱动 GoConfig
expiration time.Duration // 默认过期时间 db *cache.Cache // 驱动
clear time.Duration // 清理过期数据
} }
// NewGo 实例化 // NewGo 实例化
func NewGo(expiration, clear time.Duration) *Go { func NewGo(config *GoConfig) *Go {
c := cache.New(expiration, clear) app := &Go{}
return &Go{db: c, expiration: expiration, clear: clear} app.DefaultExpiration = config.DefaultExpiration
app.DefaultClear = config.DefaultClear
app.db = cache.New(app.DefaultExpiration, app.DefaultClear)
return app
} }
// Set 插入数据 并设置过期时间 // Set 插入数据 并设置过期时间
func (g *Go) Set(key string, value interface{}, expirationTime time.Duration) { func (c *Go) Set(key string, value interface{}, expirationTime time.Duration) {
g.db.Set(key, value, expirationTime) c.db.Set(key, value, expirationTime)
} }
// Get 获取单个数据 // Get 获取单个数据
func (g *Go) Get(key string) (interface{}, bool) { func (c *Go) Get(key string) (interface{}, bool) {
return g.db.Get(key) return c.db.Get(key)
} }
// SetDefault 插入数据 并设置为默认过期时间 // SetDefault 插入数据 并设置为默认过期时间
func (g *Go) SetDefault(key string, value interface{}) { func (c *Go) SetDefault(key string, value interface{}) {
g.db.Set(key, value, g.expiration) c.db.Set(key, value, c.DefaultExpiration)
} }

@ -4,16 +4,24 @@ import (
"time" "time"
) )
// GoCacheConfig 配置
type GoCacheConfig struct {
expiration time.Duration // 过期时间
}
// GoCache https://github.com/patrickmn/go-cache // GoCache https://github.com/patrickmn/go-cache
type GoCache struct { type GoCache struct {
GoCacheConfig
db *Go // 驱动 db *Go // 驱动
expiration time.Duration // 默认过期时间
GetterInterface GttInterfaceFunc // 不存在的操作 GetterInterface GttInterfaceFunc // 不存在的操作
} }
// NewCache 实例化 // NewCache 实例化
func (g *Go) NewCache(expiration time.Duration) *GoCache { func (c *Go) NewCache(config *GoCacheConfig) *GoCache {
return &GoCache{db: g, expiration: expiration} app := &GoCache{}
app.expiration = config.expiration
app.db = c
return app
} }
// GetInterface 缓存操作 // GetInterface 缓存操作

@ -5,9 +5,16 @@ import (
"time" "time"
) )
func logGo() *Go {
return NewGo(&GoConfig{
DefaultExpiration: time.Minute * 5,
DefaultClear: time.Minute * 10,
})
}
func TestGo(t *testing.T) { func TestGo(t *testing.T) {
newCache := NewGo(5*time.Minute, 10*time.Minute) newCache := logGo()
// 字符串 // 字符串
newCache.SetDefault("key1", "测试Go插入数据 1") newCache.SetDefault("key1", "测试Go插入数据 1")
@ -24,7 +31,7 @@ func TestGo(t *testing.T) {
t.Logf("key2%+v", key2.(name)) t.Logf("key2%+v", key2.(name))
// 缓存组件 // 缓存组件
newCacheCache := newCache.NewCache(5 * time.Minute) newCacheCache := newCache.NewCache(&GoCacheConfig{expiration: 5 * time.Minute})
newCacheCache.GetterInterface = func() interface{} { newCacheCache.GetterInterface = func() interface{} {
return name{"测试Go插入数据 3"} return name{"测试Go插入数据 3"}
} }

Loading…
Cancel
Save