cache

package
v0.0.0-...-0409f2c Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KindLocal   = "local"   // local
	KindGoCache = "gocache" // gocache
	KindLRU     = "lru"     // lru (hashicorp/golang-lru)
	KindRedis   = "redis"   // redis
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get 获取缓存值
	Get(ctx context.Context, key string) (interface{}, bool)

	// Set 设置缓存值
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

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

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

	// Clear 清空所有缓存
	Clear(ctx context.Context) error

	// GetMulti 批量获取
	GetMulti(ctx context.Context, keys ...string) map[string]interface{}

	// SetMulti 批量设置
	SetMulti(ctx context.Context, data map[string]interface{}, expiration time.Duration) error

	// DeleteMulti 批量删除
	DeleteMulti(ctx context.Context, keys ...string) error

	// Increment 自增
	Increment(ctx context.Context, key string, value int64) (int64, error)

	// Decrement 自减
	Decrement(ctx context.Context, key string, value int64) (int64, error)

	// GetWithTTL 获取值并返回剩余TTL
	GetWithTTL(ctx context.Context, key string) (interface{}, time.Duration, bool)

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

Cache 缓存接口

func NewCache

func NewCache(config Config) (Cache, error)

NewCache 创建缓存实例

func NewCacheWithOptions

func NewCacheWithOptions(config Config, options *Options) (Cache, error)

NewCacheWithOptions 创建带选项的缓存实例

func NewGoCache

func NewGoCache(config LocalConfig) Cache

NewGoCache 创建基于go-cache的本地缓存

func NewLRUCache

func NewLRUCache(config LRUCacheConfig) Cache

NewLRUCache 创建基于hashicorp/golang-lru的缓存

func NewLayeredCache

func NewLayeredCache(config Config, options *Options) (Cache, error)

NewLayeredCache 创建分层缓存(本地缓存 + 分布式缓存)

func NewLocalCache

func NewLocalCache(config LocalConfig) Cache

NewLocalCache 创建本地缓存

func NewRedisCache

func NewRedisCache(config RedisConfig) (Cache, error)

NewRedisCache 创建Redis缓存

type Config

type Config struct {
	// 缓存类型: "local" 或 "redis"
	Type string `json:"type" yaml:"type" env:"CACHE_TYPE" default:"local"`

	// Redis配置
	Redis RedisConfig `json:"redis" yaml:"redis"`

	// 本地缓存配置
	Local LocalConfig `json:"local" yaml:"local"`
}

Config 缓存配置

type LRUCacheConfig

type LRUCacheConfig struct {
	// 最大缓存项数
	MaxSize int `json:"max_size" yaml:"max_size" env:"LRU_CACHE_MAX_SIZE" default:"1000"`

	// 默认过期时间
	DefaultExpiration time.Duration `json:"default_expiration" yaml:"default_expiration" env:"LRU_CACHE_DEFAULT_EXPIRATION" default:"5m"`

	// 清理间隔
	CleanupInterval time.Duration `json:"cleanup_interval" yaml:"cleanup_interval" env:"LRU_CACHE_CLEANUP_INTERVAL" default:"10m"`
}

LRUCacheConfig LRU缓存配置

type LocalConfig

type LocalConfig struct {
	// 最大缓存项数
	MaxSize int `json:"max_size" yaml:"max_size" env:"LOCAL_CACHE_MAX_SIZE" default:"1000"`

	// 默认过期时间
	DefaultExpiration time.Duration `json:"default_expiration" yaml:"default_expiration" env:"LOCAL_CACHE_DEFAULT_EXPIRATION" default:"5m"`

	// 清理间隔
	CleanupInterval time.Duration `json:"cleanup_interval" yaml:"cleanup_interval" env:"LOCAL_CACHE_CLEANUP_INTERVAL" default:"10m"`
}

LocalConfig 本地缓存配置

type Options

type Options struct {
	// 过期时间
	Expiration time.Duration

	// 是否使用本地缓存作为一级缓存
	UseLocalCache bool

	// 本地缓存过期时间(通常比分布式缓存短)
	LocalExpiration time.Duration
}

Options 缓存选项

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions 默认选项

type RedisConfig

type RedisConfig struct {
	// Redis地址
	Addr string `json:"addr" yaml:"addr" env:"REDIS_ADDR" default:"localhost:6379"`

	// Redis密码
	Password string `json:"password" yaml:"password" env:"REDIS_PASSWORD"`

	// Redis数据库
	DB int `json:"db" yaml:"db" env:"REDIS_DB" default:"0"`

	// 连接池大小
	PoolSize int `json:"pool_size" yaml:"pool_size" env:"REDIS_POOL_SIZE" default:"10"`

	// 最小空闲连接数
	MinIdleConns int `json:"min_idle_conns" yaml:"min_idle_conns" env:"REDIS_MIN_IDLE_CONNS" default:"5"`

	// 连接超时时间
	DialTimeout time.Duration `json:"dial_timeout" yaml:"dial_timeout" env:"REDIS_DIAL_TIMEOUT" default:"5s"`

	// 读取超时时间
	ReadTimeout time.Duration `json:"read_timeout" yaml:"read_timeout" env:"REDIS_READ_TIMEOUT" default:"3s"`

	// 写入超时时间
	WriteTimeout time.Duration `json:"write_timeout" yaml:"write_timeout" env:"REDIS_WRITE_TIMEOUT" default:"3s"`

	// 连接最大空闲时间
	IdleTimeout time.Duration `json:"idle_timeout" yaml:"idle_timeout" env:"REDIS_IDLE_TIMEOUT" default:"5m"`
}

RedisConfig Redis配置

Jump to

Keyboard shortcuts

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