diff --git a/cache/cache.go b/cache/cache.go index fd805ab9..82585c4e 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -59,10 +59,6 @@ type Cache interface { Incr(key string) error // decrease cached int value by key, as a counter. Decr(key string) error - // increase cached with int value by key, as a counter. - IncrBy(key string, num int) error - // decrease cached with int value by key, as a counter. - DecrBy(key string, num int) error // check if cached value exists or not. IsExist(key string) bool // clear all cache. diff --git a/cache/cache_test.go b/cache/cache_test.go index 5e6546e2..9ceb606a 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -37,7 +37,7 @@ func TestCache(t *testing.T) { t.Error("get err") } - time.Sleep(10 * time.Second) + time.Sleep(30 * time.Second) if bm.IsExist("astaxie") { t.Error("check err") @@ -55,22 +55,6 @@ func TestCache(t *testing.T) { t.Error("get err") } - if err = bm.IncrBy("astaxie", 2); err != nil { - t.Error("Incr Error", err) - } - - if v := bm.Get("astaxie"); v.(int) != 4 { - t.Error("get err") - } - - if err = bm.DecrBy("astaxie", 2); err != nil { - t.Error("Decr Error", err) - } - - if v := bm.Get("astaxie"); v.(int) != 2 { - t.Error("get err") - } - if err = bm.Decr("astaxie"); err != nil { t.Error("Decr Error", err) } @@ -78,7 +62,6 @@ func TestCache(t *testing.T) { if v := bm.Get("astaxie"); v.(int) != 1 { t.Error("get err") } - bm.Delete("astaxie") if bm.IsExist("astaxie") { t.Error("delete err") @@ -139,22 +122,6 @@ func TestFileCache(t *testing.T) { t.Error("get err") } - if err = bm.IncrBy("astaxie", 2); err != nil { - t.Error("Incr Error", err) - } - - if v := bm.Get("astaxie"); v.(int) != 4 { - t.Error("get err") - } - - if err = bm.DecrBy("astaxie", 2); err != nil { - t.Error("Decr Error", err) - } - - if v := bm.Get("astaxie"); v.(int) != 2 { - t.Error("get err") - } - if err = bm.Decr("astaxie"); err != nil { t.Error("Decr Error", err) } @@ -162,7 +129,6 @@ func TestFileCache(t *testing.T) { if v := bm.Get("astaxie"); v.(int) != 1 { t.Error("get err") } - bm.Delete("astaxie") if bm.IsExist("astaxie") { t.Error("delete err") diff --git a/cache/file.go b/cache/file.go index 99d92212..691ce7cd 100644 --- a/cache/file.go +++ b/cache/file.go @@ -20,7 +20,6 @@ import ( "encoding/gob" "encoding/hex" "encoding/json" - "errors" "fmt" "io" "io/ioutil" @@ -184,24 +183,6 @@ func (fc *FileCache) Incr(key string) error { return nil } -// IncrBy will increase cached int value by num. -// fc value is saving forever unless Delete. -func (fc *FileCache) IncrBy(key string, num int) error { - if num < 1 { - return errors.New("increase num should be a positive number") - } - - data := fc.Get(key) - var incr int - if reflect.TypeOf(data).Name() != "int" { - incr = 0 - } else { - incr = data.(int) + int(num) - } - fc.Put(key, incr, FileCacheEmbedExpiry) - return nil -} - // Decr will decrease cached int value. func (fc *FileCache) Decr(key string) error { data := fc.Get(key) @@ -215,23 +196,6 @@ func (fc *FileCache) Decr(key string) error { return nil } -// DecrBy will decrease cached int value. -func (fc *FileCache) DecrBy(key string, num int) error { - if num < 1 { - return errors.New("decrease num should be a positive number") - } - - data := fc.Get(key) - var decr int - if reflect.TypeOf(data).Name() != "int" || data.(int)-1 <= 0 { - decr = 0 - } else { - decr = data.(int) - int(num) - } - fc.Put(key, decr, FileCacheEmbedExpiry) - return nil -} - // IsExist check value is exist. func (fc *FileCache) IsExist(key string) bool { ret, _ := exists(fc.getCacheFileName(key)) diff --git a/cache/memcache/memcache.go b/cache/memcache/memcache.go index 97822f1c..0624f5fa 100644 --- a/cache/memcache/memcache.go +++ b/cache/memcache/memcache.go @@ -127,21 +127,6 @@ func (rc *Cache) Incr(key string) error { return err } -// IncrBy increase counter by num. -func (rc *Cache) IncrBy(key string, num int) error { - if num < 1 { - return errors.New("increase num should be a positive number") - } - - if rc.conn == nil { - if err := rc.connectInit(); err != nil { - return err - } - } - _, err := rc.conn.Increment(key, uint64(num)) - return err -} - // Decr decrease counter. func (rc *Cache) Decr(key string) error { if rc.conn == nil { @@ -153,21 +138,6 @@ func (rc *Cache) Decr(key string) error { return err } -// DecrBy decrease counter by num. -func (rc *Cache) DecrBy(key string, num int) error { - if num < 1 { - return errors.New("decrease num should be a positive number") - } - - if rc.conn == nil { - if err := rc.connectInit(); err != nil { - return err - } - } - _, err := rc.conn.Decrement(key, uint64(num)) - return err -} - // IsExist check value exists in memcache. func (rc *Cache) IsExist(key string) bool { if rc.conn == nil { diff --git a/cache/memcache/memcache_test.go b/cache/memcache/memcache_test.go index 0a6ee934..d9129b69 100644 --- a/cache/memcache/memcache_test.go +++ b/cache/memcache/memcache_test.go @@ -58,22 +58,6 @@ func TestMemcacheCache(t *testing.T) { t.Error("get err") } - if err = bm.IncrBy("astaxie", 2); err != nil { - t.Error("Incr Error", err) - } - - if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 4 { - t.Error("get err") - } - - if err = bm.DecrBy("astaxie", 2); err != nil { - t.Error("Decr Error", err) - } - - if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 2 { - t.Error("get err") - } - if err = bm.Decr("astaxie"); err != nil { t.Error("Decr Error", err) } @@ -81,7 +65,6 @@ func TestMemcacheCache(t *testing.T) { if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 { t.Error("get err") } - bm.Delete("astaxie") if bm.IsExist("astaxie") { t.Error("delete err") diff --git a/cache/memory.go b/cache/memory.go index 1b894a81..57e868cf 100644 --- a/cache/memory.go +++ b/cache/memory.go @@ -135,39 +135,6 @@ func (bc *MemoryCache) Incr(key string) error { return nil } -// IncrBy increase cache counter in memory by num. -// it supports int,int32,int64,uint,uint32,uint64. -func (bc *MemoryCache) IncrBy(key string, num int) error { - bc.RLock() - defer bc.RUnlock() - itm, ok := bc.items[key] - if !ok { - return errors.New("key not exist") - } - - if num < 1 { - return errors.New("increase num should be a positive number") - } - - switch itm.val.(type) { - case int: - itm.val = itm.val.(int) + num - case int32: - itm.val = itm.val.(int32) + int32(num) - case int64: - itm.val = itm.val.(int64) + int64(num) - case uint: - itm.val = itm.val.(uint) + uint(num) - case uint32: - itm.val = itm.val.(uint32) + uint32(num) - case uint64: - itm.val = itm.val.(uint64) + uint64(num) - default: - return errors.New("item val is not (u)int (u)int32 (u)int64") - } - return nil -} - // Decr decrease counter in memory. func (bc *MemoryCache) Decr(key string) error { bc.RLock() @@ -207,50 +174,6 @@ func (bc *MemoryCache) Decr(key string) error { return nil } -// DecrBy decrease counter in memory by num. -func (bc *MemoryCache) DecrBy(key string, num int) error { - bc.RLock() - defer bc.RUnlock() - itm, ok := bc.items[key] - if !ok { - return errors.New("key not exist") - } - - if num < 1 { - return errors.New("decrease num should be a positive number") - } - - switch itm.val.(type) { - case int: - itm.val = itm.val.(int) - int(num) - case int64: - itm.val = itm.val.(int64) - int64(num) - case int32: - itm.val = itm.val.(int32) - int32(num) - case uint: - if itm.val.(uint) > 0 { - itm.val = itm.val.(uint) - uint(num) - } else { - return errors.New("item val is less than 0") - } - case uint32: - if itm.val.(uint32) > 0 { - itm.val = itm.val.(uint32) - uint32(num) - } else { - return errors.New("item val is less than 0") - } - case uint64: - if itm.val.(uint64) > 0 { - itm.val = itm.val.(uint64) - uint64(num) - } else { - return errors.New("item val is less than 0") - } - default: - return errors.New("item val is not int int64 int32") - } - return nil -} - // IsExist check cache exist in memory. func (bc *MemoryCache) IsExist(name string) bool { bc.RLock() diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 72af1d68..55fea25e 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -128,30 +128,12 @@ 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() diff --git a/cache/redis/redis_test.go b/cache/redis/redis_test.go index 3979014b..56877f6b 100644 --- a/cache/redis/redis_test.go +++ b/cache/redis/redis_test.go @@ -56,22 +56,6 @@ 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) } @@ -79,7 +63,6 @@ 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") diff --git a/cache/ssdb/ssdb.go b/cache/ssdb/ssdb.go index 7b57e45d..fa2ce04b 100644 --- a/cache/ssdb/ssdb.go +++ b/cache/ssdb/ssdb.go @@ -124,20 +124,6 @@ func (rc *Cache) Incr(key string) error { return err } -// IncrBy increase counter by num. -func (rc *Cache) IncrBy(key string, num int) error { - if num < 1 { - return errors.New("increase num should be a positive number") - } - if rc.conn == nil { - if err := rc.connectInit(); err != nil { - return err - } - } - _, err := rc.conn.Do("incr", key, num) - return err -} - // Decr decrease counter. func (rc *Cache) Decr(key string) error { if rc.conn == nil { @@ -149,21 +135,6 @@ func (rc *Cache) Decr(key string) error { return err } -// DecrBy decrease counter by num. -func (rc *Cache) DecrBy(key string, num int) error { - if num < 1 { - return errors.New("decrease num should be a positive number") - } - - if rc.conn == nil { - if err := rc.connectInit(); err != nil { - return err - } - } - _, err := rc.conn.Do("incr", key, -num) - return err -} - // IsExist check value exists in memcache. func (rc *Cache) IsExist(key string) bool { if rc.conn == nil { diff --git a/cache/ssdb/ssdb_test.go b/cache/ssdb/ssdb_test.go index 96102a56..dd474960 100644 --- a/cache/ssdb/ssdb_test.go +++ b/cache/ssdb/ssdb_test.go @@ -40,7 +40,6 @@ func TestSsdbcacheCache(t *testing.T) { if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil { t.Error("set Error", err) } - if err = ssdb.Incr("ssdb"); err != nil { t.Error("incr Error", err) } @@ -49,30 +48,10 @@ func TestSsdbcacheCache(t *testing.T) { t.Error("get err") } - if err = ssdb.IncrBy("ssdb", 2); err != nil { - t.Error("incr Error", err) - } - - if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 5 { - t.Error("get err") - } - - if err = ssdb.DecrBy("ssdb", 2); err != nil { - t.Error("decr error") - } - - if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 { - t.Error("get err") - } - if err = ssdb.Decr("ssdb"); err != nil { t.Error("decr error") } - if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 2 { - t.Error("get err") - } - // test del if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil { t.Error("set Error", err)