e635e274 by astaxie

Merge branch 'master' into develop

2 parents 46cde6e5 cec151fd
...@@ -34,11 +34,11 @@ type Cache interface { ...@@ -34,11 +34,11 @@ type Cache interface {
34 Incr(key string) error 34 Incr(key string) error
35 // decrease cached int value by key, as a counter. 35 // decrease cached int value by key, as a counter.
36 Decr(key string) error 36 Decr(key string) error
37 // check cached value is existed or not. 37 // check if cached value exists or not.
38 IsExist(key string) bool 38 IsExist(key string) bool
39 // clear all cache. 39 // clear all cache.
40 ClearAll() error 40 ClearAll() error
41 // start gc routine via config string setting. 41 // start gc routine based on config string settings.
42 StartAndGC(config string) error 42 StartAndGC(config string) error
43 } 43 }
44 44
...@@ -57,13 +57,13 @@ func Register(name string, adapter Cache) { ...@@ -57,13 +57,13 @@ func Register(name string, adapter Cache) {
57 adapters[name] = adapter 57 adapters[name] = adapter
58 } 58 }
59 59
60 // Create a new cache driver by adapter and config string. 60 // Create a new cache driver by adapter name and config string.
61 // config need to be correct JSON as string: {"interval":360}. 61 // config need to be correct JSON as string: {"interval":360}.
62 // it will start gc automatically. 62 // it will start gc automatically.
63 func NewCache(adapterName, config string) (Cache, error) { 63 func NewCache(adapterName, config string) (Cache, error) {
64 adapter, ok := adapters[adapterName] 64 adapter, ok := adapters[adapterName]
65 if !ok { 65 if !ok {
66 return nil, fmt.Errorf("cache: unknown adaptername %q (forgotten import?)", adapterName) 66 return nil, fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
67 } 67 }
68 err := adapter.StartAndGC(config) 68 err := adapter.StartAndGC(config)
69 if err != nil { 69 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
46 46
47 // Get cache from redis. 47 // Get cache from redis.
48 func (rc *RedisCache) Get(key string) interface{} { 48 func (rc *RedisCache) Get(key string) interface{} {
49 v, err := rc.do("HGET", rc.key, key) 49 v, err := rc.do("GET", key)
50 if err != nil { 50 if err != nil {
51 return nil 51 return nil
52 } 52 }
...@@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} { ...@@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} {
55 } 55 }
56 56
57 // put cache to redis. 57 // put cache to redis.
58 // timeout is ignored.
59 func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { 58 func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
60 _, err := rc.do("HSET", rc.key, key, val) 59 _, err := rc.do("SET", key, val)
60 if err != nil {
61 return nil
62 }
63 _, err = rc.do("HSET", rc.key, key, true)
64 if err != nil {
65 return nil
66 }
67 _, err = rc.do("EXPIRE", key, timeout)
61 return err 68 return err
62 } 69 }
63 70
64 // delete cache in redis. 71 // delete cache in redis.
65 func (rc *RedisCache) Delete(key string) error { 72 func (rc *RedisCache) Delete(key string) error {
66 _, err := rc.do("HDEL", rc.key, key) 73 _, err := rc.do("DEL", key)
74 if err != nil {
75 return nil
76 }
77 _, err = rc.do("HDEL", rc.key, key)
67 return err 78 return err
68 } 79 }
69 80
70 // check cache exist in redis. 81 // check cache's existence in redis.
71 func (rc *RedisCache) IsExist(key string) bool { 82 func (rc *RedisCache) IsExist(key string) bool {
72 v, err := redis.Bool(rc.do("HEXISTS", rc.key, key)) 83 v, err := redis.Bool(rc.do("EXISTS", key))
73 if err != nil { 84 if err != nil {
74 return false 85 return false
75 } 86 }
76 87 if v == false {
88 _, err := rc.do("HDEL", rc.key, key)
89 if err != nil {
90 return false
91 }
92 }
77 return v 93 return v
78 } 94 }
79 95
80 // increase counter in redis. 96 // increase counter in redis.
81 func (rc *RedisCache) Incr(key string) error { 97 func (rc *RedisCache) Incr(key string) error {
82 _, err := redis.Bool(rc.do("HINCRBY", rc.key, key, 1)) 98 _, err := redis.Bool(rc.do("INCRBY", key, 1))
83 return err 99 return err
84 } 100 }
85 101
86 // decrease counter in redis. 102 // decrease counter in redis.
87 func (rc *RedisCache) Decr(key string) error { 103 func (rc *RedisCache) Decr(key string) error {
88 _, err := redis.Bool(rc.do("HINCRBY", rc.key, key, -1)) 104 _, err := redis.Bool(rc.do("INCRBY", key, -1))
89 return err 105 return err
90 } 106 }
91 107
92 // clean all cache in redis. delete this redis collection. 108 // clean all cache in redis. delete this redis collection.
93 func (rc *RedisCache) ClearAll() error { 109 func (rc *RedisCache) ClearAll() error {
94 _, err := rc.do("DEL", rc.key) 110 cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
111 for _, str := range cachedKeys {
112 _, err := rc.do("DEL", str)
113 if err != nil {
114 return nil
115 }
116 }
117 _, err = rc.do("DEL", rc.key)
95 return err 118 return err
96 } 119 }
97 120
......
1 // Beego (http://beego.me/)
2
3 // @description beego is an open-source, high-performance web framework for the Go programming language.
4
5 // @link http://github.com/astaxie/beego for the canonical source repository
6
7 // @license http://github.com/astaxie/beego/blob/master/LICENSE
8
9 // @authors astaxie
10
11 package cache
12
13 import (
14 "testing"
15 "time"
16
17 "github.com/beego/redigo/redis"
18
19 "github.com/astaxie/beego/cache"
20 )
21
22 func TestRedisCache(t *testing.T) {
23 bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
24 if err != nil {
25 t.Error("init err")
26 }
27 if err = bm.Put("astaxie", 1, 10); err != nil {
28 t.Error("set Error", err)
29 }
30 if !bm.IsExist("astaxie") {
31 t.Error("check err")
32 }
33
34 time.Sleep(10 * time.Second)
35
36 if bm.IsExist("astaxie") {
37 t.Error("check err")
38 }
39 if err = bm.Put("astaxie", 1, 10); err != nil {
40 t.Error("set Error", err)
41 }
42
43 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
44 t.Error("get err")
45 }
46
47 if err = bm.Incr("astaxie"); err != nil {
48 t.Error("Incr Error", err)
49 }
50
51 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
52 t.Error("get err")
53 }
54
55 if err = bm.Decr("astaxie"); err != nil {
56 t.Error("Decr Error", err)
57 }
58
59 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
60 t.Error("get err")
61 }
62 bm.Delete("astaxie")
63 if bm.IsExist("astaxie") {
64 t.Error("delete err")
65 }
66 //test string
67 if err = bm.Put("astaxie", "author", 10); err != nil {
68 t.Error("set Error", err)
69 }
70 if !bm.IsExist("astaxie") {
71 t.Error("check err")
72 }
73
74 if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
75 t.Error("get err")
76 }
77 // test clear all
78 if err = bm.ClearAll(); err != nil {
79 t.Error("clear all err")
80 }
81 }
...@@ -139,7 +139,7 @@ func detectTZ(al *alias) { ...@@ -139,7 +139,7 @@ func detectTZ(al *alias) {
139 if engine != "" { 139 if engine != "" {
140 al.Engine = engine 140 al.Engine = engine
141 } else { 141 } else {
142 engine = "INNODB" 142 al.Engine = "INNODB"
143 } 143 }
144 144
145 case DR_Sqlite: 145 case DR_Sqlite:
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!