cache

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KindLocal   = "local"   // local
	KindGoCache = "gocache" // gocache
	KindRedis   = "redis"   // redis
)

Variables

This section is empty.

Functions

func Clear

func Clear(ctx context.Context) error

Clear 清空全局缓存

func CloseGlobalCache

func CloseGlobalCache() error

CloseGlobalCache 关闭全局缓存连接

func Delete

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

Delete 从全局缓存删除

func DeleteMulti

func DeleteMulti(ctx context.Context, keys ...string) error

DeleteMulti 批量删除

func Exists

func Exists(ctx context.Context, key string) bool

Exists 检查全局缓存中键是否存在

func Get

func Get(ctx context.Context, key string) (interface{}, bool)

Get 从全局缓存获取值

func GetMulti

func GetMulti(ctx context.Context, keys ...string) map[string]interface{}

GetMulti 批量获取

func InitGlobalCache

func InitGlobalCache(config Config) error

InitGlobalCache 初始化全局缓存实例

func InitGlobalCacheWithOptions

func InitGlobalCacheWithOptions(config Config, options *Options) error

InitGlobalCacheWithOptions 使用选项初始化全局缓存实例

func Set

func Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

Set 设置全局缓存值

func SetGlobalCache

func SetGlobalCache(c Cache)

SetGlobalCache 设置全局缓存实例(主要用于测试)

func SetMulti

func SetMulti(ctx context.Context, data map[string]interface{}, expiration time.Duration) error

SetMulti 批量设置

Types

type Cache

type Cache interface {
	// Get retrieves a cached value
	Get(ctx context.Context, key string) (interface{}, bool)

	// Set stores a value in cache
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

	// Delete removes a cached value
	Delete(ctx context.Context, key string) error

	// Exists checks if a key exists
	Exists(ctx context.Context, key string) bool

	// Clear removes all cached values
	Clear(ctx context.Context) error

	// GetMulti retrieves multiple values at once
	GetMulti(ctx context.Context, keys ...string) map[string]interface{}

	// SetMulti stores multiple values at once
	SetMulti(ctx context.Context, data map[string]interface{}, expiration time.Duration) error

	// DeleteMulti removes multiple values at once
	DeleteMulti(ctx context.Context, keys ...string) error

	// Increment increments a numeric value
	Increment(ctx context.Context, key string, value int64) (int64, error)

	// Decrement decrements a numeric value
	Decrement(ctx context.Context, key string, value int64) (int64, error)

	// GetWithTTL retrieves value with remaining TTL
	GetWithTTL(ctx context.Context, key string) (interface{}, time.Duration, bool)

	// Close closes the cache connection
	Close() error
}

Cache defines the cache interface

func GetGlobalCache

func GetGlobalCache() Cache

GetGlobalCache 获取全局缓存实例 如果未初始化,返回一个默认的本地缓存实例

func NewCache

func NewCache(config Config) (Cache, error)

NewCache creates a cache instance based on configuration

func NewCacheWithOptions

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

NewCacheWithOptions creates a cache instance with additional options

func NewGoCache

func NewGoCache(config LocalConfig) Cache

NewGoCache creates a local cache based on go-cache package

func NewLayeredCache

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

NewLayeredCache creates a layered cache (local cache + distributed cache)

func NewLocalCache

func NewLocalCache(config LocalConfig) Cache

NewLocalCache creates a new local cache instance

func NewRedisCache

func NewRedisCache(config RedisConfig) (Cache, error)

NewRedisCache creates a new Redis cache instance

type Config

type Config struct {
	// Cache type: "local" or "redis"
	Type string `json:"type" yaml:"type" env:"CACHE_TYPE" default:"local"`

	// Redis configuration
	Redis RedisConfig `json:"redis" yaml:"redis"`

	// Local cache configuration
	Local LocalConfig `json:"local" yaml:"local"`
}

Config defines cache configuration

type LocalConfig

type LocalConfig struct {
	// Maximum number of cache items
	MaxSize int `json:"max_size" yaml:"max_size" env:"LOCAL_CACHE_MAX_SIZE" default:"1000"`

	// Default expiration time
	DefaultExpiration time.Duration `json:"default_expiration" yaml:"default_expiration" env:"LOCAL_CACHE_DEFAULT_EXPIRATION" default:"5m"`

	// Cleanup interval
	CleanupInterval time.Duration `json:"cleanup_interval" yaml:"cleanup_interval" env:"LOCAL_CACHE_CLEANUP_INTERVAL" default:"10m"`
}

LocalConfig defines local cache configuration

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 server address
	Addr string `json:"addr" yaml:"addr" env:"REDIS_ADDR" default:"localhost:6379"`

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

	// Redis database number
	DB int `json:"db" yaml:"db" env:"REDIS_DB" default:"0"`

	// Connection pool size
	PoolSize int `json:"pool_size" yaml:"pool_size" env:"REDIS_POOL_SIZE" default:"10"`

	// Minimum idle connections
	MinIdleConns int `json:"min_idle_conns" yaml:"min_idle_conns" env:"REDIS_MIN_IDLE_CONNS" default:"5"`

	// Connection dial timeout
	DialTimeout time.Duration `json:"dial_timeout" yaml:"dial_timeout" env:"REDIS_DIAL_TIMEOUT" default:"5s"`

	// Read timeout
	ReadTimeout time.Duration `json:"read_timeout" yaml:"read_timeout" env:"REDIS_READ_TIMEOUT" default:"3s"`

	// Write timeout
	WriteTimeout time.Duration `json:"write_timeout" yaml:"write_timeout" env:"REDIS_WRITE_TIMEOUT" default:"3s"`

	// Connection idle timeout
	IdleTimeout time.Duration `json:"idle_timeout" yaml:"idle_timeout" env:"REDIS_IDLE_TIMEOUT" default:"5m"`
}

RedisConfig defines Redis configuration

Jump to

Keyboard shortcuts

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