1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-11 09:00:39 +00:00

Merge branch 'master' into develop

This commit is contained in:
astaxie
2014-07-09 08:43:37 +08:00
4 changed files with 119 additions and 15 deletions

43
cache/redis/redis.go vendored
View File

@ -46,7 +46,7 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
// Get cache from redis.
func (rc *RedisCache) Get(key string) interface{} {
v, err := rc.do("HGET", rc.key, key)
v, err := rc.do("GET", key)
if err != nil {
return nil
}
@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} {
}
// put cache to redis.
// timeout is ignored.
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
_, err := rc.do("HSET", rc.key, key, val)
_, err := rc.do("SET", key, val)
if err != nil {
return nil
}
_, err = rc.do("HSET", rc.key, key, true)
if err != nil {
return nil
}
_, err = rc.do("EXPIRE", key, timeout)
return err
}
// delete cache in redis.
func (rc *RedisCache) Delete(key string) error {
_, err := rc.do("HDEL", rc.key, key)
_, err := rc.do("DEL", key)
if err != nil {
return nil
}
_, err = rc.do("HDEL", rc.key, key)
return err
}
// check cache exist in redis.
// check cache's existence in redis.
func (rc *RedisCache) IsExist(key string) bool {
v, err := redis.Bool(rc.do("HEXISTS", rc.key, key))
v, err := redis.Bool(rc.do("EXISTS", key))
if err != nil {
return false
}
if v == false {
_, err := rc.do("HDEL", rc.key, key)
if err != nil {
return false
}
}
return v
}
// increase counter in redis.
func (rc *RedisCache) Incr(key string) error {
_, err := redis.Bool(rc.do("HINCRBY", rc.key, key, 1))
_, err := redis.Bool(rc.do("INCRBY", key, 1))
return err
}
// decrease counter in redis.
func (rc *RedisCache) Decr(key string) error {
_, err := redis.Bool(rc.do("HINCRBY", rc.key, key, -1))
_, err := redis.Bool(rc.do("INCRBY", key, -1))
return err
}
// clean all cache in redis. delete this redis collection.
func (rc *RedisCache) ClearAll() error {
_, err := rc.do("DEL", rc.key)
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)
return err
}

81
cache/redis/redis_test.go vendored Normal file
View File

@ -0,0 +1,81 @@
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
package cache
import (
"testing"
"time"
"github.com/beego/redigo/redis"
"github.com/astaxie/beego/cache"
)
func TestRedisCache(t *testing.T) {
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
if err != nil {
t.Error("init err")
}
if err = bm.Put("astaxie", 1, 10); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
time.Sleep(10 * time.Second)
if bm.IsExist("astaxie") {
t.Error("check err")
}
if err = bm.Put("astaxie", 1, 10); err != nil {
t.Error("set Error", err)
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
t.Error("get err")
}
if err = bm.Incr("astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
t.Error("get err")
}
if err = bm.Decr("astaxie"); err != nil {
t.Error("Decr Error", err)
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
t.Error("get err")
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
t.Error("delete err")
}
//test string
if err = bm.Put("astaxie", "author", 10); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
t.Error("get err")
}
// test clear all
if err = bm.ClearAll(); err != nil {
t.Error("clear all err")
}
}