Commit e635e274 authored by astaxie's avatar astaxie

Merge branch 'master' into develop

parents 46cde6e5 cec151fd
...@@ -34,11 +34,11 @@ type Cache interface { ...@@ -34,11 +34,11 @@ type Cache interface {
Incr(key string) error Incr(key string) error
// decrease cached int value by key, as a counter. // decrease cached int value by key, as a counter.
Decr(key string) error Decr(key string) error
// check cached value is existed or not. // check if cached value exists or not.
IsExist(key string) bool IsExist(key string) bool
// clear all cache. // clear all cache.
ClearAll() error ClearAll() error
// start gc routine via config string setting. // start gc routine based on config string settings.
StartAndGC(config string) error StartAndGC(config string) error
} }
...@@ -57,13 +57,13 @@ func Register(name string, adapter Cache) { ...@@ -57,13 +57,13 @@ func Register(name string, adapter Cache) {
adapters[name] = adapter adapters[name] = adapter
} }
// Create a new cache driver by adapter and config string. // Create a new cache driver by adapter name and config string.
// config need to be correct JSON as string: {"interval":360}. // config need to be correct JSON as string: {"interval":360}.
// it will start gc automatically. // it will start gc automatically.
func NewCache(adapterName, config string) (Cache, error) { func NewCache(adapterName, config string) (Cache, error) {
adapter, ok := adapters[adapterName] adapter, ok := adapters[adapterName]
if !ok { if !ok {
return nil, fmt.Errorf("cache: unknown adaptername %q (forgotten import?)", adapterName) return nil, fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
} }
err := adapter.StartAndGC(config) err := adapter.StartAndGC(config)
if err != nil { if err != nil {
......
...@@ -46,7 +46,7 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa ...@@ -46,7 +46,7 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
// Get cache from redis. // Get cache from redis.
func (rc *RedisCache) Get(key string) interface{} { func (rc *RedisCache) Get(key string) interface{} {
v, err := rc.do("HGET", rc.key, key) v, err := rc.do("GET", key)
if err != nil { if err != nil {
return nil return nil
} }
...@@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} { ...@@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} {
} }
// put cache to redis. // put cache to redis.
// timeout is ignored.
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { 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 return err
} }
// delete cache in redis. // delete cache in redis.
func (rc *RedisCache) Delete(key string) error { 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 return err
} }
// check cache exist in redis. // check cache's existence in redis.
func (rc *RedisCache) IsExist(key string) bool { 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 { if err != nil {
return false return false
} }
if v == false {
_, err := rc.do("HDEL", rc.key, key)
if err != nil {
return false
}
}
return v return v
} }
// increase counter in redis. // increase counter in redis.
func (rc *RedisCache) Incr(key string) error { 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 return err
} }
// decrease counter in redis. // decrease counter in redis.
func (rc *RedisCache) Decr(key string) error { 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 return err
} }
// clean all cache in redis. delete this redis collection. // clean all cache in redis. delete this redis collection.
func (rc *RedisCache) ClearAll() error { 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 return err
} }
......
// 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")
}
}
...@@ -139,7 +139,7 @@ func detectTZ(al *alias) { ...@@ -139,7 +139,7 @@ func detectTZ(al *alias) {
if engine != "" { if engine != "" {
al.Engine = engine al.Engine = engine
} else { } else {
engine = "INNODB" al.Engine = "INNODB"
} }
case DR_Sqlite: case DR_Sqlite:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment