Add context to cache API

This commit is contained in:
Ming Deng
2020-10-04 22:11:28 +08:00
parent b89d9511ab
commit 3364c609de
11 changed files with 259 additions and 230 deletions

View File

@@ -15,6 +15,7 @@
package cache
import (
"context"
"encoding/json"
"errors"
"sync"
@@ -58,50 +59,55 @@ func NewMemoryCache() Cache {
// Get returns cache from memory.
// If non-existent or expired, return nil.
func (bc *MemoryCache) Get(name string) interface{} {
func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error) {
bc.RLock()
defer bc.RUnlock()
if itm, ok := bc.items[name]; ok {
if itm, ok := bc.items[key]; ok {
if itm.isExpire() {
return nil
return nil, errors.New("the key is expired")
}
return itm.val
return itm.val, nil
}
return nil
return nil, nil
}
// GetMulti gets caches from memory.
// If non-existent or expired, return nil.
func (bc *MemoryCache) GetMulti(names []string) []interface{} {
func (bc *MemoryCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
var rc []interface{}
for _, name := range names {
rc = append(rc, bc.Get(name))
for _, name := range keys {
val, err := bc.Get(context.Background(), name)
if err != nil {
rc = append(rc, err)
} else {
rc = append(rc, val)
}
}
return rc
return rc, nil
}
// Put puts cache into memory.
// If lifespan is 0, it will never overwrite this value unless restarted
func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error {
func (bc *MemoryCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
bc.Lock()
defer bc.Unlock()
bc.items[name] = &MemoryItem{
val: value,
bc.items[key] = &MemoryItem{
val: val,
createdTime: time.Now(),
lifespan: lifespan,
lifespan: timeout,
}
return nil
}
// Delete cache in memory.
func (bc *MemoryCache) Delete(name string) error {
func (bc *MemoryCache) Delete(ctx context.Context, key string) error {
bc.Lock()
defer bc.Unlock()
if _, ok := bc.items[name]; !ok {
if _, ok := bc.items[key]; !ok {
return errors.New("key not exist")
}
delete(bc.items, name)
if _, ok := bc.items[name]; ok {
delete(bc.items, key)
if _, ok := bc.items[key]; ok {
return errors.New("delete key error")
}
return nil
@@ -109,7 +115,7 @@ func (bc *MemoryCache) Delete(name string) error {
// Incr increases cache counter in memory.
// Supports int,int32,int64,uint,uint32,uint64.
func (bc *MemoryCache) Incr(key string) error {
func (bc *MemoryCache) Incr(ctx context.Context, key string) error {
bc.Lock()
defer bc.Unlock()
itm, ok := bc.items[key]
@@ -136,7 +142,7 @@ func (bc *MemoryCache) Incr(key string) error {
}
// Decr decreases counter in memory.
func (bc *MemoryCache) Decr(key string) error {
func (bc *MemoryCache) Decr(ctx context.Context, key string) error {
bc.Lock()
defer bc.Unlock()
itm, ok := bc.items[key]
@@ -175,17 +181,17 @@ func (bc *MemoryCache) Decr(key string) error {
}
// IsExist checks if cache exists in memory.
func (bc *MemoryCache) IsExist(name string) bool {
func (bc *MemoryCache) IsExist(ctx context.Context, key string) (bool, error) {
bc.RLock()
defer bc.RUnlock()
if v, ok := bc.items[name]; ok {
return !v.isExpire()
if v, ok := bc.items[key]; ok {
return !v.isExpire(), nil
}
return false
return false, nil
}
// ClearAll deletes all cache in memory.
func (bc *MemoryCache) ClearAll() error {
func (bc *MemoryCache) ClearAll(context.Context) error {
bc.Lock()
defer bc.Unlock()
bc.items = make(map[string]*MemoryItem)