// Package cache 提供统一缓存接口:FCB_REDIS_ADDR 未配置时自动降级为进程内存实现, // 用于 IP 限流计数与热点配置缓存(需求 ② 的可选 Redis 增强)。 package cache import ( "context" "errors" "time" ) // ErrNotFound 表示键不存在。 var ErrNotFound = errors.New("cache: key 不存在") // Cache 缓存统一接口。 type Cache interface { // Get 读取字符串值;键不存在返回 ErrNotFound。 Get(ctx context.Context, key string) (string, error) // Set 写入字符串值,ttl<=0 表示不过期。 Set(ctx context.Context, key, value string, ttl time.Duration) error // Delete 删除键。 Delete(ctx context.Context, keys ...string) error // Exists 判断键是否存在。 Exists(ctx context.Context, key string) (bool, error) // Incr 原子自增;键不存在时从 0 开始并设置 ttl 窗口(限流固定窗口用)。 Incr(ctx context.Context, key string, ttl time.Duration) (int64, error) // Close 释放底层资源(Redis 连接;内存实现为空操作)。 Close() error } // RedisOptions Redis 连接参数(addr 为空 → 内存实现;db 为 FCB_REDIS_DB 库号)。 type RedisOptions struct { Addr string DB int // 逻辑库号 0-15(cluster 模式忽略) } // New 按配置构造缓存实现:redisAddr 为空 → 内存实现。 func New(ctx context.Context, opt RedisOptions) (Cache, error) { if opt.Addr == "" { return NewMemory(), nil } return NewRedis(ctx, opt.Addr, opt.DB) }