feat: support ctx in Cache (#1518)

* feature: support ctx in `Cache`

Signed-off-by: chenquan <chenquan.dev@foxmail.com>

* fix: `errors.Is` instead of `=`

Signed-off-by: chenquan <chenquan.dev@foxmail.com>
This commit is contained in:
chenquan
2022-02-13 03:28:14 -06:00
committed by GitHub
parent 84ddc660c4
commit e8c307e4dc
3 changed files with 169 additions and 36 deletions

View File

@@ -1,6 +1,8 @@
package cache
import (
"context"
"errors"
"fmt"
"log"
"time"
@@ -13,13 +15,36 @@ import (
type (
// Cache interface is used to define the cache implementation.
Cache interface {
// Del deletes cached values with keys.
Del(keys ...string) error
// DelCtx deletes cached values with keys.
DelCtx(ctx context.Context, keys ...string) error
// Get gets the cache with key and fills into v.
Get(key string, v interface{}) error
// GetCtx gets the cache with key and fills into v.
GetCtx(ctx context.Context, key string, v interface{}) error
// IsNotFound checks if the given error is the defined errNotFound.
IsNotFound(err error) bool
// Set sets the cache with key and v, using c.expiry.
Set(key string, v interface{}) error
// SetCtx sets the cache with key and v, using c.expiry.
SetCtx(ctx context.Context, key string, v interface{}) error
// SetWithExpire sets the cache with key and v, using given expire.
SetWithExpire(key string, v interface{}, expire time.Duration) error
// SetWithExpireCtx sets the cache with key and v, using given expire.
SetWithExpireCtx(ctx context.Context, key string, v interface{}, expire time.Duration) error
// Take takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
Take(v interface{}, key string, query func(v interface{}) error) error
// TakeCtx takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
TakeCtx(ctx context.Context, v interface{}, key string, query func(v interface{}) 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.
TakeWithExpire(v interface{}, key string, query func(v interface{}, expire time.Duration) error) error
// TakeWithExpireCtx takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
TakeWithExpireCtx(ctx context.Context, v interface{}, key string, query func(v interface{}, expire time.Duration) error) error
}
cacheCluster struct {
@@ -51,7 +76,13 @@ func New(c ClusterConf, barrier syncx.SingleFlight, st *Stat, errNotFound error,
}
}
// Del deletes cached values with keys.
func (cc cacheCluster) Del(keys ...string) error {
return cc.DelCtx(context.Background(), keys...)
}
// DelCtx deletes cached values with keys.
func (cc cacheCluster) DelCtx(ctx context.Context, keys ...string) error {
switch len(keys) {
case 0:
return nil
@@ -62,7 +93,7 @@ func (cc cacheCluster) Del(keys ...string) error {
return cc.errNotFound
}
return c.(Cache).Del(key)
return c.(Cache).DelCtx(ctx, key)
default:
var be errorx.BatchError
nodes := make(map[interface{}][]string)
@@ -76,7 +107,7 @@ func (cc cacheCluster) Del(keys ...string) error {
nodes[c] = append(nodes[c], key)
}
for c, ks := range nodes {
if err := c.(Cache).Del(ks...); err != nil {
if err := c.(Cache).DelCtx(ctx, ks...); err != nil {
be.Add(err)
}
}
@@ -85,52 +116,87 @@ func (cc cacheCluster) Del(keys ...string) error {
}
}
// Get gets the cache with key and fills into v.
func (cc cacheCluster) Get(key string, v interface{}) error {
return cc.GetCtx(context.Background(), key, v)
}
// GetCtx gets the cache with key and fills into v.
func (cc cacheCluster) GetCtx(ctx context.Context, key string, v interface{}) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).Get(key, v)
return c.(Cache).GetCtx(ctx, key, v)
}
// IsNotFound checks if the given error is the defined errNotFound.
func (cc cacheCluster) IsNotFound(err error) bool {
return err == cc.errNotFound
return errors.Is(err, cc.errNotFound)
}
// Set sets the cache with key and v, using c.expiry.
func (cc cacheCluster) Set(key string, v interface{}) error {
return cc.SetCtx(context.Background(), key, v)
}
// SetCtx sets the cache with key and v, using c.expiry.
func (cc cacheCluster) SetCtx(ctx context.Context, key string, v interface{}) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).Set(key, v)
return c.(Cache).SetCtx(ctx, key, v)
}
// SetWithExpire sets the cache with key and v, using given expire.
func (cc cacheCluster) SetWithExpire(key string, v interface{}, expire time.Duration) error {
return cc.SetWithExpireCtx(context.Background(), key, v, expire)
}
// SetWithExpireCtx sets the cache with key and v, using given expire.
func (cc cacheCluster) SetWithExpireCtx(ctx context.Context, key string, v interface{}, expire time.Duration) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).SetWithExpire(key, v, expire)
return c.(Cache).SetWithExpireCtx(ctx, key, v, expire)
}
// Take takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
func (cc cacheCluster) Take(v interface{}, key string, query func(v interface{}) error) error {
return cc.TakeCtx(context.Background(), v, key, query)
}
// TakeCtx takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
func (cc cacheCluster) TakeCtx(ctx context.Context, v interface{}, key string, query func(v interface{}) error) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).Take(v, key, query)
return c.(Cache).TakeCtx(ctx, v, key, query)
}
// TakeWithExpire takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
func (cc cacheCluster) TakeWithExpire(v interface{}, key string,
query func(v interface{}, expire time.Duration) error) error {
return cc.TakeWithExpireCtx(context.Background(), v, key, query)
}
// TakeWithExpireCtx takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
func (cc cacheCluster) TakeWithExpireCtx(ctx context.Context, v interface{}, key string, query func(v interface{}, expire time.Duration) error) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).TakeWithExpire(v, key, query)
return c.(Cache).TakeWithExpireCtx(ctx, v, key, query)
}