improve adapter feature for package gcache

This commit is contained in:
John
2020-09-26 22:44:07 +08:00
parent bae8f6315b
commit aeb9b68298
3 changed files with 25 additions and 8 deletions

View File

@ -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 <key>-<value> pair, which is expired after <duration>.
//
@ -25,7 +26,9 @@ type Adapter interface {
Sets(data map[interface{}]interface{}, duration time.Duration)
// SetIfNotExist sets cache with <key>-<value> pair which is expired after <duration>
// if <key> does not exist in the cache.
// if <key> does not exist in the cache. It returns true the <key> dose not exist in the
// cache and it sets <value> successfully to the cache, or else it returns false.
//
// The parameter <value> can be type of <func() interface{}>, 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 <key> in the cache.
//
// It returns 0 if the <key> does not expire.
// It returns -1 if the <key> does not exist in the cache.
GetExpire(key interface{}) time.Duration
@ -78,17 +83,15 @@ type Adapter interface {
// The returned value <exist> is false if the <key> does not exist in the cache.
//
// It deletes the <key> if given <value> is nil.
// It does nothing if <key> does not exist in the cache.
Update(key interface{}, value interface{}) (oldValue interface{}, exist bool)
// UpdateExpire updates the expiration of <key> and returns the old expiration duration value.
//
// It returns -1 if the <key> does not exist in the cache.
// It returns -1 and does nothing if the <key> does not exist in the cache.
// It deletes the <key> if <duration> < 0.
UpdateExpire(key interface{}, duration time.Duration) (oldDuration time.Duration)
// Contains checks and returns whether given <key> exists in the cache.
Contains(key interface{}) bool
// Size returns the number of items in the cache.
Size() (size int)

View File

@ -116,6 +116,7 @@ func (c *adapterMemory) Set(key interface{}, value interface{}, duration time.Du
// The returned value <exist> is false if the <key> does not exist in the cache.
//
// It deletes the <key> if given <value> is nil.
// It does nothing if <key> 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 <key> and returns the old expiration duration value.
//
// It returns -1 if the <key> does not exist in the cache.
// It returns -1 and does nothing if the <key> does not exist in the cache.
// It deletes the <key> if <duration> < 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 <key> in the cache.
//
// It returns 0 if the <key> does not expire.
// It returns -1 if the <key> 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 <key>-<value> pair if <key> does not exist in the cache,
// which is expired after <duration>. It does not expire if <duration> == 0.
// SetIfNotExist sets cache with <key>-<value> pair which is expired after <duration>
// if <key> does not exist in the cache. It returns true the <key> dose not exist in the
// cache and it sets <value> successfully to the cache, or else it returns false.
// The parameter <value> can be type of <func() interface{}>, but it dose nothing if its
// result is nil.
//
// It does not expire if <duration> == 0.
// It deletes the <key> if <duration> < 0 or given <value> is nil.
func (c *adapterMemory) SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool {
if !c.Contains(key) {
c.doSetWithLockCheck(key, value, duration)

View File

@ -38,6 +38,11 @@ func (c *Cache) SetAdapter(adapter Adapter) {
c.Adapter = adapter
}
// Contains returns true if <key> 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 <key> as gvar.Var.
func (c *Cache) GetVar(key interface{}) *gvar.Var {
return gvar.New(c.Get(key))