package cache import "time" type Option func(*options) type options struct { ttl time.Duration // 缓存 TTL async bool // 是否异步写缓存 lockEnabled bool // 是否启用分布式锁防击穿 lockTTL time.Duration // 锁过期 prefix string // key 前缀 waitForCache time.Duration // 获取不到锁时等待缓存可用的最大时间(轮询) staleThreshold time.Duration // stale 模式中视为“过期但可用”的阈值 } func defaultOptions() *options { return &options{ ttl: 5 * time.Minute, async: true, lockEnabled: true, lockTTL: 5 * time.Second, prefix: "", waitForCache: 500 * time.Millisecond, staleThreshold: 30 * time.Second, } } // 配置 helper func WithTTL(ttl time.Duration) Option { return func(o *options) { o.ttl = ttl } } func WithAsync(async bool) Option { return func(o *options) { o.async = async } } func WithLock(enabled bool) Option { return func(o *options) { o.lockEnabled = enabled } } func WithLockTTL(ttl time.Duration) Option { return func(o *options) { o.lockTTL = ttl } } func WithPrefix(prefix string) Option { return func(o *options) { o.prefix = prefix } } func WithWaitForCache(d time.Duration) Option { return func(o *options) { o.waitForCache = d } } func WithStaleThreshold(d time.Duration) Option { return func(o *options) { o.staleThreshold = d } }