2013-04-22 10:56:30 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2014-01-09 10:49:18 +00:00
|
|
|
"io"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
2013-11-21 14:19:19 +00:00
|
|
|
"github.com/beego/redigo/redis"
|
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 {
|
|
|
|
c redis.Conn
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// Get cache from redis.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (rc *RedisCache) Get(key string) interface{} {
|
|
|
|
if rc.c == nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
v, err := rc.c.Do("HGET", rc.key, key)
|
2014-01-09 10:49:18 +00:00
|
|
|
// write to closed socket, reset rc.c to nil
|
|
|
|
if err == io.EOF {
|
|
|
|
rc.c = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
// timeout is ignored.
|
2013-07-04 05:02:11 +00:00
|
|
|
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
|
2013-04-22 10:56:30 +00:00
|
|
|
if rc.c == nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
_, err := rc.c.Do("HSET", rc.key, key, val)
|
2014-01-09 10:49:18 +00:00
|
|
|
// write to closed socket, reset rc.c to nil
|
|
|
|
if err == io.EOF {
|
|
|
|
rc.c = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if rc.c == nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
_, err := rc.c.Do("HDEL", rc.key, key)
|
2014-01-09 10:49:18 +00:00
|
|
|
// write to closed socket, reset rc.c to nil
|
|
|
|
if err == io.EOF {
|
|
|
|
rc.c = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// check cache exist in redis.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (rc *RedisCache) IsExist(key string) bool {
|
|
|
|
if rc.c == nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key))
|
2014-01-09 10:49:18 +00:00
|
|
|
// write to closed socket, reset rc.c to nil
|
|
|
|
if err == io.EOF {
|
|
|
|
rc.c = nil
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
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
|
|
|
// increase counter in redis.
|
2013-07-16 11:05:44 +00:00
|
|
|
func (rc *RedisCache) Incr(key string) error {
|
|
|
|
if rc.c == nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-07-16 11:05:44 +00:00
|
|
|
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1))
|
2014-01-09 10:49:18 +00:00
|
|
|
// write to closed socket
|
|
|
|
if err == io.EOF {
|
|
|
|
rc.c = nil
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
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 {
|
|
|
|
if rc.c == nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-07-16 11:05:44 +00:00
|
|
|
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1))
|
2014-01-09 10:49:18 +00:00
|
|
|
|
|
|
|
// write to closed socket
|
|
|
|
if err == io.EOF {
|
|
|
|
rc.c = nil
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
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 {
|
|
|
|
if rc.c == nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
_, err := rc.c.Do("DEL", rc.key)
|
2014-01-09 10:49:18 +00:00
|
|
|
// write to closed socket
|
|
|
|
if err == io.EOF {
|
|
|
|
rc.c = nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if _, ok := cf["key"]; !ok {
|
|
|
|
cf["key"] = DefaultKey
|
|
|
|
}
|
|
|
|
if _, ok := cf["conn"]; !ok {
|
|
|
|
return errors.New("config has no conn key")
|
|
|
|
}
|
|
|
|
rc.key = cf["key"]
|
|
|
|
rc.conninfo = cf["conn"]
|
2013-09-30 03:30:22 +00:00
|
|
|
var err error
|
|
|
|
rc.c, err = rc.connectInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
if rc.c == nil {
|
|
|
|
return errors.New("dial tcp conn error")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// connect to redis.
|
2013-09-30 03:30:22 +00:00
|
|
|
func (rc *RedisCache) connectInit() (redis.Conn, error) {
|
2013-04-22 10:56:30 +00:00
|
|
|
c, err := redis.Dial("tcp", rc.conninfo)
|
|
|
|
if err != nil {
|
2013-09-30 03:30:22 +00:00
|
|
|
return nil, err
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
2013-09-30 03:30:22 +00:00
|
|
|
return c, nil
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("redis", NewRedisCache())
|
|
|
|
}
|