Beego/cache/memcache/memcache.go

146 lines
3.0 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @description beego is an open-source, high-performance web framework for the Go programming language.
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @link http://github.com/astaxie/beego for the canonical source repository
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @license http://github.com/astaxie/beego/blob/master/LICENSE
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @authors astaxie
2014-07-12 01:52:15 +00:00
package memcache
2013-04-22 10:56:30 +00:00
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 {
2014-07-12 01:52:15 +00:00
conn *memcache.Connection
2013-04-22 10:56:30 +00:00
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{} {
2014-07-12 01:52:15 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2014-01-09 10:50:30 +00:00
return err
}
2013-04-22 10:56:30 +00:00
}
2014-07-12 01:52:15 +00:00
if v, err := rc.conn.Get(key); err == nil {
if len(v) > 0 {
return string(v[0].Value)
}
2013-08-09 14:19:05 +00:00
}
2014-07-12 01:52:15 +00:00
return nil
2013-04-22 10:56:30 +00:00
}
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 {
2014-07-12 01:52:15 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2014-01-09 10:50:30 +00:00
return err
}
2013-04-22 10:56:30 +00:00
}
v, ok := val.(string)
if !ok {
return errors.New("val must string")
}
2014-07-12 01:52:15 +00:00
stored, err := rc.conn.Set(key, 0, uint64(timeout), []byte(v))
2013-04-22 10:56:30 +00:00
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 {
2014-07-12 01:52:15 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2014-01-09 10:50:30 +00:00
return err
}
2013-04-22 10:56:30 +00:00
}
2014-07-12 01:52:15 +00:00
_, err := rc.conn.Delete(key)
2013-04-22 10:56:30 +00:00
return err
}
2013-12-22 05:35:02 +00:00
// [Not Support]
// increase counter.
2014-07-12 01:52:15 +00:00
func (rc *MemcacheCache) Incr(_ string) error {
2013-07-16 11:05:44 +00:00
return errors.New("not support in memcache")
}
2013-12-22 05:35:02 +00:00
// [Not Support]
// decrease counter.
2014-07-12 01:52:15 +00:00
func (rc *MemcacheCache) Decr(_ string) error {
2013-07-16 11:05:44 +00:00
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 {
2014-07-12 01:52:15 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2014-01-09 10:50:30 +00:00
return false
}
2013-04-22 10:56:30 +00:00
}
2014-07-12 01:52:15 +00:00
v, err := rc.conn.Get(key)
2013-04-22 10:56:30 +00:00
if err != nil {
return false
}
if len(v) == 0 {
return false
}
2014-07-12 01:52:15 +00:00
return true
2013-04-22 10:56:30 +00:00
}
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 {
2014-07-12 01:52:15 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2014-01-09 10:50:30 +00:00
return err
}
2013-04-22 10:56:30 +00:00
}
2014-07-12 01:52:15 +00:00
return rc.conn.FlushAll()
2013-04-22 10:56:30 +00:00
}
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-07-12 01:52:15 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
2014-05-16 17:19:47 +00:00
}
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-07-12 01:52:15 +00:00
func (rc *MemcacheCache) connectInit() error {
2013-04-22 10:56:30 +00:00
c, err := memcache.Connect(rc.conninfo)
if err != nil {
2014-07-12 01:52:15 +00:00
return err
2013-04-22 10:56:30 +00:00
}
2014-07-12 01:52:15 +00:00
rc.conn = c
return nil
2013-04-22 10:56:30 +00:00
}
func init() {
cache.Register("memcache", NewMemCache())
2013-04-22 10:56:30 +00:00
}