use english readme as default, because of github ranking (#427)
This commit is contained in:
19
core/stores/cache/cachenode.go
vendored
19
core/stores/cache/cachenode.go
vendored
@@ -37,6 +37,12 @@ type cacheNode struct {
|
||||
errNotFound error
|
||||
}
|
||||
|
||||
// NewCacheNode returns a cacheNode.
|
||||
// rds is the underlying redis node or cluster.
|
||||
// barrier is the barrier that maybe shared with other cache nodes on cache cluster.
|
||||
// st is used to stat the cache.
|
||||
// errNotFound defines the error that returned on cache not found.
|
||||
// opts are the options that customize the cacheNode.
|
||||
func NewCacheNode(rds *redis.Redis, barrier syncx.SharedCalls, st *CacheStat,
|
||||
errNotFound error, opts ...Option) Cache {
|
||||
o := newOptions(opts...)
|
||||
@@ -53,6 +59,7 @@ func NewCacheNode(rds *redis.Redis, barrier syncx.SharedCalls, st *CacheStat,
|
||||
}
|
||||
}
|
||||
|
||||
// DelCache deletes cached values with keys.
|
||||
func (c cacheNode) DelCache(keys ...string) error {
|
||||
if len(keys) == 0 {
|
||||
return nil
|
||||
@@ -66,6 +73,7 @@ func (c cacheNode) DelCache(keys ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCache gets the cache with key and fills into v.
|
||||
func (c cacheNode) GetCache(key string, v interface{}) error {
|
||||
if err := c.doGetCache(key, v); err == errPlaceholder {
|
||||
return c.errNotFound
|
||||
@@ -74,10 +82,12 @@ func (c cacheNode) GetCache(key string, v interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
// SetCache sets the cache with key and v, using c.expiry.
|
||||
func (c cacheNode) SetCache(key string, v interface{}) error {
|
||||
return c.SetCacheWithExpire(key, v, c.aroundDuration(c.expiry))
|
||||
}
|
||||
|
||||
// SetCacheWithExpire sets the cache with key and v, using given expire.
|
||||
func (c cacheNode) SetCacheWithExpire(key string, v interface{}, expire time.Duration) error {
|
||||
data, err := jsonx.Marshal(v)
|
||||
if err != nil {
|
||||
@@ -87,18 +97,23 @@ func (c cacheNode) SetCacheWithExpire(key string, v interface{}, expire time.Dur
|
||||
return c.rds.Setex(key, string(data), int(expire.Seconds()))
|
||||
}
|
||||
|
||||
// String returns a string that represents the cacheNode.
|
||||
func (c cacheNode) String() string {
|
||||
return c.rds.Addr
|
||||
}
|
||||
|
||||
// TakeWithExpire takes the result from cache first, if not found,
|
||||
// query from DB and set cache using c.expiry, then return the result.
|
||||
func (c cacheNode) Take(v interface{}, key string, query func(v interface{}) error) error {
|
||||
return c.doTake(v, key, query, func(v interface{}) error {
|
||||
return c.SetCache(key, v)
|
||||
})
|
||||
}
|
||||
|
||||
func (c cacheNode) TakeWithExpire(v interface{}, key string,
|
||||
query func(v interface{}, expire time.Duration) error) error {
|
||||
// TakeWithExpire takes the result from cache first, if not found,
|
||||
// query from DB and set cache using given expire, then return the result.
|
||||
func (c cacheNode) TakeWithExpire(v interface{}, key string, query func(v interface{},
|
||||
expire time.Duration) error) error {
|
||||
expire := c.aroundDuration(c.expiry)
|
||||
return c.doTake(v, key, func(v interface{}) error {
|
||||
return query(v, expire)
|
||||
|
||||
Reference in New Issue
Block a user