enhance(cache):更正缓存函数名称,增加分页检索缓存功能。
This commit is contained in:
parent
73737c3753
commit
71f39c8c2f
2
cache/abstract.go
vendored
2
cache/abstract.go
vendored
|
@ -40,7 +40,7 @@ func Cache[T interface{}](key string, value *T, expires time.Duration) error {
|
|||
}
|
||||
|
||||
// 从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()
|
||||
result := global.Rd.Do(global.Ctx, getCmd)
|
||||
if result.Error() != nil {
|
||||
|
|
4
cache/count.go
vendored
4
cache/count.go
vendored
|
@ -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...)
|
||||
exist, err := Exists(countKey)
|
||||
if err != nil {
|
||||
|
@ -43,7 +43,7 @@ func RetreiveCount(entityName string, condtions ...string) (int64, error) {
|
|||
if !exist {
|
||||
return -1, nil
|
||||
}
|
||||
instance, err := Retreive[_CountRecord](countKey)
|
||||
instance, err := Retrieve[_CountRecord](countKey)
|
||||
if instance != nil && err == nil {
|
||||
return instance.Count, nil
|
||||
} else {
|
||||
|
|
4
cache/entity.go
vendored
4
cache/entity.go
vendored
|
@ -28,9 +28,9 @@ func CacheEntity[T any](instance T, relationNames []string, entityName, id strin
|
|||
}
|
||||
|
||||
// 从缓存中取出模型名称明确的,使用ID进行检索的实体内容。
|
||||
func RetreiveEntity[T any](entityName, id string) (*T, error) {
|
||||
func RetrieveEntity[T any](entityName, id string) (*T, error) {
|
||||
entityKey := assembleEntityKey(entityName, id)
|
||||
instance, err := Retreive[T](entityKey)
|
||||
instance, err := Retrieve[T](entityKey)
|
||||
return instance, err
|
||||
}
|
||||
|
||||
|
|
47
cache/search.go
vendored
47
cache/search.go
vendored
|
@ -29,9 +29,9 @@ func CacheSearch[T any](instance T, relationNames []string, entityName string, c
|
|||
}
|
||||
|
||||
// 从缓存中取得模型名称明确的,使用或者包含非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...)
|
||||
instance, err := Retreive[T](searchKey)
|
||||
instance, err := Retrieve[T](searchKey)
|
||||
return instance, err
|
||||
}
|
||||
|
||||
|
@ -40,3 +40,46 @@ func AbolishSearch(entityName string) error {
|
|||
pattern := fmt.Sprintf("%s:%s:*", TAG_SEARCH, strings.ToUpper(entityName))
|
||||
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
4
cache/session.go
vendored
|
@ -21,9 +21,9 @@ func CacheSession(session *model.Session) error {
|
|||
return Cache(key, session, config.ServiceSettings.MaxSessionLife)
|
||||
}
|
||||
|
||||
func RetreiveSession(token string) (*model.Session, error) {
|
||||
func RetrieveSession(token string) (*model.Session, error) {
|
||||
key := SessionKey(token)
|
||||
return Retreive[model.Session](key)
|
||||
return Retrieve[model.Session](key)
|
||||
}
|
||||
|
||||
func HasSession(token string) (bool, error) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user