From 3bf7713aee3d7809d3795d3bab7ec4984f4211de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 27 Aug 2022 16:18:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(cache):=E4=BF=AE=E6=AD=A3=E5=AF=B9=E4=BA=8E?= =?UTF-8?q?=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=BC=93=E5=AD=98=E5=86=85=E5=AE=B9=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E6=AD=A3=E5=B8=B8=E7=9A=84=E4=BF=9D=E5=AD=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/abstract.go | 18 +++++++++++++----- cache/entity.go | 2 +- cache/exists.go | 2 +- cache/search.go | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cache/abstract.go b/cache/abstract.go index 096d282..9d36c3f 100644 --- a/cache/abstract.go +++ b/cache/abstract.go @@ -21,11 +21,19 @@ const ( // 向Redis缓存中保存一个数据 // ! 如果需要长期保存一个数据,那么需要向expires传入0。 func Cache[T interface{}](key string, value *T, expires time.Duration) error { - setCmd := global.RedisConn.B().Set(). - Key(key).Value(rueidis.JSON(value)). - ExSeconds(int64(expires.Seconds())). - Build() - err := global.RedisConn.Do(global.Ctx, setCmd).Error() + var err error + if expires > 0 { + setCmd := global.RedisConn.B().Set(). + Key(key).Value(rueidis.JSON(value)). + ExSeconds(int64(expires.Seconds())). + Build() + err = global.RedisConn.Do(global.Ctx, setCmd).Error() + } else { + setCmd := global.RedisConn.B().Set(). + Key(key).Value(rueidis.JSON(value)). + Build() + err = global.RedisConn.Do(global.Ctx, setCmd).Error() + } return err } diff --git a/cache/entity.go b/cache/entity.go index e24f630..7157a51 100644 --- a/cache/entity.go +++ b/cache/entity.go @@ -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) - err := Cache(entityKey, &instance, 0) + err := Cache(entityKey, &instance, -1) for _, relationName := range relationNames { CacheRelation(relationName, STORE_TYPE_KEY, entityKey) } diff --git a/cache/exists.go b/cache/exists.go index d7eb2a3..d3f8c32 100644 --- a/cache/exists.go +++ b/cache/exists.go @@ -33,7 +33,7 @@ func CheckExists(entityName string, condtions ...string) (bool, error) { existsKey := assembleExistsKey(entityName) identification := assembleExistsIdentification(condtions...) cmd := global.RedisConn.B().Sismember().Key(existsKey).Member(identification).Build() - result, err := global.RedisConn.Do(global.Ctx, cmd).ToBool() + result, err := global.RedisConn.Do(global.Ctx, cmd).AsBool() return result, err } diff --git a/cache/search.go b/cache/search.go index 49c82f5..23fb003 100644 --- a/cache/search.go +++ b/cache/search.go @@ -20,7 +20,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...) - err := Cache(searchKey, &instance, 0) + err := Cache(searchKey, &instance, -1) for _, relationName := range relationNames { CacheRelation(relationName, STORE_TYPE_KEY, searchKey) }