1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-05 09:20:55 +00:00
Beego/cache/redis/redis.go

192 lines
4.3 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 redis for cache provider
//
// depend on github.com/garyburd/redigo/redis
//
// go install github.com/garyburd/redigo/redis
//
// Usage:
// import(
// _ "github.com/astaxie/beego/cache/redis"
// "github.com/astaxie/beego/cache"
// )
//
// bm, err := cache.NewCache("redis", `{"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 07:51:47 +00:00
package redis
2013-04-22 10:56:30 +00:00
import (
"encoding/json"
"errors"
"strconv"
2014-01-10 10:31:15 +00:00
"time"
2013-12-03 13:37:39 +00:00
"github.com/garyburd/redigo/redis"
"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
dbNum int
2013-04-22 10:56:30 +00:00
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-10 10:31:15 +00:00
return c.Do(commandName, args...)
}
2014-01-10 10:31:15 +00:00
// Get cache from redis.
func (rc *RedisCache) Get(key string) interface{} {
2014-07-12 07:51:47 +00:00
if v, err := rc.do("GET", key); err == nil {
return v
2013-04-22 10:56:30 +00:00
}
2014-07-12 07:51:47 +00:00
return nil
2013-04-22 10:56:30 +00:00
}
2013-12-22 05:35:02 +00:00
// put cache to redis.
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
2014-07-12 07:51:47 +00:00
var err error
2014-10-30 16:28:51 +00:00
if _, err = rc.do("SETEX", key, timeout, val); err != nil {
2014-07-12 07:51:47 +00:00
return err
}
2014-07-12 07:51:47 +00:00
if _, err = rc.do("HSET", rc.key, key, true); err != 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 {
2014-07-12 07:51:47 +00:00
var err error
if _, err = rc.do("DEL", key); err != nil {
return err
}
_, err = rc.do("HDEL", rc.key, key)
2013-04-22 10:56:30 +00:00
return err
}
// check cache's existence in redis.
2013-04-22 10:56:30 +00:00
func (rc *RedisCache) IsExist(key string) bool {
v, err := redis.Bool(rc.do("EXISTS", key))
2013-04-22 10:56:30 +00:00
if err != nil {
return false
}
if v == false {
2014-07-12 07:51:47 +00:00
if _, err = rc.do("HDEL", rc.key, key); 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 {
_, err := redis.Bool(rc.do("INCRBY", key, 1))
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 {
_, err := redis.Bool(rc.do("INCRBY", key, -1))
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 {
cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
2014-07-12 07:51:47 +00:00
if err != nil {
return err
}
for _, str := range cachedKeys {
2014-07-12 07:51:47 +00:00
if _, err = rc.do("DEL", str); err != nil {
return err
}
}
_, 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","dbNum":"0"}
2013-12-22 05:35:02 +00:00
// 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")
}
if _, ok := cf["dbNum"]; !ok {
cf["dbNum"] = "0"
}
2013-04-22 10:56:30 +00:00
rc.key = cf["key"]
rc.conninfo = cf["conn"]
rc.dbNum, _ = strconv.Atoi(cf["dbNum"])
2014-01-10 10:31:15 +00:00
rc.connectInit()
c := rc.p.Get()
defer c.Close()
2014-07-12 07:51:47 +00:00
return c.Err()
2013-04-22 10:56:30 +00:00
}
2013-12-22 05:35:02 +00:00
// connect to redis.
2014-01-10 10:31:15 +00:00
func (rc *RedisCache) connectInit() {
2014-07-12 07:51:47 +00:00
dialFunc := func() (c redis.Conn, err error) {
c, err = redis.Dial("tcp", rc.conninfo)
_, selecterr := c.Do("SELECT", rc.dbNum)
if selecterr != nil {
c.Close()
return nil, selecterr
}
2014-07-12 07:51:47 +00:00
return
}
2014-01-10 10:31:15 +00:00
// initialize a new pool
rc.p = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 180 * time.Second,
2014-07-12 07:51:47 +00:00
Dial: dialFunc,
2014-01-10 10:31:15 +00:00
}
2013-04-22 10:56:30 +00:00
}
func init() {
cache.Register("redis", NewRedisCache())
2013-04-22 10:56:30 +00:00
}