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

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

@ -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 缓存操作

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

@ -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 缓存操作

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

Loading…
Cancel
Save