cache

package
v1.4.51 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cache 缓存层 提供 Redis 和本地缓存的统一接口,支持分布式锁、防击穿、防穿透等功能

Package cache is a package that unifies the definition of caching, supporting both redis and local caching.

Package cache 缓存层

提供业务数据的缓存访问能力,支持 Redis 和本地缓存两种后端。 本包使用泛型适配器模式,通过嵌入 dao.CacheAdapter 获得通用缓存操作, 同时支持在业务缓存中添加自定义方法。

Index

Constants

View Source
const (
	// CacheNameExampleCachePrefixKeyLock cache prefix key, must end with a colon
	CacheNameExampleCachePrefixKeyLock = "lock:prefixKeyExample:"
	// CacheNameExampleCachePrefixKey 缓存名称示例数据前缀
	CacheNameExampleCachePrefixKey = "data:prefixKeyExample:"
	// CacheNameExampleExpireTime expire time
	CacheNameExampleExpireTime = 30 * time.Minute
)
View Source
const (
	// UserExampleCachePrefixKey UserExample 缓存数据的 Redis key 前缀
	// 实际存储格式:data:userExample::{id}
	UserExampleCachePrefixKey = "data:userExample:"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type NameExample added in v1.4.27

type NameExample interface {
	GetLoopLock(ctx context.Context, key string, options ...redsync.Option) (*redsync.Mutex, error)
	GetLock(ctx context.Context, key string, options ...redsync.Option) (*redsync.Mutex, error)
	// 封装了锁的获取、看门狗自动续期、业务执行及释放逻辑
	WatchDogLock(ctx context.Context, key string, expiry time.Duration, task func(ctx context.Context) error, options ...redsync.Option) error
	WatchDogLoopLock(ctx context.Context, key string, expiry time.Duration, task func(ctx context.Context) error, options ...redsync.Option) error

	Set(ctx context.Context, key string, data interface{}, duration time.Duration) error

	Get(ctx context.Context, key string) (interface{}, error)
	GetIDByKey(ctx context.Context, key string) (id string, err error)

	Del(ctx context.Context, key string) error
	DelByPrefix(ctx context.Context, prefix string) error
	DelByKey(ctx context.Context, key string) error

	SetPlaceholder(ctx context.Context, key string) error
	SetPlaceholderByKey(ctx context.Context, key string) error
	IsPlaceholderErr(err error) bool

	GetCacheName(ctx context.Context, keyNameExample keyTypeExample) (valueTypeExample, error)
	SetCacheName(ctx context.Context, keyNameExample keyTypeExample, data valueTypeExample, duration time.Duration) error
	DelCacheName(ctx context.Context, keyNameExample keyTypeExample) error
}

NameExample is the cache interface for cache name example

func NewNameExample added in v1.4.27

func NewNameExample(cacheType *database.CacheType) NameExample

NewNameExample new a cache

type UserExampleCache

type UserExampleCache interface {
	dao.Cache[model.UserExample]
}

UserExampleCache UserExample 业务缓存接口

设计思路:

  • 直接嵌入 dao.Cache[model.UserExample],自动继承所有缓存方法
  • 如需添加自定义缓存方法,在此接口中声明并在 userExampleCache 中实现

继承的方法包括:

  • Set/Get/Del: 单条数据的增删改查
  • MultiSet/MultiGet: 批量数据操作
  • DelByPrefix: 按前缀批量删除
  • Lock/Unlock: 分布式锁

func NewUserExampleCache

func NewUserExampleCache(cacheType *database.CacheType) UserExampleCache

NewUserExampleCache 创建 UserExample 缓存实例

参数说明:

  • cacheType: 缓存类型配置,包含 Redis 连接信息和缓存类型标识

返回值:

  • UserExampleCache: 缓存接口实例,传入 nil 或不支持的类型时返回 nil

创建流程:

  1. 创建通用 Redis 驱动(底层缓存操作)
  2. 创建泛型适配器(将通用驱动适配为 dao.Cache[T] 接口)
  3. 包装为业务缓存并返回

Jump to

Keyboard shortcuts

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