From 71f39c8c2fd767f25725df7861e2489aa6a8f168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Thu, 1 Jun 2023 10:05:29 +0800 Subject: [PATCH] =?UTF-8?q?enhance(cache):=E6=9B=B4=E6=AD=A3=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=87=BD=E6=95=B0=E5=90=8D=E7=A7=B0=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=88=86=E9=A1=B5=E6=A3=80=E7=B4=A2=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/abstract.go | 2 +- cache/count.go | 4 ++-- cache/entity.go | 4 ++-- cache/search.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- cache/session.go | 4 ++-- 5 files changed, 52 insertions(+), 9 deletions(-) diff --git a/cache/abstract.go b/cache/abstract.go index cedab1f..8d24c69 100644 --- a/cache/abstract.go +++ b/cache/abstract.go @@ -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 { diff --git a/cache/count.go b/cache/count.go index b1baa54..5b8b65b 100644 --- a/cache/count.go +++ b/cache/count.go @@ -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 { diff --git a/cache/entity.go b/cache/entity.go index c9d4eb8..db1ff52 100644 --- a/cache/entity.go +++ b/cache/entity.go @@ -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 } diff --git a/cache/search.go b/cache/search.go index 7468bd4..d83bcd6 100644 --- a/cache/search.go +++ b/cache/search.go @@ -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) +} diff --git a/cache/session.go b/cache/session.go index 30000d7..2dab0d2 100644 --- a/cache/session.go +++ b/cache/session.go @@ -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) {