fix(cache):修正对于过期时间的处理,使缓存内容可以正常的保存。

This commit is contained in:
徐涛 2022-08-27 16:18:41 +08:00
parent 306ef3e1d9
commit 3bf7713aee
4 changed files with 16 additions and 8 deletions

10
cache/abstract.go vendored
View File

@ -21,11 +21,19 @@ const (
// 向Redis缓存中保存一个数据
// ! 如果需要长期保存一个数据那么需要向expires传入0。
func Cache[T interface{}](key string, value *T, expires time.Duration) 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()
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
}

2
cache/entity.go vendored
View File

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

2
cache/exists.go vendored
View File

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

2
cache/search.go vendored
View File

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