1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 22:10:55 +00:00

Merge pull request #454 from pengfei-xue/develop

support redis cache auto connection
This commit is contained in:
astaxie 2014-01-09 04:49:31 -08:00
commit 299cb9130b
2 changed files with 81 additions and 16 deletions

41
cache/memcache.go vendored
View File

@ -21,7 +21,11 @@ func NewMemCache() *MemcacheCache {
// get value from memcache. // get value from memcache.
func (rc *MemcacheCache) Get(key string) interface{} { func (rc *MemcacheCache) Get(key string) interface{} {
if rc.c == nil { if rc.c == nil {
rc.c = rc.connectInit() var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
} }
v, err := rc.c.Get(key) v, err := rc.c.Get(key)
if err != nil { if err != nil {
@ -39,7 +43,11 @@ func (rc *MemcacheCache) Get(key string) interface{} {
// put value to memcache. only support string. // put value to memcache. only support string.
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
if rc.c == nil { if rc.c == nil {
rc.c = rc.connectInit() var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
} }
v, ok := val.(string) v, ok := val.(string)
if !ok { if !ok {
@ -55,7 +63,11 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
// delete value in memcache. // delete value in memcache.
func (rc *MemcacheCache) Delete(key string) error { func (rc *MemcacheCache) Delete(key string) error {
if rc.c == nil { if rc.c == nil {
rc.c = rc.connectInit() var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
} }
_, err := rc.c.Delete(key) _, err := rc.c.Delete(key)
return err return err
@ -76,7 +88,11 @@ func (rc *MemcacheCache) Decr(key string) error {
// check value exists in memcache. // check value exists in memcache.
func (rc *MemcacheCache) IsExist(key string) bool { func (rc *MemcacheCache) IsExist(key string) bool {
if rc.c == nil { if rc.c == nil {
rc.c = rc.connectInit() var err error
rc.c, err = rc.connectInit()
if err != nil {
return false
}
} }
v, err := rc.c.Get(key) v, err := rc.c.Get(key)
if err != nil { if err != nil {
@ -93,7 +109,11 @@ func (rc *MemcacheCache) IsExist(key string) bool {
// clear all cached in memcache. // clear all cached in memcache.
func (rc *MemcacheCache) ClearAll() error { func (rc *MemcacheCache) ClearAll() error {
if rc.c == nil { if rc.c == nil {
rc.c = rc.connectInit() var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
} }
err := rc.c.FlushAll() err := rc.c.FlushAll()
return err return err
@ -109,20 +129,21 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
return errors.New("config has no conn key") return errors.New("config has no conn key")
} }
rc.conninfo = cf["conn"] rc.conninfo = cf["conn"]
rc.c = rc.connectInit() var err error
if rc.c == nil { rc.c, err = rc.connectInit()
if err != nil {
return errors.New("dial tcp conn error") return errors.New("dial tcp conn error")
} }
return nil return nil
} }
// connect to memcache and keep the connection. // connect to memcache and keep the connection.
func (rc *MemcacheCache) connectInit() *memcache.Connection { func (rc *MemcacheCache) connectInit() (*memcache.Connection, error) {
c, err := memcache.Connect(rc.conninfo) c, err := memcache.Connect(rc.conninfo)
if err != nil { if err != nil {
return nil return nil, err
} }
return c return c, nil
} }
func init() { func init() {

56
cache/redis.go vendored
View File

@ -3,6 +3,7 @@ package cache
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"io"
"github.com/beego/redigo/redis" "github.com/beego/redigo/redis"
) )
@ -33,10 +34,18 @@ func (rc *RedisCache) Get(key string) interface{} {
return nil return nil
} }
} }
v, err := rc.c.Do("HGET", rc.key, key) v, err := rc.c.Do("HGET", rc.key, key)
// write to closed socket, reset rc.c to nil
if err == io.EOF {
rc.c = nil
return nil
}
if err != nil { if err != nil {
return nil return nil
} }
return v return v
} }
@ -50,7 +59,14 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
return err return err
} }
} }
_, err := rc.c.Do("HSET", rc.key, key, val) _, err := rc.c.Do("HSET", rc.key, key, val)
// write to closed socket, reset rc.c to nil
if err == io.EOF {
rc.c = nil
return err
}
return err return err
} }
@ -63,7 +79,14 @@ func (rc *RedisCache) Delete(key string) error {
return err return err
} }
} }
_, err := rc.c.Do("HDEL", rc.key, key) _, err := rc.c.Do("HDEL", rc.key, key)
// write to closed socket, reset rc.c to nil
if err == io.EOF {
rc.c = nil
return err
}
return err return err
} }
@ -76,10 +99,18 @@ func (rc *RedisCache) IsExist(key string) bool {
return false return false
} }
} }
v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key)) v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key))
// write to closed socket, reset rc.c to nil
if err == io.EOF {
rc.c = nil
return false
}
if err != nil { if err != nil {
return false return false
} }
return v return v
} }
@ -92,11 +123,14 @@ func (rc *RedisCache) Incr(key string) error {
return err return err
} }
} }
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1)) _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1))
if err != nil { // write to closed socket
return err if err == io.EOF {
rc.c = nil
} }
return nil
return err
} }
// decrease counter in redis. // decrease counter in redis.
@ -108,11 +142,15 @@ func (rc *RedisCache) Decr(key string) error {
return err return err
} }
} }
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1)) _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1))
if err != nil {
return err // write to closed socket
if err == io.EOF {
rc.c = nil
} }
return nil
return err
} }
// clean all cache in redis. delete this redis collection. // clean all cache in redis. delete this redis collection.
@ -124,7 +162,13 @@ func (rc *RedisCache) ClearAll() error {
return err return err
} }
} }
_, err := rc.c.Do("DEL", rc.key) _, err := rc.c.Do("DEL", rc.key)
// write to closed socket
if err == io.EOF {
rc.c = nil
}
return err return err
} }