cache

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 5 Imported by: 0

README

cache

该目录只是对外公开的便捷转发层。

  • 面向业务代码提供 gorp/cache 入口
  • 真实实现仍在 framework/*
  • 不在这里承载独立缓存实现或新语义

Documentation

Index

Constants

This section is empty.

Variables

ErrCacheMiss indicates that the cache key does not exist. ErrCacheMiss 表示缓存未命中。

Functions

func Del

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

Del 删除缓存。 删除指定 key 的缓存值。

func Get

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

Get 读取缓存。 根据 key 获取缓存值,若 key 不存在返回 ErrCacheMiss。

func GetService

func GetService(ctx context.Context) (datacontract.Cache, error)

GetService 从容器获取统一缓存服务。 通过 ctx 解析框架容器后再解析缓存实例。

func MGet

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

MGet 批量读取缓存。 一次性获取多个 key 的值,返回 key→value 映射。

func MustGetService added in v0.1.4

func MustGetService(ctx context.Context) datacontract.Cache

MustGetService 从容器获取统一缓存服务,失败时 panic。 用于业务初始化阶段,确认缓存服务必须可用的场景。

func Remember

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

Remember 读取缓存,未命中时回源计算并回填。 先尝试读取 key 的缓存值;若缓存未命中,则调用 fn 计算结果, 将结果写入缓存(ttl 过期时间)并返回。

Example:

value, err := cache.Remember(ctx, "user:42", time.Minute, func(ctx context.Context) (string, error) {
    return loadUserJSON(ctx, 42)
})

func Set

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

Set 写入缓存。 将 value 写入 key,并设置过期时间 ttl。

Types

type Cache

type Cache = datacontract.Cache

Cache is the top-level alias of the unified cache contract. Cache 是统一缓存契约的顶层别名。

Jump to

Keyboard shortcuts

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