mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 14:10:54 +00:00
Make redis cache timeout not trivial.
- Instead of using HASH for all the caches, use HASH + normal KEYs. - HASH being used as a collection of all KEYs, this is useful when application wants to clear all keys.
This commit is contained in:
parent
cbffcaa7a8
commit
2933d1fedd
8
cache/cache.go
vendored
8
cache/cache.go
vendored
@ -35,11 +35,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,13 +58,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 {
|
||||||
|
43
cache/redis/redis.go
vendored
43
cache/redis/redis.go
vendored
@ -47,7 +47,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
|
||||||
}
|
}
|
||||||
@ -56,43 +56,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("HSET", rc.key, key, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, err = rc.do("SET", key, val)
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
81
cache/redis/redis_test.go
vendored
Normal file
81
cache/redis/redis_test.go
vendored
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
// 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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user