forked from free-lancers/electricity_bill_calc_service
		
	enhance(repo):改进缓存方法。
This commit is contained in:
		| @@ -1,5 +1,10 @@ | ||||
| package repository | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/cache" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| func _postProcessSingle[T interface{}](instance *T, has bool, err error) (*T, error) { | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @@ -21,3 +26,21 @@ func _postProcessList[T interface{}](instance []*T, has bool, err error) ([]*T, | ||||
| 		return nil, nil | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func cacheData[T interface{}](instance T, category string, key ...string) error { | ||||
| 	var keys = make([]string, 0) | ||||
| 	keys = append(keys, category) | ||||
| 	keys = append(keys, key...) | ||||
| 	cacheKey := cache.CacheKey("cache", keys...) | ||||
| 	if exists, _ := cache.Exists(cacheKey); exists { | ||||
| 		cache.Delete(cacheKey) | ||||
| 	} | ||||
| 	return cache.Cache(cacheKey, &instance, 5*time.Minute) | ||||
| } | ||||
|  | ||||
| func retreiveData[T interface{}](category string, key ...string) (*T, error) { | ||||
| 	var keys = make([]string, 0) | ||||
| 	keys = append(keys, category) | ||||
| 	keys = append(keys, key...) | ||||
| 	return cache.Retreive[T](cache.CacheKey("cache", keys...)) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user