forked from free-lancers/electricity_bill_calc_service
		
	enhacne(setting):增加缓存生命期配置。
This commit is contained in:
		
							
								
								
									
										6
									
								
								cache/repository.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								cache/repository.go
									
									
									
									
										vendored
									
									
								
							| @@ -1,6 +1,8 @@ | |||||||
| package cache | package cache | ||||||
|  |  | ||||||
| import "time" | import ( | ||||||
|  | 	"electricity_bill_calc/config" | ||||||
|  | ) | ||||||
|  |  | ||||||
| func CacheData[T interface{}](instance T, category string, key ...string) error { | func CacheData[T interface{}](instance T, category string, key ...string) error { | ||||||
| 	var keys = make([]string, 0) | 	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 { | 	if exists, _ := Exists(cacheKey); exists { | ||||||
| 		Delete(cacheKey) | 		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) { | func RetreiveData[T interface{}](category string, key ...string) (*T, error) { | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								cache/session.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								cache/session.go
									
									
									
									
										vendored
									
									
								
							| @@ -1,10 +1,10 @@ | |||||||
| package cache | package cache | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"electricity_bill_calc/config" | ||||||
| 	"electricity_bill_calc/model" | 	"electricity_bill_calc/model" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func SessionKey(keys ...string) string { | func SessionKey(keys ...string) string { | ||||||
| @@ -18,7 +18,7 @@ func SessionKey(keys ...string) string { | |||||||
|  |  | ||||||
| func CacheSession(session *model.Session) error { | func CacheSession(session *model.Session) error { | ||||||
| 	key := SessionKey(session.Token) | 	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) { | func RetreiveSession(token string) (*model.Session, error) { | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| //服务器配置 | // 服务器配置 | ||||||
| type ServerSetting struct { | type ServerSetting struct { | ||||||
| 	RunMode      string | 	RunMode      string | ||||||
| 	HttpPort     int | 	HttpPort     int | ||||||
| @@ -12,7 +12,7 @@ type ServerSetting struct { | |||||||
| 	WriteTimeout time.Duration | 	WriteTimeout time.Duration | ||||||
| } | } | ||||||
|  |  | ||||||
| //数据库配置 | // 数据库配置 | ||||||
| type DatabaseSetting struct { | type DatabaseSetting struct { | ||||||
| 	User         string | 	User         string | ||||||
| 	Pass         string | 	Pass         string | ||||||
| @@ -31,9 +31,11 @@ type RedisSetting struct { | |||||||
|  |  | ||||||
| type ServiceSetting struct { | type ServiceSetting struct { | ||||||
| 	MaxSessionLife time.Duration | 	MaxSessionLife time.Duration | ||||||
|  | 	ItemsPageSize  uint16 | ||||||
|  | 	CacheLifeTime  time.Duration | ||||||
| } | } | ||||||
|  |  | ||||||
| //定义全局变量 | // 定义全局变量 | ||||||
| var ( | var ( | ||||||
| 	ServerSettings   *ServerSetting | 	ServerSettings   *ServerSetting | ||||||
| 	DatabaseSettings *DatabaseSetting | 	DatabaseSettings *DatabaseSetting | ||||||
| @@ -41,7 +43,7 @@ var ( | |||||||
| 	ServiceSettings  *ServiceSetting | 	ServiceSettings  *ServiceSetting | ||||||
| ) | ) | ||||||
|  |  | ||||||
| //读取配置到全局变量 | // 读取配置到全局变量 | ||||||
| func SetupSetting() error { | func SetupSetting() error { | ||||||
| 	s, err := NewConfigurationFile() | 	s, err := NewConfigurationFile() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|   | |||||||
| @@ -17,4 +17,6 @@ Redis: | |||||||
|   Password: |   Password: | ||||||
|   DB: 2 |   DB: 2 | ||||||
| Service: | Service: | ||||||
|   MaxSessionLife: 2h |   MaxSessionLife: 2h | ||||||
|  |   ItemsPageSize: 20 | ||||||
|  |   CacheLifeTime: 5m | ||||||
		Reference in New Issue
	
	Block a user