cache

package
v0.1.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package cache 提供缓存抽象入口,默认支持内存和 Redis。

全局默认(bootstrap 自动 Init):

c := cache.Default()

服务自定义:

c := cache.New(cache.WithRedis(client), cache.WithPrefix("svc:"))

Index

Constants

View Source
const KeyCache runtime.Key = "cache"

Variables

View Source
var ErrNotFound = errors.New("cache not found")

ErrNotFound is returned by typed cache helpers when a key is missing.

Functions

func Bind

func Bind(app *runtime.App, c Cache) error

func Close

func Close()

Close 关闭全局缓存

func Del

func Del(ctx context.Context, keys ...string) error

Del removes keys from the default cache.

func DelWith

func DelWith(ctx context.Context, c Cache, keys ...string) error

DelWith removes keys from the provided cache.

func Delete

func Delete(ctx context.Context, keys []string) error

Delete removes keys from the default cache.

func DeleteJSON

func DeleteJSON(ctx context.Context, key string) error

DeleteJSON removes one JSON cache key from the default cache.

func DeleteJSONWith

func DeleteJSONWith(ctx context.Context, c Cache, key string) error

DeleteJSONWith removes one JSON cache key from the provided cache.

func DeleteWith

func DeleteWith(ctx context.Context, c Cache, keys []string) error

DeleteWith removes keys from the provided cache.

func Exists

func Exists(ctx context.Context, keys ...string) (int64, error)

Exists reports how many keys exist in the default cache.

func ExistsWith

func ExistsWith(ctx context.Context, c Cache, keys ...string) (int64, error)

ExistsWith reports how many keys exist in the provided cache.

func Expire

func Expire(ctx context.Context, key string, ttl time.Duration) error

Expire updates the expiration for a key in the default cache.

func ExpireWith

func ExpireWith(ctx context.Context, c Cache, key string, ttl time.Duration) error

ExpireWith updates the expiration for a key in the provided cache.

func Get

func Get(ctx context.Context, key string) (string, error)

Get reads a string value from the default cache.

func GetJSON

func GetJSON(ctx context.Context, key string, dst any) (bool, error)

GetJSON loads a JSON value from the default cache and reports whether the key existed.

func GetJSONWith

func GetJSONWith(ctx context.Context, c Cache, key string, dst any) (bool, error)

GetJSONWith loads a JSON value from the provided cache and reports whether the key existed.

func GetWith

func GetWith(ctx context.Context, c Cache, key string) (string, error)

GetWith reads a string value from the provided cache.

func Gets

func Gets(ctx context.Context, keys []string) (map[string]string, []string)

Gets reads multiple string values from the default cache.

func GetsWith

func GetsWith(ctx context.Context, c Cache, keys []string) (map[string]string, []string)

GetsWith reads multiple string values from the provided cache.

func Incr

func Incr(ctx context.Context, key string) (int64, error)

Incr increments a key in the default cache.

func IncrWith

func IncrWith(ctx context.Context, c Cache, key string) (int64, error)

IncrWith increments a key in the provided cache.

func Init

func Init(cacheCfg *Config) error

Init 初始化全局缓存:有 Redis 用 Redis,否则内存

func IsNotFound

func IsNotFound(err error) bool

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 Set

func Set(ctx context.Context, key, value string, ttl time.Duration) error

Set writes a string value to the default cache.

func SetJSON

func SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error

SetJSON stores value as JSON through the default cache.

func SetJSONWith

func SetJSONWith(ctx context.Context, c Cache, key string, value any, ttl time.Duration) error

SetJSONWith stores value as JSON through the provided cache.

func SetWith

func SetWith(ctx context.Context, c Cache, key, value string, ttl time.Duration) error

SetWith writes a string value to the provided cache.

func Sets

func Sets(ctx context.Context, values map[string]string, ttl time.Duration) error

Sets writes multiple string values to the default cache.

func SetsWith

func SetsWith(ctx context.Context, c Cache, values map[string]string, ttl time.Duration) error

SetsWith writes multiple string values to the provided cache.

func TTL

func TTL(ctx context.Context, key string) (time.Duration, error)

TTL returns the ttl for a key in the default cache.

func TTLWith

func TTLWith(ctx context.Context, c Cache, key string) (time.Duration, error)

TTLWith returns the ttl for a key in the provided cache.

Types

type Cache

type Cache = pkgcache.Cache

Cache 是 core/cache 对外缓存接口,底层由 pkg/cache 后端实现。

func Default

func Default() Cache

Default 返回全局默认缓存(Init 之后为 Redis,否则内存)

func From

func From(app *runtime.App) Cache

func New

func New(opts ...Option) Cache

New 创建缓存实例(服务自定义配置)

func NewFromConfig

func NewFromConfig(cacheCfg *Config, client *redis.Client) Cache

NewFromConfig 按缓存配置和可选 Redis client 创建缓存实例。

type Config

type Config struct {
	Prefix string `mapstructure:"prefix" yaml:"prefix"`
}

type Option

type Option func(*options)

Option 缓存配置选项

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix 设置 key 前缀

func WithRedis

func WithRedis(client *redis.Client) Option

WithRedis 使用 Redis 后端

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL