You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gocache/go.go

41 lines
932 B

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