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) }