2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-06-25 02:39:37 +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-06-25 02:39:37 +00:00
|
|
|
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-04-12 05:18:18 +00:00
|
|
|
// @authors astaxie
|
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2014-01-10 10:31:15 +00:00
|
|
|
"time"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
2013-11-21 14:19:19 +00:00
|
|
|
"github.com/beego/redigo/redis"
|
2014-04-04 00:31:22 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/cache"
|
2013-04-22 10:56:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-12-22 05:35:02 +00:00
|
|
|
// the collection name of redis for cache adapter.
|
2013-04-22 10:56:30 +00:00
|
|
|
DefaultKey string = "beecacheRedis"
|
|
|
|
)
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// Redis cache adapter.
|
2013-04-22 10:56:30 +00:00
|
|
|
type RedisCache struct {
|
2014-01-10 10:31:15 +00:00
|
|
|
p *redis.Pool // redis connection pool
|
2013-04-22 10:56:30 +00:00
|
|
|
conninfo string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// create new redis cache with default collection name.
|
2013-04-22 10:56:30 +00:00
|
|
|
func NewRedisCache() *RedisCache {
|
|
|
|
return &RedisCache{key: DefaultKey}
|
|
|
|
}
|
|
|
|
|
2014-01-10 10:31:15 +00:00
|
|
|
// actually do the redis cmds
|
|
|
|
func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interface{}, err error) {
|
|
|
|
c := rc.p.Get()
|
|
|
|
defer c.Close()
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2014-01-10 10:31:15 +00:00
|
|
|
return c.Do(commandName, args...)
|
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2014-01-10 10:31:15 +00:00
|
|
|
// Get cache from redis.
|
|
|
|
func (rc *RedisCache) Get(key string) interface{} {
|
2014-07-05 14:27:31 +00:00
|
|
|
v, err := rc.do("GET", key)
|
2013-04-22 10:56:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// put cache to redis.
|
2013-07-04 05:02:11 +00:00
|
|
|
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
|
2014-07-05 14:27:31 +00:00
|
|
|
_, err := rc.do("HSET", rc.key, key, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err = rc.do("SET", key, val)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err = rc.do("EXPIRE", key, timeout)
|
2013-04-22 10:56:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// delete cache in redis.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (rc *RedisCache) Delete(key string) error {
|
2014-07-05 14:27:31 +00:00
|
|
|
_, err := rc.do("DEL", key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err = rc.do("HDEL", rc.key, key)
|
2013-04-22 10:56:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-05 14:27:31 +00:00
|
|
|
// check cache's existence in redis.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (rc *RedisCache) IsExist(key string) bool {
|
2014-07-05 14:27:31 +00:00
|
|
|
v, err := redis.Bool(rc.do("EXISTS", key))
|
2013-04-22 10:56:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2014-07-05 14:27:31 +00:00
|
|
|
if v == false {
|
|
|
|
_, err := rc.do("HDEL", rc.key, key)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// increase counter in redis.
|
2013-07-16 11:05:44 +00:00
|
|
|
func (rc *RedisCache) Incr(key string) error {
|
2014-07-05 14:27:31 +00:00
|
|
|
_, err := redis.Bool(rc.do("INCRBY", key, 1))
|
2014-01-09 10:49:18 +00:00
|
|
|
return err
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// decrease counter in redis.
|
2013-07-16 11:05:44 +00:00
|
|
|
func (rc *RedisCache) Decr(key string) error {
|
2014-07-05 14:27:31 +00:00
|
|
|
_, err := redis.Bool(rc.do("INCRBY", key, -1))
|
2014-01-09 10:49:18 +00:00
|
|
|
return err
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// clean all cache in redis. delete this redis collection.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (rc *RedisCache) ClearAll() error {
|
2014-07-05 14:27:31 +00:00
|
|
|
cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
|
|
|
|
for _, str := range cachedKeys {
|
|
|
|
_, err := rc.do("DEL", str)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = rc.do("DEL", rc.key)
|
2013-04-22 10:56:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// start redis cache adapter.
|
|
|
|
// config is like {"key":"collection key","conn":"connection info"}
|
|
|
|
// the cache item in redis are stored forever,
|
|
|
|
// so no gc operation.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (rc *RedisCache) StartAndGC(config string) error {
|
|
|
|
var cf map[string]string
|
|
|
|
json.Unmarshal([]byte(config), &cf)
|
2014-01-10 10:31:15 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
if _, ok := cf["key"]; !ok {
|
|
|
|
cf["key"] = DefaultKey
|
|
|
|
}
|
2014-01-10 10:31:15 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
if _, ok := cf["conn"]; !ok {
|
|
|
|
return errors.New("config has no conn key")
|
|
|
|
}
|
2014-01-10 10:31:15 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
rc.key = cf["key"]
|
|
|
|
rc.conninfo = cf["conn"]
|
2014-01-10 10:31:15 +00:00
|
|
|
rc.connectInit()
|
|
|
|
|
|
|
|
c := rc.p.Get()
|
|
|
|
defer c.Close()
|
|
|
|
if err := c.Err(); err != nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-01-10 10:31:15 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// connect to redis.
|
2014-01-10 10:31:15 +00:00
|
|
|
func (rc *RedisCache) connectInit() {
|
|
|
|
// initialize a new pool
|
|
|
|
rc.p = &redis.Pool{
|
|
|
|
MaxIdle: 3,
|
|
|
|
IdleTimeout: 180 * time.Second,
|
|
|
|
Dial: func() (redis.Conn, error) {
|
|
|
|
c, err := redis.Dial("tcp", rc.conninfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
},
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2014-04-04 00:31:22 +00:00
|
|
|
cache.Register("redis", NewRedisCache())
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|