Feat: redis driver for cache

This commit is contained in:
HFO4
2019-12-09 15:20:02 +08:00
parent f6da4cc7f0
commit cb61945125
7 changed files with 315 additions and 18 deletions

View File

@@ -13,14 +13,6 @@ type Setting struct {
Value string `gorm:"size:65535"`
}
// settingCache 设置项缓存
var settingCache = make(map[string]string)
// ClearCache 清空设置缓存
func ClearCache() {
settingCache = make(map[string]string)
}
// IsTrueVal 返回设置的值是否为真
func IsTrueVal(val string) bool {
return val == "1" || val == "true"
@@ -32,13 +24,13 @@ func GetSettingByName(name string) string {
// 优先从缓存中查找
cacheKey := "setting_" + name
if optionValue, ok := cache.Store.Get(cacheKey); ok {
if optionValue, ok := cache.Get(cacheKey); ok {
return optionValue.(string)
}
// 尝试数据库中查找
result := DB.Where("name = ?", name).First(&setting)
if result.Error == nil {
_ = cache.Store.Set(cacheKey, setting.Value)
_ = cache.Set(cacheKey, setting.Value)
return setting.Value
}
return ""
@@ -48,13 +40,14 @@ func GetSettingByName(name string) string {
// TODO 其他设置获取也使用缓存
func GetSettingByNames(names []string) map[string]string {
var queryRes []Setting
res := make(map[string]string)
res, miss := cache.GetsSettingByName(names)
DB.Where("name IN (?)", names).Find(&queryRes)
DB.Where("name IN (?)", miss).Find(&queryRes)
for _, setting := range queryRes {
res[setting.Name] = setting.Value
}
_ = cache.SetSettings(res)
return res
}