代码拉取完成,页面将自动刷新
package cache
import (
"errors"
"strings"
"time"
"github.com/bradfitz/gomemcache/memcache"
)
type MemCache struct {
conn *memcache.Client
conninfo []string
}
type MemOpts struct {
Conn string
}
func NewMemCache() Cache {
return &MemCache{}
}
func (rc *MemCache) Get(key string) interface{} {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
if item, err := rc.conn.Get(key); err == nil {
return item.Value
}
return nil
}
func (rc *MemCache) Set(key string, val interface{}, timeout time.Duration) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
item := memcache.Item{Key: key, Expiration: int32(timeout / time.Second)}
if v, ok := val.([]byte); ok {
item.Value = v
} else if str, ok := val.(string); ok {
item.Value = []byte(str)
} else {
return errors.New("val only support string and []byte")
}
return rc.conn.Set(&item)
}
func (rc *MemCache) Delete(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
return rc.conn.Delete(key)
}
func (rc *MemCache) IsExist(key string) bool {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return false
}
}
_, err := rc.conn.Get(key)
return !(err != nil)
}
func (rc *MemCache) Init(cfg interface{}) error {
var opts *MemOpts
if val, ok := cfg.(*MemOpts); !ok {
return errors.New("interface not type MemOpts")
} else {
opts = val
}
if opts.Conn == "" {
return errors.New("config has no conn key")
}
rc.conninfo = strings.Split(opts.Conn, ";")
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
return nil
}
func (rc *MemCache) connectInit() error {
rc.conn = memcache.New(rc.conninfo...)
return nil
}
func init() {
Register("memcache", NewMemCache())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。