1 Star 0 Fork 0

yzsunjianguo / common_pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cache.go 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2024-02-08 10:53 . 初始化
// Package cache is memory and redis cache libraries.
package cache
import (
"context"
"errors"
"time"
)
var (
// DefaultExpireTime default expiry time
DefaultExpireTime = time.Hour * 24
// DefaultNotFoundExpireTime expiry time when result is empty 1 minute,
// often used for cache time when data is empty (cache pass-through)
DefaultNotFoundExpireTime = time.Minute * 10
// NotFoundPlaceholder placeholder
NotFoundPlaceholder = "*"
// DefaultClient generate a cache client, where keyPrefix is generally the business prefix
DefaultClient Cache
// ErrPlaceholder .
ErrPlaceholder = errors.New("cache: placeholder")
// ErrSetMemoryWithNotFound .
ErrSetMemoryWithNotFound = errors.New("cache: set memory cache err for not found")
)
// Cache driver interface
type Cache interface {
Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error
Get(ctx context.Context, key string, val interface{}) error
MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error
MultiGet(ctx context.Context, keys []string, valueMap interface{}) error
Del(ctx context.Context, keys ...string) error
SetCacheWithNotFound(ctx context.Context, key string) error
}
// Set data
func Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error {
return DefaultClient.Set(ctx, key, val, expiration)
}
// Get data
func Get(ctx context.Context, key string, val interface{}) error {
return DefaultClient.Get(ctx, key, val)
}
// MultiSet multiple set data
func MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error {
return DefaultClient.MultiSet(ctx, valMap, expiration)
}
// MultiGet multiple get data
func MultiGet(ctx context.Context, keys []string, valueMap interface{}) error {
return DefaultClient.MultiGet(ctx, keys, valueMap)
}
// Del multiple delete data
func Del(ctx context.Context, keys ...string) error {
return DefaultClient.Del(ctx, keys...)
}
// SetCacheWithNotFound .
func SetCacheWithNotFound(ctx context.Context, key string) error {
return DefaultClient.SetCacheWithNotFound(ctx, key)
}
Go
1
https://gitee.com/yzsunjianguo/common_pkg.git
git@gitee.com:yzsunjianguo/common_pkg.git
yzsunjianguo
common_pkg
common_pkg
v1.0.1

搜索帮助