Documentation
¶
Overview ¶
Package cache 提供缓存抽象入口,默认支持内存和 Redis。
全局默认(bootstrap 自动 Init):
c := cache.Default()
服务自定义:
c := cache.New(cache.WithRedis(client), cache.WithPrefix("svc:"))
Index ¶
- Constants
- Variables
- func Bind(app *runtime.App, c Cache) error
- func Close()
- func Del(ctx context.Context, keys ...string) error
- func DelWith(ctx context.Context, c Cache, keys ...string) error
- func Delete(ctx context.Context, keys []string) error
- func DeleteJSON(ctx context.Context, key string) error
- func DeleteJSONWith(ctx context.Context, c Cache, key string) error
- func DeleteWith(ctx context.Context, c Cache, keys []string) error
- func Exists(ctx context.Context, keys ...string) (int64, error)
- func ExistsWith(ctx context.Context, c Cache, keys ...string) (int64, error)
- func Expire(ctx context.Context, key string, ttl time.Duration) error
- func ExpireWith(ctx context.Context, c Cache, key string, ttl time.Duration) error
- func Get(ctx context.Context, key string) (string, error)
- func GetJSON(ctx context.Context, key string, dst any) (bool, error)
- func GetJSONWith(ctx context.Context, c Cache, key string, dst any) (bool, error)
- func GetWith(ctx context.Context, c Cache, key string) (string, error)
- func Gets(ctx context.Context, keys []string) (map[string]string, []string)
- func GetsWith(ctx context.Context, c Cache, keys []string) (map[string]string, []string)
- func Incr(ctx context.Context, key string) (int64, error)
- func IncrWith(ctx context.Context, c Cache, key string) (int64, error)
- func Init(cacheCfg *Config) error
- func IsNotFound(err error) bool
- func Remember[T any](ctx context.Context, key string, ttl time.Duration, fn func() (T, error)) (T, error)
- func RememberWith[T any](ctx context.Context, c Cache, key string, ttl time.Duration, ...) (T, error)
- func Set(ctx context.Context, key, value string, ttl time.Duration) error
- func SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error
- func SetJSONWith(ctx context.Context, c Cache, key string, value any, ttl time.Duration) error
- func SetWith(ctx context.Context, c Cache, key, value string, ttl time.Duration) error
- func Sets(ctx context.Context, values map[string]string, ttl time.Duration) error
- func SetsWith(ctx context.Context, c Cache, values map[string]string, ttl time.Duration) error
- func TTL(ctx context.Context, key string) (time.Duration, error)
- func TTLWith(ctx context.Context, c Cache, key string) (time.Duration, error)
- type Cache
- type Config
- type Option
Constants ¶
const KeyCache runtime.Key = "cache"
Variables ¶
var ErrNotFound = errors.New("cache not found")
ErrNotFound is returned by typed cache helpers when a key is missing.
Functions ¶
func DeleteJSON ¶
DeleteJSON removes one JSON cache key from the default cache.
func DeleteJSONWith ¶
DeleteJSONWith removes one JSON cache key from the provided cache.
func DeleteWith ¶
DeleteWith removes keys from the provided cache.
func ExistsWith ¶
ExistsWith reports how many keys exist in the provided cache.
func ExpireWith ¶
ExpireWith updates the expiration for a key in the provided cache.
func GetJSON ¶
GetJSON loads a JSON value from the default cache and reports whether the key existed.
func GetJSONWith ¶
GetJSONWith loads a JSON value from the provided cache and reports whether the key existed.
func IsNotFound ¶
IsNotFound reports whether err represents a cache miss.
func Remember ¶
func Remember[T any](ctx context.Context, key string, ttl time.Duration, fn func() (T, error)) (T, error)
Remember returns a cached JSON value from the default cache, or loads and stores it once per key.
func RememberWith ¶
func RememberWith[T any](ctx context.Context, c Cache, key string, ttl time.Duration, fn func() (T, error)) (T, error)
RememberWith returns a cached JSON value from the provided cache, or loads and stores it once per key.
func SetJSONWith ¶
SetJSONWith stores value as JSON through the provided cache.