diff --git a/cache/repository.go b/cache/repository.go index 90d72b6..8242c0d 100644 --- a/cache/repository.go +++ b/cache/repository.go @@ -1,6 +1,8 @@ package cache -import "time" +import ( + "electricity_bill_calc/config" +) func CacheData[T interface{}](instance T, category string, key ...string) error { var keys = make([]string, 0) @@ -10,7 +12,7 @@ func CacheData[T interface{}](instance T, category string, key ...string) error if exists, _ := Exists(cacheKey); exists { Delete(cacheKey) } - return Cache(cacheKey, &instance, 5*time.Minute) + return Cache(cacheKey, &instance, config.ServiceSettings.CacheLifeTime) } func RetreiveData[T interface{}](category string, key ...string) (*T, error) { diff --git a/cache/session.go b/cache/session.go index 4e4b179..501d5b5 100644 --- a/cache/session.go +++ b/cache/session.go @@ -1,10 +1,10 @@ package cache import ( + "electricity_bill_calc/config" "electricity_bill_calc/model" "fmt" "strings" - "time" ) func SessionKey(keys ...string) string { @@ -18,7 +18,7 @@ func SessionKey(keys ...string) string { func CacheSession(session *model.Session) error { key := SessionKey(session.Token) - return Cache(key, session, 2*time.Hour) + return Cache(key, session, config.ServiceSettings.MaxSessionLife) } func RetreiveSession(token string) (*model.Session, error) { diff --git a/config/settings.go b/config/settings.go index 8ee6227..ffbb9eb 100644 --- a/config/settings.go +++ b/config/settings.go @@ -4,7 +4,7 @@ import ( "time" ) -//服务器配置 +// 服务器配置 type ServerSetting struct { RunMode string HttpPort int @@ -12,7 +12,7 @@ type ServerSetting struct { WriteTimeout time.Duration } -//数据库配置 +// 数据库配置 type DatabaseSetting struct { User string Pass string @@ -31,9 +31,11 @@ type RedisSetting struct { type ServiceSetting struct { MaxSessionLife time.Duration + ItemsPageSize uint16 + CacheLifeTime time.Duration } -//定义全局变量 +// 定义全局变量 var ( ServerSettings *ServerSetting DatabaseSettings *DatabaseSetting @@ -41,7 +43,7 @@ var ( ServiceSettings *ServiceSetting ) -//读取配置到全局变量 +// 读取配置到全局变量 func SetupSetting() error { s, err := NewConfigurationFile() if err != nil { diff --git a/settings.yaml b/settings.yaml index 179ae1f..39eaa8f 100644 --- a/settings.yaml +++ b/settings.yaml @@ -17,4 +17,6 @@ Redis: Password: DB: 2 Service: - MaxSessionLife: 2h \ No newline at end of file + MaxSessionLife: 2h + ItemsPageSize: 20 + CacheLifeTime: 5m \ No newline at end of file