Commit 63f19974 authored by astaxie's avatar astaxie

Merge pull request #460 from pengfei-xue/develop

use connection pool for redis, support auto connection
parents 6e9ba0ea cb55009c
...@@ -3,7 +3,7 @@ package cache ...@@ -3,7 +3,7 @@ package cache
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"io" "time"
"github.com/beego/redigo/redis" "github.com/beego/redigo/redis"
) )
...@@ -15,7 +15,7 @@ var ( ...@@ -15,7 +15,7 @@ var (
// Redis cache adapter. // Redis cache adapter.
type RedisCache struct { type RedisCache struct {
c redis.Conn p *redis.Pool // redis connection pool
conninfo string conninfo string
key string key string
} }
...@@ -25,23 +25,17 @@ func NewRedisCache() *RedisCache { ...@@ -25,23 +25,17 @@ func NewRedisCache() *RedisCache {
return &RedisCache{key: DefaultKey} return &RedisCache{key: DefaultKey}
} }
// Get cache from redis. // actually do the redis cmds
func (rc *RedisCache) Get(key string) interface{} { func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interface{}, err error) {
if rc.c == nil { c := rc.p.Get()
var err error defer c.Close()
rc.c, err = rc.connectInit()
if err != nil {
return nil
}
}
v, err := rc.c.Do("HGET", rc.key, key) return c.Do(commandName, args...)
// write to closed socket, reset rc.c to nil }
if err == io.EOF {
rc.c = nil
return nil
}
// Get cache from redis.
func (rc *RedisCache) Get(key string) interface{} {
v, err := rc.do("HGET", rc.key, key)
if err != nil { if err != nil {
return nil return nil
} }
...@@ -52,61 +46,19 @@ func (rc *RedisCache) Get(key string) interface{} { ...@@ -52,61 +46,19 @@ func (rc *RedisCache) Get(key string) interface{} {
// put cache to redis. // put cache to redis.
// timeout is ignored. // 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 {
if rc.c == nil { _, err := rc.do("HSET", rc.key, key, val)
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, 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
} }
// delete cache in redis. // delete cache in redis.
func (rc *RedisCache) Delete(key string) error { func (rc *RedisCache) Delete(key string) error {
if rc.c == nil { _, err := rc.do("HDEL", rc.key, key)
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, 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
} }
// check cache exist in redis. // check cache exist in redis.
func (rc *RedisCache) IsExist(key string) bool { func (rc *RedisCache) IsExist(key string) bool {
if rc.c == nil { v, err := redis.Bool(rc.do("HEXISTS", rc.key, key))
var err error
rc.c, err = rc.connectInit()
if err != nil {
return false
}
}
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
} }
...@@ -116,59 +68,19 @@ func (rc *RedisCache) IsExist(key string) bool { ...@@ -116,59 +68,19 @@ func (rc *RedisCache) IsExist(key string) bool {
// increase counter in redis. // increase counter in redis.
func (rc *RedisCache) Incr(key string) error { func (rc *RedisCache) Incr(key string) error {
if rc.c == nil { _, err := redis.Bool(rc.do("HINCRBY", rc.key, key, 1))
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1))
// write to closed socket
if err == io.EOF {
rc.c = nil
}
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 {
if rc.c == nil { _, err := redis.Bool(rc.do("HINCRBY", rc.key, key, -1))
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1))
// write to closed socket
if err == io.EOF {
rc.c = nil
}
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 {
if rc.c == nil { _, err := rc.do("DEL", rc.key)
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, err := rc.c.Do("DEL", rc.key)
// write to closed socket
if err == io.EOF {
rc.c = nil
}
return err return err
} }
...@@ -179,32 +91,42 @@ func (rc *RedisCache) ClearAll() error { ...@@ -179,32 +91,42 @@ func (rc *RedisCache) ClearAll() error {
func (rc *RedisCache) StartAndGC(config string) error { func (rc *RedisCache) StartAndGC(config string) error {
var cf map[string]string var cf map[string]string
json.Unmarshal([]byte(config), &cf) json.Unmarshal([]byte(config), &cf)
if _, ok := cf["key"]; !ok { if _, ok := cf["key"]; !ok {
cf["key"] = DefaultKey cf["key"] = DefaultKey
} }
if _, ok := cf["conn"]; !ok { if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key") return errors.New("config has no conn key")
} }
rc.key = cf["key"] rc.key = cf["key"]
rc.conninfo = cf["conn"] rc.conninfo = cf["conn"]
var err error rc.connectInit()
rc.c, err = rc.connectInit()
if err != nil { c := rc.p.Get()
defer c.Close()
if err := c.Err(); err != nil {
return err return err
} }
if rc.c == nil {
return errors.New("dial tcp conn error")
}
return nil return nil
} }
// connect to redis. // connect to redis.
func (rc *RedisCache) connectInit() (redis.Conn, error) { func (rc *RedisCache) connectInit() {
// initialize a new pool
rc.p = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 180 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", rc.conninfo) c, err := redis.Dial("tcp", rc.conninfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return c, nil return c, nil
},
}
} }
func init() { func init() {
......
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