Beego/cache/memcache/memcache.go

190 lines
4.1 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// package memcahe for cache provider
//
// depend on github.com/bradfitz/gomemcache/memcache
//
// go install github.com/bradfitz/gomemcache/memcache
//
// Usage:
// import(
// _ "github.com/astaxie/beego/cache/memcache"
// "github.com/astaxie/beego/cache"
// )
//
// bm, err := cache.NewCache("memcache", `{"conn":"127.0.0.1:11211"}`)
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// more docs http://beego.me/docs/module/cache.md
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
}
// get value from memcache.
func (rc *MemcacheCache) GetMulti(keys []string) []interface{} {
size := len(keys)
var rv []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
for i := 0; i < size; i++ {
rv = append(rv, err)
}
return rv
}
}
mv, err := rc.conn.GetMulti(keys)
if err == nil {
for _, v := range mv {
rv = append(rv, string(v.Value))
}
return rv
} else {
for i := 0; i < size; i++ {
rv = append(rv, err)
}
return rv
}
}
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
}