1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 03:41:29 +00:00
Beego/cache/memcache/memcache.go

161 lines
3.2 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
2013-04-22 10:56:30 +00:00
package cache
import (
"encoding/json"
"errors"
2013-12-03 13:37:39 +00:00
2013-11-21 14:19:19 +00:00
"github.com/beego/memcache"
"github.com/astaxie/beego/cache"
2013-04-22 10:56:30 +00:00
)
2013-12-22 05:35:02 +00:00
// Memcache adapter.
2013-04-22 10:56:30 +00:00
type MemcacheCache struct {
c *memcache.Connection
conninfo string
}
2013-12-22 05:35:02 +00:00
// create new memcache adapter.
2013-04-22 10:56:30 +00:00
func NewMemCache() *MemcacheCache {
return &MemcacheCache{}
}
2013-12-22 05:35:02 +00:00
// get value from memcache.
2013-04-22 10:56:30 +00:00
func (rc *MemcacheCache) Get(key string) interface{} {
if rc.c == nil {
2014-01-09 10:50:30 +00:00
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
2013-04-22 10:56:30 +00:00
}
2013-08-06 08:04:35 +00:00
v, err := rc.c.Get(key)
2013-04-22 10:56:30 +00:00
if err != nil {
return nil
}
var contain interface{}
2013-08-09 14:19:05 +00:00
if len(v) > 0 {
contain = string(v[0].Value)
} else {
contain = nil
}
2013-04-22 10:56:30 +00:00
return contain
}
2013-12-22 05:35:02 +00:00
// put value to memcache. only support string.
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
2013-04-22 10:56:30 +00:00
if rc.c == nil {
2014-01-09 10:50:30 +00:00
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
2013-04-22 10:56:30 +00:00
}
v, ok := val.(string)
if !ok {
return errors.New("val must string")
}
stored, err := rc.c.Set(key, 0, uint64(timeout), []byte(v))
if err == nil && stored == false {
return errors.New("stored fail")
}
return err
}
2013-12-22 05:35:02 +00:00
// delete value in memcache.
2013-04-22 10:56:30 +00:00
func (rc *MemcacheCache) Delete(key string) error {
if rc.c == nil {
2014-01-09 10:50:30 +00:00
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
2013-04-22 10:56:30 +00:00
}
_, err := rc.c.Delete(key)
return err
}
2013-12-22 05:35:02 +00:00
// [Not Support]
// increase counter.
2013-07-16 11:05:44 +00:00
func (rc *MemcacheCache) Incr(key string) error {
return errors.New("not support in memcache")
}
2013-12-22 05:35:02 +00:00
// [Not Support]
// decrease counter.
2013-07-16 11:05:44 +00:00
func (rc *MemcacheCache) Decr(key string) error {
return errors.New("not support in memcache")
}
2013-12-22 05:35:02 +00:00
// check value exists in memcache.
2013-04-22 10:56:30 +00:00
func (rc *MemcacheCache) IsExist(key string) bool {
if rc.c == nil {
2014-01-09 10:50:30 +00:00
var err error
rc.c, err = rc.connectInit()
if err != nil {
return false
}
2013-04-22 10:56:30 +00:00
}
2013-08-06 08:04:35 +00:00
v, err := rc.c.Get(key)
2013-04-22 10:56:30 +00:00
if err != nil {
return false
}
if len(v) == 0 {
return false
} else {
return true
}
}
2013-12-22 05:35:02 +00:00
// clear all cached in memcache.
2013-04-22 10:56:30 +00:00
func (rc *MemcacheCache) ClearAll() error {
if rc.c == nil {
2014-01-09 10:50:30 +00:00
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
2013-04-22 10:56:30 +00:00
}
err := rc.c.FlushAll()
return err
}
2013-12-22 05:35:02 +00:00
// start memcache adapter.
// config string is like {"conn":"connection info"}.
// if connecting error, return.
2013-04-22 10:56:30 +00:00
func (rc *MemcacheCache) StartAndGC(config string) error {
var cf map[string]string
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key")
}
rc.conninfo = cf["conn"]
2014-01-09 10:50:30 +00:00
var err error
2014-05-16 17:19:47 +00:00
if rc.c != nil {
rc.c, err = rc.connectInit()
if err != nil {
return errors.New("dial tcp conn error")
}
2013-04-22 10:56:30 +00:00
}
return nil
}
2013-12-22 05:35:02 +00:00
// connect to memcache and keep the connection.
2014-01-09 10:50:30 +00:00
func (rc *MemcacheCache) connectInit() (*memcache.Connection, error) {
2013-04-22 10:56:30 +00:00
c, err := memcache.Connect(rc.conninfo)
if err != nil {
2014-01-09 10:50:30 +00:00
return nil, err
2013-04-22 10:56:30 +00:00
}
2014-01-09 10:50:30 +00:00
return c, nil
2013-04-22 10:56:30 +00:00
}
func init() {
cache.Register("memcache", NewMemCache())
2013-04-22 10:56:30 +00:00
}