enhance(cache):更正缓存函数名称,增加分页检索缓存功能。

This commit is contained in:
徐涛 2023-06-01 10:05:29 +08:00
parent 73737c3753
commit 71f39c8c2f
5 changed files with 52 additions and 9 deletions

2
cache/abstract.go vendored
View File

@ -40,7 +40,7 @@ func Cache[T interface{}](key string, value *T, expires time.Duration) error {
} }
// 从Redis缓存中获取一个数据 // 从Redis缓存中获取一个数据
func Retreive[T interface{}](key string) (*T, error) { func Retrieve[T interface{}](key string) (*T, error) {
getCmd := global.Rd.B().Get().Key(key).Build() getCmd := global.Rd.B().Get().Key(key).Build()
result := global.Rd.Do(global.Ctx, getCmd) result := global.Rd.Do(global.Ctx, getCmd)
if result.Error() != nil { if result.Error() != nil {

4
cache/count.go vendored
View File

@ -34,7 +34,7 @@ func CacheCount(relationNames []string, entityName string, count int64, conditio
} }
// 从缓存中获取模型名称明确的,包含指定条件的实体记录数量 // 从缓存中获取模型名称明确的,包含指定条件的实体记录数量
func RetreiveCount(entityName string, condtions ...string) (int64, error) { func RetrieveCount(entityName string, condtions ...string) (int64, error) {
countKey := assembleCountKey(entityName, condtions...) countKey := assembleCountKey(entityName, condtions...)
exist, err := Exists(countKey) exist, err := Exists(countKey)
if err != nil { if err != nil {
@ -43,7 +43,7 @@ func RetreiveCount(entityName string, condtions ...string) (int64, error) {
if !exist { if !exist {
return -1, nil return -1, nil
} }
instance, err := Retreive[_CountRecord](countKey) instance, err := Retrieve[_CountRecord](countKey)
if instance != nil && err == nil { if instance != nil && err == nil {
return instance.Count, nil return instance.Count, nil
} else { } else {

4
cache/entity.go vendored
View File

@ -28,9 +28,9 @@ func CacheEntity[T any](instance T, relationNames []string, entityName, id strin
} }
// 从缓存中取出模型名称明确的使用ID进行检索的实体内容。 // 从缓存中取出模型名称明确的使用ID进行检索的实体内容。
func RetreiveEntity[T any](entityName, id string) (*T, error) { func RetrieveEntity[T any](entityName, id string) (*T, error) {
entityKey := assembleEntityKey(entityName, id) entityKey := assembleEntityKey(entityName, id)
instance, err := Retreive[T](entityKey) instance, err := Retrieve[T](entityKey)
return instance, err return instance, err
} }

47
cache/search.go vendored
View File

@ -29,9 +29,9 @@ func CacheSearch[T any](instance T, relationNames []string, entityName string, c
} }
// 从缓存中取得模型名称明确的使用或者包含非ID检索条件的实体内容。 // 从缓存中取得模型名称明确的使用或者包含非ID检索条件的实体内容。
func RetreiveSearch[T any](entityName string, conditions ...string) (*T, error) { func RetrieveSearch[T any](entityName string, conditions ...string) (*T, error) {
searchKey := assembleSearchKey(entityName, conditions...) searchKey := assembleSearchKey(entityName, conditions...)
instance, err := Retreive[T](searchKey) instance, err := Retrieve[T](searchKey)
return instance, err return instance, err
} }
@ -40,3 +40,46 @@ func AbolishSearch(entityName string) error {
pattern := fmt.Sprintf("%s:%s:*", TAG_SEARCH, strings.ToUpper(entityName)) pattern := fmt.Sprintf("%s:%s:*", TAG_SEARCH, strings.ToUpper(entityName))
return DeleteAll(pattern) return DeleteAll(pattern)
} }
// 向缓存中保存指定模型名称的分页检索结果,会同时采用`CacheCount`中的方法保存检索结果的总数量。
func CachePagedSearch[T any](instance T, total int64, relationNames []string, entityName string, conditions ...string) error {
searchKey := assembleSearchKey(entityName, conditions...)
countKey := assembleCountKey(entityName, conditions...)
err := Cache(searchKey, &instance, 5*time.Minute)
if err != nil {
return err
}
cacheInstance := &_CountRecord{Count: total}
err = Cache(countKey, cacheInstance, 5*time.Minute)
for _, relationName := range relationNames {
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
CacheRelation(relationName, STORE_TYPE_KEY, countKey)
}
return err
}
// 从缓存中获取指定模型名称的分页检索结果,会同时采用`RetrieveCount`中的方法获取检索结果的总数量。
func RetrievePagedSearch[T any](entityName string, conditions ...string) (*T, int64, error) {
searchKey := assembleSearchKey(entityName, conditions...)
countKey := assembleCountKey(entityName, conditions...)
instance, err := Retrieve[T](searchKey)
if err != nil {
return nil, -1, err
}
count, err := Retrieve[_CountRecord](countKey)
if err != nil {
return nil, -1, err
}
return instance, count.Count, nil
}
// 从缓存中删除指定模型名称的分页检索结果,会同时采用`AbolishCountEntity`中的方法删除检索结果的总数量。
func AbolishPagedSearch(entityName string) error {
pattern := fmt.Sprintf("%s:%s:*", TAG_SEARCH, strings.ToUpper(entityName))
err := DeleteAll(pattern)
if err != nil {
return err
}
pattern = fmt.Sprintf("%s:%s:*", TAG_COUNT, strings.ToUpper(entityName))
return DeleteAll(pattern)
}

4
cache/session.go vendored
View File

@ -21,9 +21,9 @@ func CacheSession(session *model.Session) error {
return Cache(key, session, config.ServiceSettings.MaxSessionLife) return Cache(key, session, config.ServiceSettings.MaxSessionLife)
} }
func RetreiveSession(token string) (*model.Session, error) { func RetrieveSession(token string) (*model.Session, error) {
key := SessionKey(token) key := SessionKey(token)
return Retreive[model.Session](key) return Retrieve[model.Session](key)
} }
func HasSession(token string) (bool, error) { func HasSession(token string) (bool, error) {