1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-07 22:22:28 +08:00

cache增加带Context版本,开放平台相关接口支持Context版本 (#653)

This commit is contained in:
okhowang
2023-04-03 20:32:44 +08:00
committed by GitHub
parent 01784c2a4a
commit 07b7dc40fc
7 changed files with 178 additions and 43 deletions

46
cache/cache.go vendored
View File

@@ -1,6 +1,9 @@
package cache
import "time"
import (
"context"
"time"
)
// Cache interface
type Cache interface {
@@ -9,3 +12,44 @@ type Cache interface {
IsExist(key string) bool
Delete(key string) error
}
// ContextCache interface
type ContextCache interface {
Cache
GetContext(ctx context.Context, key string) interface{}
SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error
IsExistContext(ctx context.Context, key string) bool
DeleteContext(ctx context.Context, key string) error
}
// GetContext get value from cache
func GetContext(ctx context.Context, cache Cache, key string) interface{} {
if cache, ok := cache.(ContextCache); ok {
return cache.GetContext(ctx, key)
}
return cache.Get(key)
}
// SetContext set value to cache
func SetContext(ctx context.Context, cache Cache, key string, val interface{}, timeout time.Duration) error {
if cache, ok := cache.(ContextCache); ok {
return cache.SetContext(ctx, key, val, timeout)
}
return cache.Set(key, val, timeout)
}
// IsExistContext check value exists in cache.
func IsExistContext(ctx context.Context, cache Cache, key string) bool {
if cache, ok := cache.(ContextCache); ok {
return cache.IsExistContext(ctx, key)
}
return cache.IsExist(key)
}
// DeleteContext delete value in cache.
func DeleteContext(ctx context.Context, cache Cache, key string) error {
if cache, ok := cache.(ContextCache); ok {
return cache.DeleteContext(ctx, key)
}
return cache.Delete(key)
}