项目开发中只需定义一个常量映射表,后期需要更改键名直接通过映射表统一修改,从而无需每个文件去一一修改,降低代码出错率的同时确保可维护性。
Go示例
// Resdis键名映射表
package cache
const (
UserNameKey = "user:"
UserOnlineKey = "user:online:"
EmailCaptchaKey = "email:captcha:"
EmailRegisterCaptchaKey = "email:captcha:register:"
EmailForgetCaptchaKey = "email:captcha:forget:"
RequestCount = "request_count:"
)
// 在其他包中调用
package demo
ctx := context.Background()
onlineUser, err := bootstrap.GetRedis().Keys(ctx, cache.UserOnlineKey+"*").Result()