diff --git a/os/gcache/gcache_adapter.go b/os/gcache/gcache_adapter.go index c7e9e2bcb..cb0961b01 100644 --- a/os/gcache/gcache_adapter.go +++ b/os/gcache/gcache_adapter.go @@ -11,6 +11,7 @@ import ( ) // Adapter is the adapter for cache features implements. +// TODO error returning for each function. type Adapter interface { // Set sets cache with - pair, which is expired after . // @@ -25,7 +26,9 @@ type Adapter interface { Sets(data map[interface{}]interface{}, duration time.Duration) // SetIfNotExist sets cache with - pair which is expired after - // if does not exist in the cache. + // if does not exist in the cache. It returns true the dose not exist in the + // cache and it sets successfully to the cache, or else it returns false. + // // The parameter can be type of , but it dose nothing if its // result is nil. // @@ -67,6 +70,8 @@ type Adapter interface { GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration) interface{} // GetExpire retrieves and returns the expiration of in the cache. + // + // It returns 0 if the does not expire. // It returns -1 if the does not exist in the cache. GetExpire(key interface{}) time.Duration @@ -78,17 +83,15 @@ type Adapter interface { // The returned value is false if the does not exist in the cache. // // It deletes the if given is nil. + // It does nothing if does not exist in the cache. Update(key interface{}, value interface{}) (oldValue interface{}, exist bool) // UpdateExpire updates the expiration of and returns the old expiration duration value. // - // It returns -1 if the does not exist in the cache. + // It returns -1 and does nothing if the does not exist in the cache. // It deletes the if < 0. UpdateExpire(key interface{}, duration time.Duration) (oldDuration time.Duration) - // Contains checks and returns whether given exists in the cache. - Contains(key interface{}) bool - // Size returns the number of items in the cache. Size() (size int) diff --git a/os/gcache/gcache_adapter_memory.go b/os/gcache/gcache_adapter_memory.go index 0d49aa4d6..f74165363 100644 --- a/os/gcache/gcache_adapter_memory.go +++ b/os/gcache/gcache_adapter_memory.go @@ -116,6 +116,7 @@ func (c *adapterMemory) Set(key interface{}, value interface{}, duration time.Du // The returned value is false if the does not exist in the cache. // // It deletes the if given is nil. +// It does nothing if does not exist in the cache. func (c *adapterMemory) Update(key interface{}, value interface{}) (oldValue interface{}, exist bool) { c.dataMu.Lock() defer c.dataMu.Unlock() @@ -131,7 +132,7 @@ func (c *adapterMemory) Update(key interface{}, value interface{}) (oldValue int // UpdateExpire updates the expiration of and returns the old expiration duration value. // -// It returns -1 if the does not exist in the cache. +// It returns -1 and does nothing if the does not exist in the cache. // It deletes the if < 0. func (c *adapterMemory) UpdateExpire(key interface{}, duration time.Duration) (oldDuration time.Duration) { newExpireTime := c.getInternalExpire(duration) @@ -152,6 +153,8 @@ func (c *adapterMemory) UpdateExpire(key interface{}, duration time.Duration) (o } // GetExpire retrieves and returns the expiration of in the cache. +// +// It returns 0 if the does not expire. // It returns -1 if the does not exist in the cache. func (c *adapterMemory) GetExpire(key interface{}) time.Duration { c.dataMu.RLock() @@ -227,8 +230,14 @@ func (c *adapterMemory) getOrNewExpireSet(expire int64) (expireSet *gset.Set) { return } -// SetIfNotExist sets cache with - pair if does not exist in the cache, -// which is expired after . It does not expire if == 0. +// SetIfNotExist sets cache with - pair which is expired after +// if does not exist in the cache. It returns true the dose not exist in the +// cache and it sets successfully to the cache, or else it returns false. +// The parameter can be type of , but it dose nothing if its +// result is nil. +// +// It does not expire if == 0. +// It deletes the if < 0 or given is nil. func (c *adapterMemory) SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool { if !c.Contains(key) { c.doSetWithLockCheck(key, value, duration) diff --git a/os/gcache/gcache_cache.go b/os/gcache/gcache_cache.go index 50fb2ec81..7151fd9f4 100644 --- a/os/gcache/gcache_cache.go +++ b/os/gcache/gcache_cache.go @@ -38,6 +38,11 @@ func (c *Cache) SetAdapter(adapter Adapter) { c.Adapter = adapter } +// Contains returns true if exists in the cache, or else returns false. +func (c *Cache) Contains(key interface{}) bool { + return c.Get(key) != nil +} + // GetVar retrieves and returns the value of as gvar.Var. func (c *Cache) GetVar(key interface{}) *gvar.Var { return gvar.New(c.Get(key))