mirror of
https://github.com/astaxie/beego.git
synced 2025-06-11 20:30:39 +00:00
hible
cache add function // IncrBy increase counter by num. IncrBy(key string, num int) // DecrBy decrease counter by num. DecrBy(key string, num int)
This commit is contained in:
18
cache/redis/redis.go
vendored
18
cache/redis/redis.go
vendored
@ -128,12 +128,30 @@ func (rc *Cache) Incr(key string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// IncrBy increase counter in redis by num.
|
||||
func (rc *Cache) IncrBy(key string, num int) error {
|
||||
if num < 1 {
|
||||
return errors.New("increase num should be a positive number")
|
||||
}
|
||||
_, err := redis.Bool(rc.do("INCRBY", key, num))
|
||||
return err
|
||||
}
|
||||
|
||||
// Decr decrease counter in redis.
|
||||
func (rc *Cache) Decr(key string) error {
|
||||
_, err := redis.Bool(rc.do("INCRBY", key, -1))
|
||||
return err
|
||||
}
|
||||
|
||||
// DecrBy decrease counter in redis by num.
|
||||
func (rc *Cache) DecrBy(key string, num int) error {
|
||||
if num < 1 {
|
||||
return errors.New("decrease num should be a positive number")
|
||||
}
|
||||
_, err := redis.Bool(rc.do("DECRBY", key, num))
|
||||
return err
|
||||
}
|
||||
|
||||
// ClearAll clean all cache in redis. delete this redis collection.
|
||||
func (rc *Cache) ClearAll() error {
|
||||
c := rc.p.Get()
|
||||
|
17
cache/redis/redis_test.go
vendored
17
cache/redis/redis_test.go
vendored
@ -56,6 +56,22 @@ func TestRedisCache(t *testing.T) {
|
||||
t.Error("get err")
|
||||
}
|
||||
|
||||
if err = bm.IncrBy("astaxie", 2); err != nil {
|
||||
t.Error("Incr Error", err)
|
||||
}
|
||||
|
||||
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 4 {
|
||||
t.Error("get err")
|
||||
}
|
||||
|
||||
if err = bm.DecrBy("astaxie", 2); err != nil {
|
||||
t.Error("Decr 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)
|
||||
}
|
||||
@ -63,6 +79,7 @@ func TestRedisCache(t *testing.T) {
|
||||
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")
|
||||
|
Reference in New Issue
Block a user