Documentation
¶
Index ¶
- type Cache
- func (c *Cache) Clone() internal.Configurable
- func (c *Cache) Get() interface{}
- func (c *Cache) GetDefaultTTL() time.Duration
- func (c *Cache) GetKeyPrefix() string
- func (c *Cache) GetModuleName() string
- func (c *Cache) GetSerializer() string
- func (c *Cache) GetType() CacheType
- func (c *Cache) IsEnabled() bool
- func (c *Cache) Set(data interface{})
- func (c *Cache) Validate() error
- func (c *Cache) WithDefaultTTL(ttl time.Duration) *Cache
- func (c *Cache) WithEnabled(enabled bool) *Cache
- func (c *Cache) WithExpiring(expiring Expiring) *Cache
- func (c *Cache) WithKeyPrefix(prefix string) *Cache
- func (c *Cache) WithMemory(memory Memory) *Cache
- func (c *Cache) WithModuleName(moduleName string) *Cache
- func (c *Cache) WithRedis(redis Redis) *Cache
- func (c *Cache) WithRistretto(ristretto Ristretto) *Cache
- func (c *Cache) WithSerializer(serializer string) *Cache
- func (c *Cache) WithSharded(sharded Sharded) *Cache
- func (c *Cache) WithTwoLevel(twoLevel TwoLevel) *Cache
- func (c *Cache) WithType(cacheType CacheType) *Cache
- type CacheType
- type Expiring
- type Memory
- func (m *Memory) Clone() internal.Configurable
- func (m *Memory) Get() interface{}
- func (m *Memory) Set(data interface{})
- func (m *Memory) Validate() error
- func (m *Memory) WithCapacity(capacity int) *Memory
- func (m *Memory) WithCleanupSize(cleanupSize int) *Memory
- func (m *Memory) WithDefaultTTL(defaultTTL time.Duration) *Memory
- func (m *Memory) WithMaxSize(maxSize int) *Memory
- func (m *Memory) WithModuleName(moduleName string) *Memory
- type Redis
- func (r *Redis) Clone() internal.Configurable
- func (r *Redis) Get() interface{}
- func (r *Redis) Set(data interface{})
- func (r *Redis) Validate() error
- func (r *Redis) WithAddr(addr string) *Redis
- func (r *Redis) WithAddrs(addrs []string) *Redis
- func (r *Redis) WithClientName(clientName string) *Redis
- func (r *Redis) WithClusterMode(clusterMode bool) *Redis
- func (r *Redis) WithContextTimeoutEnabled(enabled bool) *Redis
- func (r *Redis) WithDB(db int) *Redis
- func (r *Redis) WithIdleTimeout(idleTimeout time.Duration) *Redis
- func (r *Redis) WithMasterName(masterName string) *Redis
- func (r *Redis) WithMaxActiveConns(maxActiveConns int) *Redis
- func (r *Redis) WithMaxConnAge(maxConnAge time.Duration) *Redis
- func (r *Redis) WithMaxRedirects(maxRedirects int) *Redis
- func (r *Redis) WithMaxRetries(maxRetries int) *Redis
- func (r *Redis) WithMaxRetryBackoff(maxRetryBackoff time.Duration) *Redis
- func (r *Redis) WithMinIdleConns(minIdleConns int) *Redis
- func (r *Redis) WithMinRetryBackoff(minRetryBackoff time.Duration) *Redis
- func (r *Redis) WithModuleName(moduleName string) *Redis
- func (r *Redis) WithPassword(password string) *Redis
- func (r *Redis) WithPoolSize(poolSize int) *Redis
- func (r *Redis) WithPoolTimeout(poolTimeout time.Duration) *Redis
- func (r *Redis) WithProtocol(protocol int) *Redis
- func (r *Redis) WithReadBufferSize(readBufferSize int) *Redis
- func (r *Redis) WithReadOnly(readOnly bool) *Redis
- func (r *Redis) WithReadTimeout(readTimeout time.Duration) *Redis
- func (r *Redis) WithRouteByLatency(routeByLatency bool) *Redis
- func (r *Redis) WithRouteRandomly(routeRandomly bool) *Redis
- func (r *Redis) WithSentinelPassword(password string) *Redis
- func (r *Redis) WithSentinelUsername(username string) *Redis
- func (r *Redis) WithUsername(username string) *Redis
- func (r *Redis) WithWriteBufferSize(writeBufferSize int) *Redis
- func (r *Redis) WithWriteTimeout(writeTimeout time.Duration) *Redis
- type Ristretto
- func (r *Ristretto) Clone() internal.Configurable
- func (r *Ristretto) Get() interface{}
- func (r *Ristretto) GetBufferItems() int64
- func (r *Ristretto) GetCost() int64
- func (r *Ristretto) GetMaxCost() int64
- func (r *Ristretto) GetNumCounters() int64
- func (r *Ristretto) IsIgnoreInternalCost() bool
- func (r *Ristretto) IsKeyToHash() bool
- func (r *Ristretto) IsMetricsEnabled() bool
- func (r *Ristretto) Set(data interface{})
- func (r *Ristretto) Validate() error
- type Sharded
- type TwoLevel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`
Type CacheType `mapstructure:"type" yaml:"type" json:"type"`
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"`
// 基础配置
DefaultTTL time.Duration `mapstructure:"default-ttl" yaml:"default-ttl" json:"defaultTtl"`
KeyPrefix string `mapstructure:"key-prefix" yaml:"key-prefix" json:"keyPrefix"`
Serializer string `mapstructure:"serializer" yaml:"serializer" json:"serializer"` // json, gob, msgpack
// 各种缓存实现的配置(按需使用)
Memory Memory `mapstructure:"memory" yaml:"memory" json:"memory"`
Ristretto Ristretto `mapstructure:"ristretto" yaml:"ristretto" json:"ristretto"`
Redis Redis `mapstructure:"redis" yaml:"redis" json:"redis"`
Sharded Sharded `mapstructure:"sharded" yaml:"sharded" json:"sharded"`
TwoLevel TwoLevel `mapstructure:"two-level" yaml:"two-level" json:"twoLevel"`
Expiring Expiring `mapstructure:"expiring" yaml:"expiring" json:"expiring"`
}
Cache 总缓存配置
func (*Cache) WithDefaultTTL ¶
WithDefaultTTL 设置默认TTL
func (*Cache) WithExpiring ¶
WithExpiring 设置过期缓存配置
func (*Cache) WithKeyPrefix ¶
WithKeyPrefix 设置键前缀
func (*Cache) WithModuleName ¶
WithModuleName 设置模块名称
func (*Cache) WithRistretto ¶
WithRistretto 设置Ristretto缓存配置
func (*Cache) WithSerializer ¶
WithSerializer 设置序列化器
func (*Cache) WithSharded ¶
WithSharded 设置分片缓存配置
func (*Cache) WithTwoLevel ¶
WithTwoLevel 设置二级缓存配置
type CacheType ¶
type CacheType string
CacheType 缓存类型
const ( CacheTypeMemory CacheType = "memory" // 内存缓存(LRU) CacheTypeRistretto CacheType = "ristretto" // 高性能缓存 CacheTypeRedis CacheType = "redis" // Redis缓存 CacheTypeSharded CacheType = "sharded" // 分片缓存 CacheTypeTwoLevel CacheType = "two-level" // 二级缓存 CacheTypeExpiring CacheType = "expiring" // 过期缓存 )
type Expiring ¶
type Expiring struct {
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名
CleanupInterval time.Duration `mapstructure:"cleanup-interval" yaml:"cleanup-interval" json:"cleanupInterval"` // 清理间隔
DefaultTTL time.Duration `mapstructure:"default-ttl" yaml:"default-ttl" json:"defaultTtl"` // 默认TTL
MaxSize int `mapstructure:"max-size" yaml:"max-size" json:"maxSize"` // 最大大小
EvictionPolicy string `mapstructure:"eviction-policy" yaml:"eviction-policy" json:"evictionPolicy"` // 驱逐策略: lru, lfu, fifo
EnableLazyExpiry bool `mapstructure:"enable-lazy-expiry" yaml:"enable-lazy-expiry" json:"enableLazyExpiry"` // 启用懒惰过期
MaxMemoryUsage int64 `mapstructure:"max-memory-usage" yaml:"max-memory-usage" json:"maxMemoryUsage"` // 最大内存使用量(字节)
}
Expiring 过期缓存配置
type Memory ¶
type Memory struct {
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名
Capacity int `mapstructure:"capacity" yaml:"capacity" json:"capacity" validate:"min=1"` // 缓存容量
DefaultTTL time.Duration `mapstructure:"default-ttl" yaml:"default-ttl" json:"defaultTtl"` // 默认过期时间
CleanupSize int `mapstructure:"cleanup-size" yaml:"cleanup-size" json:"cleanupSize" validate:"min=0"` // 清理大小
MaxSize int `mapstructure:"max-size" yaml:"max-size" json:"maxSize" validate:"min=0"` // 最大大小
}
Memory 内存缓存配置
func (*Memory) WithCapacity ¶
WithCapacity 设置缓存容量
func (*Memory) WithCleanupSize ¶
WithCleanupSize 设置清理大小
func (*Memory) WithDefaultTTL ¶
WithDefaultTTL 设置默认过期时间
func (*Memory) WithModuleName ¶
WithModuleName 设置模块名称
type Redis ¶
type Redis struct {
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名
// 兼容原有配置
Addr string `mapstructure:"addr" yaml:"addr" json:"addr" validate:"url"` // Redis 数据服务器 IP 和端口(兼容旧版)
// 新增增强配置
Addrs []string `mapstructure:"addrs" yaml:"addrs" json:"addrs"` // Redis服务器地址列表(集群模式)
ClientName string `mapstructure:"client-name" yaml:"client-name" json:"clientName"` // 客户端名称,执行 CLIENT SETNAME
Protocol int `mapstructure:"protocol" yaml:"protocol" json:"protocol"` // RESP 协议版本(2 或 3)
Username string `mapstructure:"username" yaml:"username" json:"username"` // 用户名
Password string `mapstructure:"password" yaml:"password" json:"password"` // 连接密码
SentinelUsername string `mapstructure:"sentinel-username" yaml:"sentinel-username" json:"sentinelUsername"` // 哨兵认证用户名
SentinelPassword string `mapstructure:"sentinel-password" yaml:"sentinel-password" json:"sentinelPassword"` // 哨兵认证密码
DB int `mapstructure:"db" yaml:"db" json:"db" validate:"min=0"` // 指定连接的数据库,默认连数据库 0
MaxRetries int `mapstructure:"max-retries" yaml:"max-retries" json:"maxRetries" validate:"min=0"` // 最大重试次数,最小值为 0
MaxRedirects int `mapstructure:"max-redirects" yaml:"max-redirects" json:"maxRedirects" validate:"min=0"` // 集群模式最大重定向次数
PoolSize int `mapstructure:"pool-size" yaml:"pool-size" json:"poolSize" validate:"min=1"` // 连接池大小,最小值为 1
MaxActiveConns int `mapstructure:"max-active-conns" yaml:"max-active-conns" json:"maxActiveConns" validate:"min=0"` // 最大活跃连接数,0 表示不限制
MinIdleConns int `mapstructure:"min-idle-conns" yaml:"min-idle-conns" json:"minIdleConns" validate:"min=0"` // 最小空闲连接数,最小值为 0
MaxIdleConns int `mapstructure:"max-idle-conns" yaml:"max-idle-conns" json:"maxIdleConns" validate:"min=0"` // 最大空闲连接数,最小值为 0
ReadBufferSize int `mapstructure:"read-buffer-size" yaml:"read-buffer-size" json:"readBufferSize" validate:"min=0"` // 读缓冲区大小(字节),默认 32KB
WriteBufferSize int `mapstructure:"write-buffer-size" yaml:"write-buffer-size" json:"writeBufferSize" validate:"min=0"` // 写缓冲区大小(字节),默认 32KB
MaxConnAge time.Duration `mapstructure:"max-conn-age" yaml:"max-conn-age" json:"maxConnAge"` // 连接最大存活时间
DialTimeout time.Duration `mapstructure:"dial-timeout" yaml:"dial-timeout" json:"dialTimeout"` // 连接超时
PoolTimeout time.Duration `mapstructure:"pool-timeout" yaml:"pool-timeout" json:"poolTimeout"` // 连接池超时
IdleTimeout time.Duration `mapstructure:"idle-timeout" yaml:"idle-timeout" json:"idleTimeout"` // 空闲超时
ReadTimeout time.Duration `mapstructure:"read-timeout" yaml:"read-timeout" json:"readTimeout"` // 读取超时
WriteTimeout time.Duration `mapstructure:"write-timeout" yaml:"write-timeout" json:"writeTimeout"` // 写入超时
MinRetryBackoff time.Duration `mapstructure:"min-retry-backoff" yaml:"min-retry-backoff" json:"minRetryBackoff" validate:"min=0"` // 最小重试间隔
MaxRetryBackoff time.Duration `mapstructure:"max-retry-backoff" yaml:"max-retry-backoff" json:"maxRetryBackoff" validate:"min=0"` // 最大重试间隔
ContextTimeoutEnabled bool `mapstructure:"context-timeout-enabled" yaml:"context-timeout-enabled" json:"contextTimeoutEnabled"` // 是否用 context 控制命令超时
ReadOnly bool `mapstructure:"read-only" yaml:"read-only" json:"readOnly"` // 集群模式只读(从节点读)
ClusterMode bool `mapstructure:"cluster-mode" yaml:"cluster-mode" json:"clusterMode"` // 是否集群模式(单地址集群也生效,映射到 go-redis IsClusterMode)
MasterName string `mapstructure:"master-name" yaml:"master-name" json:"masterName"` // 哨兵模式 master 名称,非空时启用哨兵模式
RouteByLatency bool `mapstructure:"route-by-latency" yaml:"route-by-latency" json:"routeByLatency"` // 集群模式按延迟路由(只读从节点)
RouteRandomly bool `mapstructure:"route-randomly" yaml:"route-randomly" json:"routeRandomly"` // 集群模式随机路由(只读从节点)
}
Redis 结构体用于配置 Redis 相关参数(增强版配置) 字段与 go-redis v9 UniversalOptions 对齐,通过 redis.go 映射到 UniversalOptions
func (*Redis) WithClientName ¶
WithClientName 设置客户端名称
func (*Redis) WithClusterMode ¶
WithClusterMode 设置是否集群模式
func (*Redis) WithContextTimeoutEnabled ¶
WithContextTimeoutEnabled 设置是否用 context 控制命令超时
func (*Redis) WithIdleTimeout ¶
WithIdleTimeout 设置空闲超时
func (*Redis) WithMasterName ¶
WithMasterName 设置哨兵模式 master 名称
func (*Redis) WithMaxActiveConns ¶
WithMaxActiveConns 设置最大活跃连接数
func (*Redis) WithMaxConnAge ¶
WithMaxConnAge 设置连接最大存活时间
func (*Redis) WithMaxRedirects ¶
WithMaxRedirects 设置集群最大重定向次数
func (*Redis) WithMaxRetries ¶
WithMaxRetries 设置最大重试次数
func (*Redis) WithMaxRetryBackoff ¶
WithMaxRetryBackoff 设置最大重试间隔
func (*Redis) WithMinIdleConns ¶
WithMinIdleConns 设置最小空闲连接数
func (*Redis) WithMinRetryBackoff ¶
WithMinRetryBackoff 设置最小重试间隔
func (*Redis) WithModuleName ¶
WithModuleName 设置模块名称
func (*Redis) WithPoolTimeout ¶
WithPoolTimeout 设置连接池超时
func (*Redis) WithProtocol ¶
WithProtocol 设置 RESP 协议版本
func (*Redis) WithReadBufferSize ¶
WithReadBufferSize 设置读缓冲区大小
func (*Redis) WithReadOnly ¶
WithReadOnly 设置集群模式只读
func (*Redis) WithReadTimeout ¶
WithReadTimeout 设置读取超时
func (*Redis) WithRouteByLatency ¶
WithRouteByLatency 设置集群模式按延迟路由
func (*Redis) WithRouteRandomly ¶
WithRouteRandomly 设置集群模式随机路由
func (*Redis) WithSentinelPassword ¶
WithSentinelPassword 设置哨兵认证密码
func (*Redis) WithSentinelUsername ¶
WithSentinelUsername 设置哨兵认证用户名
func (*Redis) WithUsername ¶
WithUsername 设置用户名
func (*Redis) WithWriteBufferSize ¶
WithWriteBufferSize 设置写缓冲区大小
type Ristretto ¶
type Ristretto struct {
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名
NumCounters int64 `mapstructure:"num-counters" yaml:"num-counters" json:"numCounters" validate:"min=1"` // 计数器数量,用于跟踪访问频率
MaxCost int64 `mapstructure:"max-cost" yaml:"max-cost" json:"maxCost" validate:"min=1"` // 最大缓存成本
BufferItems int64 `mapstructure:"buffer-items" yaml:"buffer-items" json:"bufferItems" validate:"min=1"` // Get 缓存的大小
Metrics bool `mapstructure:"metrics" yaml:"metrics" json:"metrics"` // 是否启用缓存统计
IgnoreInternalCost bool `mapstructure:"ignore-internal-cost" yaml:"ignore-internal-cost" json:"ignoreInternalCost"` // 是否忽略内部成本计算
KeyToHash bool `mapstructure:"key-to-hash" yaml:"key-to-hash" json:"keyToHash"` // 是否将键转换为哈希
Cost int64 `mapstructure:"cost" yaml:"cost" json:"cost" validate:"min=0"` // 每个条目的默认成本
}
Ristretto缓存配置
func DefaultRistretto ¶
func DefaultRistretto() *Ristretto
DefaultRistretto 返回默认 Ristretto 配置的指针,支持链式调用
func DefaultRistrettoConfig ¶
func DefaultRistrettoConfig() Ristretto
DefaultRistrettoConfig 返回默认 Ristretto 配置
func (*Ristretto) Clone ¶
func (r *Ristretto) Clone() internal.Configurable
Clone 返回 Ristretto 配置的副本
func (*Ristretto) GetBufferItems ¶
GetBufferItems 获取缓冲项数量
func (*Ristretto) GetNumCounters ¶
GetNumCounters 获取计数器数量
func (*Ristretto) IsIgnoreInternalCost ¶
IsIgnoreInternalCost 检查是否忽略内部成本
func (*Ristretto) IsMetricsEnabled ¶
IsMetricsEnabled 检查是否启用指标
type Sharded ¶
type Sharded struct {
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名
ShardCount int `mapstructure:"shard-count" yaml:"shard-count" json:"shardCount" validate:"min=1"` // 分片数量
BaseType CacheType `mapstructure:"base-type" yaml:"base-type" json:"baseType"` // 基础缓存类型
HashFunc string `mapstructure:"hash-func" yaml:"hash-func" json:"hashFunc"` // 哈希函数: fnv, crc32, md5
LoadBalancer string `mapstructure:"load-balancer" yaml:"load-balancer" json:"loadBalancer"` // 负载均衡策略: consistent_hash, round_robin
}
Sharded 分片缓存配置
type TwoLevel ¶
type TwoLevel struct {
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名
L1Type CacheType `mapstructure:"l1-type" yaml:"l1-type" json:"l1Type"` // L1缓存类型
L2Type CacheType `mapstructure:"l2-type" yaml:"l2-type" json:"l2Type"` // L2缓存类型
L1TTL time.Duration `mapstructure:"l1-ttl" yaml:"l1-ttl" json:"l1Ttl"` // L1缓存TTL
L2TTL time.Duration `mapstructure:"l2-ttl" yaml:"l2-ttl" json:"l2Ttl"` // L2缓存TTL
SyncStrategy string `mapstructure:"sync-strategy" yaml:"sync-strategy" json:"syncStrategy"` // 同步策略: write_through, write_back, write_around
L1Size int `mapstructure:"l1-size" yaml:"l1-size" json:"l1Size"` // L1缓存大小
L2Size int `mapstructure:"l2-size" yaml:"l2-size" json:"l2Size"` // L2缓存大小
PromoteThreshold int `mapstructure:"promote-threshold" yaml:"promote-threshold" json:"promoteThreshold"` // 提升阈值
}
TwoLevel 二级缓存配置