From 24f2c26d86db2b98fa2726314be8bea3eb564896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 16 Jun 2023 10:43:06 +0800 Subject: [PATCH] =?UTF-8?q?enhance(cache):=E6=94=BE=E5=BC=80=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=AE=A1=E7=90=86=E4=B8=AD=E5=AF=B9=E4=BA=8E=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E7=BC=93=E5=AD=98=E9=94=AE=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/count.go | 6 +++--- cache/entity.go | 8 ++++---- cache/exists.go | 6 +++--- cache/relation.go | 10 +++++----- cache/search.go | 14 +++++++------- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cache/count.go b/cache/count.go index 5b8b65b..78844b9 100644 --- a/cache/count.go +++ b/cache/count.go @@ -10,7 +10,7 @@ type _CountRecord struct { Count int64 } -func assembleCountKey(entityName string, additional ...string) string { +func AssembleCountKey(entityName string, additional ...string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(entityName)) keys = append(keys, additional...) @@ -24,7 +24,7 @@ func assembleCountKey(entityName string, additional ...string) string { // 向缓存中缓存模型名称明确的包含指定条件的实体记录数量 func CacheCount(relationNames []string, entityName string, count int64, conditions ...string) error { - countKey := assembleCountKey(entityName, conditions...) + countKey := AssembleCountKey(entityName, conditions...) cacheInstance := &_CountRecord{Count: count} err := Cache(countKey, cacheInstance, 5*time.Minute) for _, relationName := range relationNames { @@ -35,7 +35,7 @@ func CacheCount(relationNames []string, entityName string, count int64, conditio // 从缓存中获取模型名称明确的,包含指定条件的实体记录数量 func RetrieveCount(entityName string, condtions ...string) (int64, error) { - countKey := assembleCountKey(entityName, condtions...) + countKey := AssembleCountKey(entityName, condtions...) exist, err := Exists(countKey) if err != nil { return -1, err diff --git a/cache/entity.go b/cache/entity.go index db1ff52..203133b 100644 --- a/cache/entity.go +++ b/cache/entity.go @@ -6,7 +6,7 @@ import ( "time" ) -func assembleEntityKey(entityName, id string) string { +func AssembleEntityKey(entityName, id string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(entityName), id) var b strings.Builder @@ -19,7 +19,7 @@ func assembleEntityKey(entityName, id string) string { // 缓存模型名称明确的,使用ID进行检索的实体内容。 func CacheEntity[T any](instance T, relationNames []string, entityName, id string) error { - entityKey := assembleEntityKey(entityName, id) + entityKey := AssembleEntityKey(entityName, id) err := Cache(entityKey, &instance, 5*time.Minute) for _, relationName := range relationNames { CacheRelation(relationName, STORE_TYPE_KEY, entityKey) @@ -29,14 +29,14 @@ func CacheEntity[T any](instance T, relationNames []string, entityName, id strin // 从缓存中取出模型名称明确的,使用ID进行检索的实体内容。 func RetrieveEntity[T any](entityName, id string) (*T, error) { - entityKey := assembleEntityKey(entityName, id) + entityKey := AssembleEntityKey(entityName, id) instance, err := Retrieve[T](entityKey) return instance, err } // 精确的从缓存中删除指定模型名称、指定ID的实体内容。 func AbolishSpecificEntity(entityName, id string) (bool, error) { - entityKey := assembleEntityKey(entityName, id) + entityKey := AssembleEntityKey(entityName, id) return Delete(entityKey) } diff --git a/cache/exists.go b/cache/exists.go index cae7ad7..150c0c7 100644 --- a/cache/exists.go +++ b/cache/exists.go @@ -8,7 +8,7 @@ import ( "github.com/samber/lo" ) -func assembleExistsKey(entityName string, additional ...string) string { +func AssembleExistsKey(entityName string, additional ...string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(entityName)) keys = append(keys, additional...) @@ -22,7 +22,7 @@ func assembleExistsKey(entityName string, additional ...string) string { // 缓存模型名称明确的、包含指定ID以及一些附加条件的记录 func CacheExists(relationNames []string, entityName string, conditions ...string) error { - existskey := assembleExistsKey(entityName, conditions...) + existskey := AssembleExistsKey(entityName, conditions...) err := Cache(existskey, lo.ToPtr(true), 5*time.Minute) for _, relationName := range relationNames { CacheRelation(relationName, STORE_TYPE_KEY, existskey) @@ -32,7 +32,7 @@ func CacheExists(relationNames []string, entityName string, conditions ...string // 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记,函数在返回false时不保证数据库中相关记录也不存在 func CheckExists(entityName string, condtions ...string) (bool, error) { - existsKey := assembleExistsKey(entityName, condtions...) + existsKey := AssembleExistsKey(entityName, condtions...) return Exists(existsKey) } diff --git a/cache/relation.go b/cache/relation.go index 03cf099..5b89af3 100644 --- a/cache/relation.go +++ b/cache/relation.go @@ -15,13 +15,13 @@ const ( STORE_TYPE_HASH = "HASH" ) -func assembleRelationKey(relationName string) string { +func AssembleRelationKey(relationName string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(relationName)) return CacheKey(TAG_RELATION, keys...) } -func assembleRelationIdentity(storeType, key string, field ...string) string { +func AssembleRelationIdentity(storeType, key string, field ...string) string { var identity = make([]string, 0) identity = append(identity, storeType, key) identity = append(identity, field...) @@ -30,8 +30,8 @@ func assembleRelationIdentity(storeType, key string, field ...string) string { // 向缓存中保存与指定关联名称相关联的键的名称以及键的类型和子字段的组成。 func CacheRelation(relationName, storeType, key string, field ...string) error { - relationKey := assembleRelationKey(relationName) - relationIdentity := assembleRelationIdentity(storeType, key, field...) + relationKey := AssembleRelationKey(relationName) + relationIdentity := AssembleRelationIdentity(storeType, key, field...) cmd := global.Rd.B().Sadd().Key(relationKey).Member(relationIdentity).Build() result := global.Rd.Do(global.Ctx, cmd) return result.Error() @@ -39,7 +39,7 @@ func CacheRelation(relationName, storeType, key string, field ...string) error { // 从缓存中清理指定的关联键 func AbolishRelation(relationName string) error { - relationKey := assembleRelationKey(relationName) + relationKey := AssembleRelationKey(relationName) cmd := global.Rd.B().Smembers().Key(relationKey).Build() relationItems, err := global.Rd.Do(global.Ctx, cmd).AsStrSlice() if err != nil { diff --git a/cache/search.go b/cache/search.go index fb4b8df..42ffcc9 100644 --- a/cache/search.go +++ b/cache/search.go @@ -11,7 +11,7 @@ import ( var log = logger.Named("Cache") -func assembleSearchKey(entityName string, additional ...string) string { +func AssembleSearchKey(entityName string, additional ...string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(entityName)) keys = append(keys, additional...) @@ -25,7 +25,7 @@ func assembleSearchKey(entityName string, additional ...string) string { // 缓存模型名称明确的,使用或者包含非ID检索条件的实体内容。 func CacheSearch[T any](instance T, relationNames []string, entityName string, conditions ...string) error { - searchKey := assembleSearchKey(entityName, conditions...) + searchKey := AssembleSearchKey(entityName, conditions...) err := Cache(searchKey, &instance, 5*time.Minute) for _, relationName := range relationNames { CacheRelation(relationName, STORE_TYPE_KEY, searchKey) @@ -35,7 +35,7 @@ func CacheSearch[T any](instance T, relationNames []string, entityName string, c // 从缓存中取得模型名称明确的,使用或者包含非ID检索条件的实体内容。 func RetrieveSearch[T any](entityName string, conditions ...string) (*T, error) { - searchKey := assembleSearchKey(entityName, conditions...) + searchKey := AssembleSearchKey(entityName, conditions...) instance, err := Retrieve[T](searchKey) return instance, err } @@ -48,8 +48,8 @@ func AbolishSearch(entityName string) error { // 向缓存中保存指定模型名称的分页检索结果,会同时采用`CacheCount`中的方法保存检索结果的总数量。 func CachePagedSearch[T any](instance T, total int64, relationNames []string, entityName string, conditions ...string) error { - searchKey := assembleSearchKey(entityName, conditions...) - countKey := assembleCountKey(entityName, conditions...) + searchKey := AssembleSearchKey(entityName, conditions...) + countKey := AssembleCountKey(entityName, conditions...) err := Cache(searchKey, &instance, 5*time.Minute) if err != nil { return err @@ -65,8 +65,8 @@ func CachePagedSearch[T any](instance T, total int64, relationNames []string, en // 从缓存中获取指定模型名称的分页检索结果,会同时采用`RetrieveCount`中的方法获取检索结果的总数量。 func RetrievePagedSearch[T any](entityName string, conditions ...string) (*T, int64, error) { - searchKey := assembleSearchKey(entityName, conditions...) - countKey := assembleCountKey(entityName, conditions...) + searchKey := AssembleSearchKey(entityName, conditions...) + countKey := AssembleCountKey(entityName, conditions...) instance, err := Retrieve[T](searchKey) if err != nil { return nil, -1, err