Beego/cache/memcache/memcache.go

189 lines
4.2 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.
2015-09-08 16:15:03 +00:00
// Package memcache for cache provider
2014-08-18 08:41:43 +00:00
//
// 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"
2016-01-08 05:47:14 +00:00
"time"
2016-01-17 15:57:07 +00:00
"github.com/astaxie/beego/cache"
2016-08-30 13:24:56 +00:00
"github.com/bradfitz/gomemcache/memcache"
2013-04-22 10:56:30 +00:00
)
2015-09-08 16:15:03 +00:00
// Cache Memcache adapter.
type Cache struct {
conn *memcache.Client
conninfo []string
2013-04-22 10:56:30 +00:00
}
2015-09-08 16:15:03 +00:00
// NewMemCache create new memcache adapter.
func NewMemCache() cache.Cache {
2015-09-08 16:15:03 +00:00
return &Cache{}
2013-04-22 10:56:30 +00:00
}
2015-09-08 16:15:03 +00:00
// Get get value from memcache.
func (rc *Cache) 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 {
2016-08-30 13:24:56 +00:00
return 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
}
2015-09-08 16:15:03 +00:00
// GetMulti get value from memcache.
func (rc *Cache) 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 {
2016-08-30 13:24:56 +00:00
rv = append(rv, v.Value)
}
return rv
}
2015-09-08 16:15:03 +00:00
for i := 0; i < size; i++ {
rv = append(rv, err)
}
return rv
}
2016-08-30 13:24:56 +00:00
// Put put value to memcache.
2016-01-08 05:47:14 +00:00
func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) 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
}
2016-08-30 13:24:56 +00:00
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")
2013-04-22 10:56:30 +00:00
}
return rc.conn.Set(&item)
2013-04-22 10:56:30 +00:00
}
2015-09-08 16:15:03 +00:00
// Delete delete value in memcache.
func (rc *Cache) 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
}
2015-09-08 16:15:03 +00:00
// Incr increase counter.
func (rc *Cache) 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
}
2015-09-08 16:15:03 +00:00
// Decr decrease counter.
func (rc *Cache) 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
}
2015-09-08 16:15:03 +00:00
// IsExist check value exists in memcache.
func (rc *Cache) 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)
2017-03-17 17:24:45 +00:00
return !(err != nil)
2013-04-22 10:56:30 +00:00
}
2015-09-08 16:15:03 +00:00
// ClearAll clear all cached in memcache.
func (rc *Cache) 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
}
2015-09-08 16:15:03 +00:00
// StartAndGC start memcache adapter.
2013-12-22 05:35:02 +00:00
// config string is like {"conn":"connection info"}.
// if connecting error, return.
2015-09-08 16:15:03 +00:00
func (rc *Cache) StartAndGC(config string) error {
2013-04-22 10:56:30 +00:00
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.
2015-09-08 16:15:03 +00:00
func (rc *Cache) 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
}