enhance(repo):改进缓存方法。

This commit is contained in:
徐涛 2022-08-12 11:39:19 +08:00
parent b8c44c9b1e
commit 5234ed88de

View File

@ -1,5 +1,10 @@
package repository package repository
import (
"electricity_bill_calc/cache"
"time"
)
func _postProcessSingle[T interface{}](instance *T, has bool, err error) (*T, error) { func _postProcessSingle[T interface{}](instance *T, has bool, err error) (*T, error) {
if err != nil { if err != nil {
return nil, err return nil, err
@ -21,3 +26,21 @@ func _postProcessList[T interface{}](instance []*T, has bool, err error) ([]*T,
return nil, nil 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...))
}