mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: redis driver for cache
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"github.com/HFO4/cloudreve/pkg/cache"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
@@ -42,11 +43,16 @@ type PolicyOption struct {
|
||||
RangeTransferEnabled bool `json:"range_transfer_enabled"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 注册缓存用到的复杂结构
|
||||
gob.Register(Policy{})
|
||||
}
|
||||
|
||||
// GetPolicyByID 用ID获取存储策略
|
||||
func GetPolicyByID(ID interface{}) (Policy, error) {
|
||||
// 尝试读取缓存
|
||||
cacheKey := "policy_" + strconv.Itoa(int(ID.(uint)))
|
||||
if policy, ok := cache.Store.Get(cacheKey); ok {
|
||||
if policy, ok := cache.Get(cacheKey); ok {
|
||||
return policy.(Policy), nil
|
||||
}
|
||||
|
||||
@@ -55,7 +61,7 @@ func GetPolicyByID(ID interface{}) (Policy, error) {
|
||||
|
||||
// 写入缓存
|
||||
if result.Error == nil {
|
||||
_ = cache.Store.Set(cacheKey, policy)
|
||||
_ = cache.Set(cacheKey, policy)
|
||||
}
|
||||
|
||||
return policy, result.Error
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -73,11 +73,11 @@ func TestGetSettingByNames(t *testing.T) {
|
||||
|
||||
//找到其中一个设置时
|
||||
rows = sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
AddRow("siteName", "Cloudreve", "basic")
|
||||
AddRow("siteName2", "Cloudreve", "basic")
|
||||
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
|
||||
settings = GetSettingByNames([]string{"siteName", "siteDes2333"})
|
||||
settings = GetSettingByNames([]string{"siteName2", "siteDes2333"})
|
||||
asserts.Equal(map[string]string{
|
||||
"siteName": "Cloudreve",
|
||||
"siteName2": "Cloudreve",
|
||||
}, settings)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
@@ -87,6 +87,17 @@ func TestGetSettingByNames(t *testing.T) {
|
||||
settings = GetSettingByNames([]string{"siteName2333", "siteDes2333"})
|
||||
asserts.Equal(map[string]string{}, settings)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 一个设置命中缓存
|
||||
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WithArgs("siteDes2").WillReturnRows(sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
AddRow("siteDes2", "Cloudreve2", "basic"))
|
||||
settings = GetSettingByNames([]string{"siteName", "siteDes2"})
|
||||
asserts.Equal(map[string]string{
|
||||
"siteName": "Cloudreve",
|
||||
"siteDes2": "Cloudreve2",
|
||||
}, settings)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
}
|
||||
|
||||
// TestGetSettingByName 测试GetSettingByName
|
||||
|
||||
Reference in New Issue
Block a user