cache

package
v0.0.0-...-e636480 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCacheKey

func GenerateCacheKey(prefix string, parts ...string) string

GenerateCacheKey 生成标准化的缓存键 可以基于不同参数生成一致的键

func RegisterCache

func RegisterCache(name string, factory Factory)

RegisterCache 注册缓存实现

Types

type Cache

type Cache interface {
	Get(key string) (value string, found bool, err error)
	Set(key string, value string, ttl time.Duration) error
	Delete(key string) error
	Clear() error
}

Cache 缓存接口

func NewCache

func NewCache(config Config) (Cache, error)

NewCache 创建缓存实例

func NewMemoryCache

func NewMemoryCache(config Config) (Cache, error)

NewMemoryCache 创建一个新的内存缓存

func NewRedisCache

func NewRedisCache(config Config) (Cache, error)

NewRedisCache 创建一个新的Redis缓存

type Config

type Config struct {
	// 缓存类型: "memory", "redis" 等
	Type string
	// Redis连接地址 (仅Redis缓存使用)
	RedisAddr string
	// Redis密码 (仅Redis缓存使用)
	RedisPassword string
	// Redis数据库编号 (仅Redis缓存使用)
	RedisDB int
	// 默认缓存过期时间
	DefaultTTL time.Duration
	// 自动清理间隔时间 (仅内存缓存使用)
	CleanupInterval time.Duration
}

Config 缓存配置

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 返回默认缓存配置

type Factory

type Factory func(config Config) (Cache, error)

Factory 缓存工厂函数类型

type MemoryCache

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

MemoryCache 基于go-cache实现的内存缓存

func (*MemoryCache) Clear

func (m *MemoryCache) Clear() error

Clear 清空所有缓存

func (*MemoryCache) Delete

func (m *MemoryCache) Delete(key string) error

Delete 删除缓存项

func (*MemoryCache) Get

func (m *MemoryCache) Get(key string) (string, bool, error)

Get 获取缓存内容

func (*MemoryCache) Set

func (m *MemoryCache) Set(key string, value string, ttl time.Duration) error

Set 设置缓存内容

type RedisCache

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

RedisCache 基于Redis实现的缓存

func (*RedisCache) Clear

func (r *RedisCache) Clear() error

Clear 清空所有缓存 注意:这会清空整个Redis数据库,谨慎使用

func (*RedisCache) Delete

func (r *RedisCache) Delete(key string) error

Delete 删除缓存项

func (*RedisCache) Get

func (r *RedisCache) Get(key string) (string, bool, error)

Get 获取缓存内容

func (*RedisCache) Set

func (r *RedisCache) Set(key string, value string, ttl time.Duration) error

Set 设置缓存内容

Jump to

Keyboard shortcuts

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