cache

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = &CacheError{Message: "cache miss"}

ErrCacheMiss 缓存未命中错误

Functions

func AdminCacheKey

func AdminCacheKey(username string) string

AdminCacheKey 生成管理员缓存键

func AdminIDCacheKey

func AdminIDCacheKey(adminID uint) string

AdminIDCacheKey 生成基于ID的管理员缓存键

func AdminRolesCacheKey

func AdminRolesCacheKey(adminID uint) string

AdminRolesCacheKey 生成管理员角色缓存键

func CacheKey

func CacheKey(parts ...string) string

CacheKey 生成缓存键的辅助函数

func GameCacheKey

func GameCacheKey(gameID uint) string

GameCacheKey 生成游戏缓存键

func GamesCacheKey

func GamesCacheKey() string

GamesCacheKey 生成游戏列表缓存键

func IsCacheMiss

func IsCacheMiss(err error) bool

IsCacheMiss 判断是否为缓存未命中

func PermissionCacheKey

func PermissionCacheKey(permissionID string) string

PermissionCacheKey 生成权限缓存键

func RoleCacheKey

func RoleCacheKey(roleID uint) string

RoleCacheKey 生成角色缓存键

func RolePermissionsCacheKey

func RolePermissionsCacheKey(roleID uint) string

RolePermissionsCacheKey 生成角色权限缓存键

Types

type CacheError

type CacheError struct {
	Message string
}

CacheError 缓存错误

func (*CacheError) Error

func (e *CacheError) Error() string

type CacheHelper

type CacheHelper struct {
	// contains filtered or unexported fields
}

CacheHelper 缓存辅助函数

func NewCacheHelper

func NewCacheHelper(store CacheStore) *CacheHelper

func (*CacheHelper) GetJSON

func (h *CacheHelper) GetJSON(ctx context.Context, key string, dest interface{}) error

GetJSON 从缓存获取 JSON 对象

func (*CacheHelper) Remember

func (h *CacheHelper) Remember(ctx context.Context, key string, ttl time.Duration, dest interface{}, loader func() (interface{}, error)) error

Remember 缓存模式:如果缓存存在则返回,否则执行 loader 并缓存结果

func (*CacheHelper) SetJSON

func (h *CacheHelper) SetJSON(ctx context.Context, key string, value interface{}, ttl time.Duration) error

SetJSON 设置 JSON 对象到缓存

type CacheStore

type CacheStore interface {
	// Get 获取缓存值
	Get(ctx context.Context, key string) ([]byte, error)

	// Set 设置缓存值
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

	// Delete 删除缓存
	Delete(ctx context.Context, key string) error

	// DeletePattern 删除匹配模式的所有缓存
	DeletePattern(ctx context.Context, pattern string) error

	// Exists 检查键是否存在
	Exists(ctx context.Context, key string) (bool, error)

	// Close 关闭缓存连接
	Close() error
}

CacheStore 定义缓存接口

func NewCacheStore

func NewCacheStore(cfg config.CacheConfig) (CacheStore, error)

NewCacheStore 根据配置创建缓存实例

type LocalCache

type LocalCache struct {
	// contains filtered or unexported fields
}

LocalCache 本地内存缓存实现(使用 go-cache,类似 Java Caffeine)

func NewLocalCache

func NewLocalCache(defaultTTL, cleanupInterval time.Duration) *LocalCache

NewLocalCache 创建本地缓存实例

func (*LocalCache) Close

func (c *LocalCache) Close() error

func (*LocalCache) Delete

func (c *LocalCache) Delete(ctx context.Context, key string) error

func (*LocalCache) DeletePattern

func (c *LocalCache) DeletePattern(ctx context.Context, pattern string) error

func (*LocalCache) Exists

func (c *LocalCache) Exists(ctx context.Context, key string) (bool, error)

func (*LocalCache) Get

func (c *LocalCache) Get(ctx context.Context, key string) ([]byte, error)

func (*LocalCache) Set

func (c *LocalCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

func (*LocalCache) Stats

func (c *LocalCache) Stats() (itemCount int)

Stats 返回缓存统计信息

type NullCache

type NullCache struct{}

NullCache 空缓存实现(禁用缓存时使用)

func NewNullCache

func NewNullCache() *NullCache

func (*NullCache) Close

func (c *NullCache) Close() error

func (*NullCache) Delete

func (c *NullCache) Delete(ctx context.Context, key string) error

func (*NullCache) DeletePattern

func (c *NullCache) DeletePattern(ctx context.Context, pattern string) error

func (*NullCache) Exists

func (c *NullCache) Exists(ctx context.Context, key string) (bool, error)

func (*NullCache) Get

func (c *NullCache) Get(ctx context.Context, key string) ([]byte, error)

func (*NullCache) Set

func (c *NullCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache Redis 缓存实现

func NewRedisCache

func NewRedisCache(addr, password string, db int, poolSize int, defaultTTL time.Duration) (*RedisCache, error)

NewRedisCache 创建 Redis 缓存实例

func (*RedisCache) Client

func (c *RedisCache) Client() *redis.Client

Client 返回底层 Redis 客户端(用于高级操作)

func (*RedisCache) Close

func (c *RedisCache) Close() error

func (*RedisCache) Delete

func (c *RedisCache) Delete(ctx context.Context, key string) error

func (*RedisCache) DeletePattern

func (c *RedisCache) DeletePattern(ctx context.Context, pattern string) error

func (*RedisCache) Exists

func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Jump to

Keyboard shortcuts

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