1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-05 05:40:54 +00:00
Beego/cache/memcache/memcache.go

144 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"
"strings"
2013-12-03 13:37:39 +00:00
"github.com/bradfitz/gomemcache/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 {
conn *memcache.Client
conninfo []string
2013-04-22 10:56:30 +00:00
}
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
}
if item, err := rc.conn.Get(key); err == nil {
return string(item.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")
}
item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout)}
return rc.conn.Set(&item)
2013-04-22 10:56:30 +00:00
}
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
}
return rc.conn.Delete(key)
2013-04-22 10:56:30 +00:00
}
2013-12-22 05:35:02 +00:00
// increase counter.
func (rc *MemcacheCache) Incr(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Increment(key, 1)
return err
2013-07-16 11:05:44 +00:00
}
2013-12-22 05:35:02 +00:00
// decrease counter.
func (rc *MemcacheCache) Decr(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Decrement(key, 1)
return err
2013-07-16 11:05:44 +00:00
}
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
}
_, err := rc.conn.Get(key)
2013-04-22 10:56:30 +00:00
if err != nil {
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 = strings.Split(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 {
rc.conn = memcache.New(rc.conninfo...)
2014-07-12 01:52:15 +00:00
return nil
2013-04-22 10:56:30 +00:00
}
func init() {
cache.Register("memcache", NewMemCache())
2013-04-22 10:56:30 +00:00
}