a369b15e by Pengfei Xue

reset cache connection to nil, if err isio.EOF

* this will support auto-connection
1 parent e34f8c46
...@@ -3,6 +3,7 @@ package cache ...@@ -3,6 +3,7 @@ package cache
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "errors" 5 "errors"
6 "io"
6 7
7 "github.com/beego/redigo/redis" 8 "github.com/beego/redigo/redis"
8 ) 9 )
...@@ -33,10 +34,18 @@ func (rc *RedisCache) Get(key string) interface{} { ...@@ -33,10 +34,18 @@ func (rc *RedisCache) Get(key string) interface{} {
33 return nil 34 return nil
34 } 35 }
35 } 36 }
37
36 v, err := rc.c.Do("HGET", rc.key, key) 38 v, err := rc.c.Do("HGET", rc.key, key)
39 // write to closed socket, reset rc.c to nil
40 if err == io.EOF {
41 rc.c = nil
42 return nil
43 }
44
37 if err != nil { 45 if err != nil {
38 return nil 46 return nil
39 } 47 }
48
40 return v 49 return v
41 } 50 }
42 51
...@@ -50,7 +59,14 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { ...@@ -50,7 +59,14 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
50 return err 59 return err
51 } 60 }
52 } 61 }
62
53 _, err := rc.c.Do("HSET", rc.key, key, val) 63 _, err := rc.c.Do("HSET", rc.key, key, val)
64 // write to closed socket, reset rc.c to nil
65 if err == io.EOF {
66 rc.c = nil
67 return err
68 }
69
54 return err 70 return err
55 } 71 }
56 72
...@@ -63,7 +79,14 @@ func (rc *RedisCache) Delete(key string) error { ...@@ -63,7 +79,14 @@ func (rc *RedisCache) Delete(key string) error {
63 return err 79 return err
64 } 80 }
65 } 81 }
82
66 _, err := rc.c.Do("HDEL", rc.key, key) 83 _, err := rc.c.Do("HDEL", rc.key, key)
84 // write to closed socket, reset rc.c to nil
85 if err == io.EOF {
86 rc.c = nil
87 return err
88 }
89
67 return err 90 return err
68 } 91 }
69 92
...@@ -76,10 +99,18 @@ func (rc *RedisCache) IsExist(key string) bool { ...@@ -76,10 +99,18 @@ func (rc *RedisCache) IsExist(key string) bool {
76 return false 99 return false
77 } 100 }
78 } 101 }
102
79 v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key)) 103 v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key))
104 // write to closed socket, reset rc.c to nil
105 if err == io.EOF {
106 rc.c = nil
107 return false
108 }
109
80 if err != nil { 110 if err != nil {
81 return false 111 return false
82 } 112 }
113
83 return v 114 return v
84 } 115 }
85 116
...@@ -92,11 +123,14 @@ func (rc *RedisCache) Incr(key string) error { ...@@ -92,11 +123,14 @@ func (rc *RedisCache) Incr(key string) error {
92 return err 123 return err
93 } 124 }
94 } 125 }
126
95 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1)) 127 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1))
96 if err != nil { 128 // write to closed socket
97 return err 129 if err == io.EOF {
130 rc.c = nil
98 } 131 }
99 return nil 132
133 return err
100 } 134 }
101 135
102 // decrease counter in redis. 136 // decrease counter in redis.
...@@ -108,11 +142,15 @@ func (rc *RedisCache) Decr(key string) error { ...@@ -108,11 +142,15 @@ func (rc *RedisCache) Decr(key string) error {
108 return err 142 return err
109 } 143 }
110 } 144 }
145
111 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1)) 146 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1))
112 if err != nil { 147
113 return err 148 // write to closed socket
149 if err == io.EOF {
150 rc.c = nil
114 } 151 }
115 return nil 152
153 return err
116 } 154 }
117 155
118 // clean all cache in redis. delete this redis collection. 156 // clean all cache in redis. delete this redis collection.
...@@ -124,7 +162,13 @@ func (rc *RedisCache) ClearAll() error { ...@@ -124,7 +162,13 @@ func (rc *RedisCache) ClearAll() error {
124 return err 162 return err
125 } 163 }
126 } 164 }
165
127 _, err := rc.c.Do("DEL", rc.key) 166 _, err := rc.c.Do("DEL", rc.key)
167 // write to closed socket
168 if err == io.EOF {
169 rc.c = nil
170 }
171
128 return err 172 return err
129 } 173 }
130 174
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!