1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 06:40:55 +00:00
This commit is contained in:
astaxie 2013-07-16 19:05:44 +08:00
parent 60200689f4
commit 7f4ad7ff46
5 changed files with 111 additions and 0 deletions

2
cache/cache.go vendored
View File

@ -8,6 +8,8 @@ type Cache interface {
Get(key string) interface{} Get(key string) interface{}
Put(key string, val interface{}, timeout int64) error Put(key string, val interface{}, timeout int64) error
Delete(key string) error Delete(key string) error
Incr(key string) error
Decr(key string) error
IsExist(key string) bool IsExist(key string) bool
ClearAll() error ClearAll() error
StartAndGC(config string) error StartAndGC(config string) error

15
cache/cache_test.go vendored
View File

@ -31,6 +31,21 @@ func Test_cache(t *testing.T) {
t.Error("set Error", err) t.Error("set Error", err)
} }
if err = bm.Incr("astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v := bm.Get("astaxie"); v.(int) != 2 {
t.Error("get err")
}
if err = bm.Decr("astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
}
bm.Delete("astaxie") bm.Delete("astaxie")
if bm.IsExist("astaxie") { if bm.IsExist("astaxie") {
t.Error("delete err") t.Error("delete err")

8
cache/memcache.go vendored
View File

@ -51,6 +51,14 @@ func (rc *MemcacheCache) Delete(key string) error {
return err return err
} }
func (rc *MemcacheCache) Incr(key string) error {
return errors.New("not support in memcache")
}
func (rc *MemcacheCache) Decr(key string) error {
return errors.New("not support 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() rc.c = rc.connectInit()

64
cache/memory.go vendored
View File

@ -75,6 +75,70 @@ func (bc *MemoryCache) Delete(name string) error {
return nil return nil
} }
func (bc *MemoryCache) Incr(key string) error {
bc.lock.RLock()
defer bc.lock.RUnlock()
itm, ok := bc.items[key]
if !ok {
return errors.New("key not exist")
}
switch itm.val.(type) {
case int:
itm.val = itm.val.(int) + 1
case int64:
itm.val = itm.val.(int64) + 1
case int32:
itm.val = itm.val.(int32) + 1
case uint:
itm.val = itm.val.(uint) + 1
case uint32:
itm.val = itm.val.(uint32) + 1
case uint64:
itm.val = itm.val.(uint64) + 1
default:
return errors.New("item val is not int int64 int32")
}
return nil
}
func (bc *MemoryCache) Decr(key string) error {
bc.lock.RLock()
defer bc.lock.RUnlock()
itm, ok := bc.items[key]
if !ok {
return errors.New("key not exist")
}
switch itm.val.(type) {
case int:
itm.val = itm.val.(int) - 1
case int64:
itm.val = itm.val.(int64) - 1
case int32:
itm.val = itm.val.(int32) - 1
case uint:
if itm.val.(uint) > 0 {
itm.val = itm.val.(uint) - 1
} else {
return errors.New("item val is less than 0")
}
case uint32:
if itm.val.(uint32) > 0 {
itm.val = itm.val.(uint32) - 1
} else {
return errors.New("item val is less than 0")
}
case uint64:
if itm.val.(uint64) > 0 {
itm.val = itm.val.(uint64) - 1
} else {
return errors.New("item val is less than 0")
}
default:
return errors.New("item val is not int int64 int32")
}
return nil
}
func (bc *MemoryCache) IsExist(name string) bool { func (bc *MemoryCache) IsExist(name string) bool {
bc.lock.RLock() bc.lock.RLock()
defer bc.lock.RUnlock() defer bc.lock.RUnlock()

22
cache/redis.go vendored
View File

@ -58,6 +58,28 @@ func (rc *RedisCache) IsExist(key string) bool {
return v return v
} }
func (rc *RedisCache) Incr(key string) error {
if rc.c == nil {
rc.c = rc.connectInit()
}
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1))
if err != nil {
return err
}
return nil
}
func (rc *RedisCache) Decr(key string) error {
if rc.c == nil {
rc.c = rc.connectInit()
}
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1))
if err != nil {
return err
}
return nil
}
func (rc *RedisCache) ClearAll() error { func (rc *RedisCache) ClearAll() error {
if rc.c == nil { if rc.c == nil {
rc.c = rc.connectInit() rc.c = rc.connectInit()