fix #91
Showing
5 changed files
with
111 additions
and
0 deletions
| ... | @@ -8,6 +8,8 @@ type Cache interface { | ... | @@ -8,6 +8,8 @@ type Cache interface { |
| 8 | Get(key string) interface{} | 8 | Get(key string) interface{} |
| 9 | Put(key string, val interface{}, timeout int64) error | 9 | Put(key string, val interface{}, timeout int64) error |
| 10 | Delete(key string) error | 10 | Delete(key string) error |
| 11 | Incr(key string) error | ||
| 12 | Decr(key string) error | ||
| 11 | IsExist(key string) bool | 13 | IsExist(key string) bool |
| 12 | ClearAll() error | 14 | ClearAll() error |
| 13 | StartAndGC(config string) error | 15 | StartAndGC(config string) error | ... | ... |
| ... | @@ -31,6 +31,21 @@ func Test_cache(t *testing.T) { | ... | @@ -31,6 +31,21 @@ func Test_cache(t *testing.T) { |
| 31 | t.Error("set Error", err) | 31 | t.Error("set Error", err) |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | if err = bm.Incr("astaxie"); err != nil { | ||
| 35 | t.Error("Incr Error", err) | ||
| 36 | } | ||
| 37 | |||
| 38 | if v := bm.Get("astaxie"); v.(int) != 2 { | ||
| 39 | t.Error("get err") | ||
| 40 | } | ||
| 41 | |||
| 42 | if err = bm.Decr("astaxie"); err != nil { | ||
| 43 | t.Error("Incr Error", err) | ||
| 44 | } | ||
| 45 | |||
| 46 | if v := bm.Get("astaxie"); v.(int) != 1 { | ||
| 47 | t.Error("get err") | ||
| 48 | } | ||
| 34 | bm.Delete("astaxie") | 49 | bm.Delete("astaxie") |
| 35 | if bm.IsExist("astaxie") { | 50 | if bm.IsExist("astaxie") { |
| 36 | t.Error("delete err") | 51 | t.Error("delete err") | ... | ... |
| ... | @@ -51,6 +51,14 @@ func (rc *MemcacheCache) Delete(key string) error { | ... | @@ -51,6 +51,14 @@ func (rc *MemcacheCache) Delete(key string) error { |
| 51 | return err | 51 | return err |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | func (rc *MemcacheCache) Incr(key string) error { | ||
| 55 | return errors.New("not support in memcache") | ||
| 56 | } | ||
| 57 | |||
| 58 | func (rc *MemcacheCache) Decr(key string) error { | ||
| 59 | return errors.New("not support in memcache") | ||
| 60 | } | ||
| 61 | |||
| 54 | func (rc *MemcacheCache) IsExist(key string) bool { | 62 | func (rc *MemcacheCache) IsExist(key string) bool { |
| 55 | if rc.c == nil { | 63 | if rc.c == nil { |
| 56 | rc.c = rc.connectInit() | 64 | rc.c = rc.connectInit() | ... | ... |
| ... | @@ -75,6 +75,70 @@ func (bc *MemoryCache) Delete(name string) error { | ... | @@ -75,6 +75,70 @@ func (bc *MemoryCache) Delete(name string) error { |
| 75 | return nil | 75 | return nil |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | func (bc *MemoryCache) Incr(key string) error { | ||
| 79 | bc.lock.RLock() | ||
| 80 | defer bc.lock.RUnlock() | ||
| 81 | itm, ok := bc.items[key] | ||
| 82 | if !ok { | ||
| 83 | return errors.New("key not exist") | ||
| 84 | } | ||
| 85 | switch itm.val.(type) { | ||
| 86 | case int: | ||
| 87 | itm.val = itm.val.(int) + 1 | ||
| 88 | case int64: | ||
| 89 | itm.val = itm.val.(int64) + 1 | ||
| 90 | case int32: | ||
| 91 | itm.val = itm.val.(int32) + 1 | ||
| 92 | case uint: | ||
| 93 | itm.val = itm.val.(uint) + 1 | ||
| 94 | case uint32: | ||
| 95 | itm.val = itm.val.(uint32) + 1 | ||
| 96 | case uint64: | ||
| 97 | itm.val = itm.val.(uint64) + 1 | ||
| 98 | default: | ||
| 99 | return errors.New("item val is not int int64 int32") | ||
| 100 | } | ||
| 101 | return nil | ||
| 102 | } | ||
| 103 | |||
| 104 | func (bc *MemoryCache) Decr(key string) error { | ||
| 105 | bc.lock.RLock() | ||
| 106 | defer bc.lock.RUnlock() | ||
| 107 | itm, ok := bc.items[key] | ||
| 108 | if !ok { | ||
| 109 | return errors.New("key not exist") | ||
| 110 | } | ||
| 111 | switch itm.val.(type) { | ||
| 112 | case int: | ||
| 113 | itm.val = itm.val.(int) - 1 | ||
| 114 | case int64: | ||
| 115 | itm.val = itm.val.(int64) - 1 | ||
| 116 | case int32: | ||
| 117 | itm.val = itm.val.(int32) - 1 | ||
| 118 | case uint: | ||
| 119 | if itm.val.(uint) > 0 { | ||
| 120 | itm.val = itm.val.(uint) - 1 | ||
| 121 | } else { | ||
| 122 | return errors.New("item val is less than 0") | ||
| 123 | } | ||
| 124 | case uint32: | ||
| 125 | if itm.val.(uint32) > 0 { | ||
| 126 | itm.val = itm.val.(uint32) - 1 | ||
| 127 | } else { | ||
| 128 | return errors.New("item val is less than 0") | ||
| 129 | } | ||
| 130 | case uint64: | ||
| 131 | if itm.val.(uint64) > 0 { | ||
| 132 | itm.val = itm.val.(uint64) - 1 | ||
| 133 | } else { | ||
| 134 | return errors.New("item val is less than 0") | ||
| 135 | } | ||
| 136 | default: | ||
| 137 | return errors.New("item val is not int int64 int32") | ||
| 138 | } | ||
| 139 | return nil | ||
| 140 | } | ||
| 141 | |||
| 78 | func (bc *MemoryCache) IsExist(name string) bool { | 142 | func (bc *MemoryCache) IsExist(name string) bool { |
| 79 | bc.lock.RLock() | 143 | bc.lock.RLock() |
| 80 | defer bc.lock.RUnlock() | 144 | defer bc.lock.RUnlock() | ... | ... |
| ... | @@ -58,6 +58,28 @@ func (rc *RedisCache) IsExist(key string) bool { | ... | @@ -58,6 +58,28 @@ func (rc *RedisCache) IsExist(key string) bool { |
| 58 | return v | 58 | return v |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | func (rc *RedisCache) Incr(key string) error { | ||
| 62 | if rc.c == nil { | ||
| 63 | rc.c = rc.connectInit() | ||
| 64 | } | ||
| 65 | _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1)) | ||
| 66 | if err != nil { | ||
| 67 | return err | ||
| 68 | } | ||
| 69 | return nil | ||
| 70 | } | ||
| 71 | |||
| 72 | func (rc *RedisCache) Decr(key string) error { | ||
| 73 | if rc.c == nil { | ||
| 74 | rc.c = rc.connectInit() | ||
| 75 | } | ||
| 76 | _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1)) | ||
| 77 | if err != nil { | ||
| 78 | return err | ||
| 79 | } | ||
| 80 | return nil | ||
| 81 | } | ||
| 82 | |||
| 61 | func (rc *RedisCache) ClearAll() error { | 83 | func (rc *RedisCache) ClearAll() error { |
| 62 | if rc.c == nil { | 84 | if rc.c == nil { |
| 63 | rc.c = rc.connectInit() | 85 | rc.c = rc.connectInit() | ... | ... |
-
Please register or sign in to post a comment