Documentation
¶
Index ¶
- Constants
- Variables
- func DeleteStore(ctx context.Context, store Store, key string) error
- func GO(fn func())
- func GetStore[T any](ctx context.Context, store Store, key string) (T, int, error)
- func SetStore[T any](ctx context.Context, store Store, key string, value T, ttl time.Duration) error
- func Wrap[T any](ctx context.Context, name string, store Store, key string, query Query[T]) (T, error)deprecated
- func WrapForFirstIgnoreError[T any](ctx context.Context, name string, store Store, key string, query Query[T]) (T, error)deprecated
- func WrapForFirstIgnoreErrorWithTTL[T any](ctx context.Context, store Store, key string, ttl time.Duration, ...) (T, error)
- func WrapForReuseIgnoreError[T any](ctx context.Context, name string, store Store, key string, query Query[T]) (T, error)
- func WrapForReuseIgnoreErrorWithTTL[T any](ctx context.Context, store Store, key string, ttl time.Duration, ...) (T, error)
- func WrapWithTTL[T any](ctx context.Context, store Store, key string, ttl time.Duration, ...) (T, error)
- type AbcBox
- type CacheCtr
- type CtxStorageKey
- type LimitQueryPlugin
- type LoadingForCache
- type LoadingForQuery
- type MetricsPlugin
- type Mutex128
- type Option
- type Plugin
- type Policy
- type Query
- type RedisHashStore
- func (r *RedisHashStore) Del(ctx context.Context, _ string) error
- func (r *RedisHashStore) DelAll(ctx context.Context) error
- func (r *RedisHashStore) Get(ctx context.Context, _ string) (any, error)
- func (r *RedisHashStore) IsDirectStore() bool
- func (r *RedisHashStore) Set(ctx context.Context, _ string, data any, ttl time.Duration) error
- type SingleflightGroup
- type Store
- type TaskResult
- type TimerJobList
Constants ¶
const ( SHadowKeyPrefix = "shadow:" KeepTTL = -1 // 永久存储 )
const Mutex128Shards = 128
Variables ¶
Functions ¶
func DeleteStore ¶ added in v1.0.2
DeleteStore 删除缓存
func SetStore ¶ added in v1.0.1
func SetStore[T any](ctx context.Context, store Store, key string, value T, ttl time.Duration) error
SetStore 设置缓存
func Wrap
deprecated
func WrapForFirstIgnoreError
deprecated
func WrapForFirstIgnoreError[T any](ctx context.Context, name string, store Store, key string, query Query[T]) (T, error)
Deprecated: use WrapForReuseIgnoreErrorWithTTL WrapForFirst 优先缓存封装模型, 注意 name 只能够对应一个缓存 T 如果,冲突创建,会引发错误 使用缓存策略 FirstCachePoly(1 * time.Minute) # 注意如果命中缓存,那么当 query 执行失败时,这个策略会重复使用缓存数据,直到 query 执行成功为止。
func WrapForFirstIgnoreErrorWithTTL ¶
func WrapForFirstIgnoreErrorWithTTL[T any](ctx context.Context, store Store, key string, ttl time.Duration, query Query[T]) (T, error)
WrapForFirstIgnoreErrorWithTTL # 注意如果命中缓存,那么当 query 执行失败时,这个策略会重复使用缓存数据,直到 query 执行成功为止。
func WrapForReuseIgnoreError ¶
func WrapForReuseIgnoreError[T any](ctx context.Context, name string, store Store, key string, query Query[T]) (T, error)
// Deprecated: use WrapForReuseIgnoreErrorWithTTL WrapForReuseIgnoreError 重用缓存封装模型, 注意 name 只能够对应一个缓存 T 如果,冲突创建,会引发错误 使用缓存策略 ReuseCachePloy(30 * time.Second) # 注意如果命中缓存,那么当 query 执行失败时,这个策略会重复使用缓存数据,直到 query 执行成功为止。
Types ¶
type CacheCtr ¶
func NewCacheController ¶
NewCacheController 创建一个缓存控制器, 默认使用简单策略模式,设置 15 秒的缓存过期时间
type CtxStorageKey ¶
type CtxStorageKey struct{}
CtxStorageKey 上下文存储键,用来存储可变的 storage 实现替换全局 storage
type LimitQueryPlugin ¶
type LimitQueryPlugin struct {
// contains filtered or unexported fields
}
使用 go 限流器实现 query 访问限流插件
func (*LimitQueryPlugin) InterceptCallCache ¶
func (*LimitQueryPlugin) InterceptCallQuery ¶
func (m *LimitQueryPlugin) InterceptCallQuery(ctx context.Context, key string, loadQuery LoadingForQuery) (LoadingForQuery, bool, error)
DB 限流器
type LoadingForCache ¶
LoadingForCache 封装查询方法,return:数据, 数据创建时间,错误
type LoadingForQuery ¶
LoadingForQuery 数据库封装方法
type MetricsPlugin ¶
type MetricsPlugin struct {
// contains filtered or unexported fields
}
MetricsPlugin 指标插件
func (*MetricsPlugin) InterceptCallCache ¶
func (*MetricsPlugin) InterceptCallQuery ¶
type Mutex128 ¶
type Mutex128 struct {
// contains filtered or unexported fields
}
type Plugin ¶
type Plugin interface {
// InterceptCallQuery 查询 query 前拦截调用
// return: LoadingForQuery: 不为空的场景,替换执行的 LoadingForQuery
// return: bool:是否允许继续执行插件,还是提前熔断
// return: error: 错误, 会导流程结束返回 error
InterceptCallQuery(ctx context.Context, key string, loadQuery LoadingForQuery) (LoadingForQuery, bool, error)
// InterceptCallCache 查询 cache 前拦截调用
// return: LoadingForCache: 不为空的场景,替换执行的 LoadingForCache
// return: bool:是否允许继续执行插件,还是提前熔断
// return: error: 错误, 会导流程结束返回 error
InterceptCallCache(ctx context.Context, key string, loadCache LoadingForCache) (LoadingForCache, bool, error)
}
访问控制插件
func NewLimitQueryPlugin ¶
func NewMetricsPlugin ¶
func NewMetricsPlugin(name string) Plugin
type Policy ¶
type Policy func(ctx context.Context, key string, queryFormDB LoadingForQuery, queryFormCache LoadingForCache) (any, error)
Policy 缓存控制策略, 用来控制缓存策略
func FirstCachePolyIgnoreError ¶
FirstCachePolyIgnoreError 创建一个快速缓存模型 快速缓存模型,会长时间保存缓存,并且优先使用缓存,使用业务过期时间 expireTime 来控制缓存是否过期,如果缓存过期会 拉起一个单例携程来访问 query 异步刷新缓存,并且返回本次获取到的缓存中的数据,如果访问缓存失败,则退化为简单缓存模型 # 注意如果命中缓存,那么当 query 执行失败时,这个策略会重复使用缓存数据,直到 query 执行成功为止。
func ReuseCachePloyIgnoreError ¶
ReuseCachePloyIgnoreError 创建一个使用重用缓存的访问模式 重用缓存模型,会把数据长时间的存储到缓存中,使用业务过期时间 expireTime 来控制缓存的过期, 并且在 下游 query 接口无法调用成功的场景,使用缓存数据完成服务 # 注意如果命中缓存,那么当 query 执行失败时,这个策略会重复使用缓存数据,直到 query 执行成功为止。
type RedisHashStore ¶
type RedisHashStore struct {
// contains filtered or unexported fields
}
NewRedisHashStore 创建 redis hash cache 注意 NewHashStore 设置过期时间会对整个 hash 进行设置
func NewRedisHashStore ¶
func NewRedisHashStore(ctx context.Context, rd *redis.Client, rdsKey string, rdsHashKey string) (context.Context, *RedisHashStore)
NewRedisHashStoreWithPrefix 新创建 hashKey redis 其中 key: redis key, 最后存储的 redis key 注意这里不应该使用 modecache_key hashKey: redis hash key,注意不是 redis key return: ctx 需要向下传递用来替换默认的 storage
func (*RedisHashStore) DelAll ¶
func (r *RedisHashStore) DelAll(ctx context.Context) error
DelAll 删除整个 hash
func (*RedisHashStore) IsDirectStore ¶
func (r *RedisHashStore) IsDirectStore() bool
IsDirectStore 判断是否是直接存储
type SingleflightGroup ¶
type SingleflightGroup struct {
singleflight.Group
}
type Store ¶
type Store interface {
// Get 获取缓存。当缓存键不存在时返回 ErrKeyNonExistent 错误。
Get(ctx context.Context, key string) (any, error)
// Set 设置缓存, ttl 使用 KeepTTL 表示用不过期
Set(ctx context.Context, key string, data any, ttl time.Duration) error
// Del 删除缓存。
Del(ctx context.Context, key string) error
// IsDirectStore 释放可以直接存储数据,而不需要编码后存储
// 当 IsDirectStore 为 True 时,存储管理器会少一次编码和解码的操作,以提高缓存读取的性能(本地缓存可用)
IsDirectStore() bool
}
func NewCacheStore ¶
type TaskResult ¶
type TimerJobList ¶
type TimerJobList[T any] func(ctx context.Context) ([]*TaskResult[T], error)