fix(cache):修正对于过期时间的处理,使缓存内容可以正常的保存。
This commit is contained in:
parent
306ef3e1d9
commit
3bf7713aee
18
cache/abstract.go
vendored
18
cache/abstract.go
vendored
|
@ -21,11 +21,19 @@ const (
|
||||||
// 向Redis缓存中保存一个数据
|
// 向Redis缓存中保存一个数据
|
||||||
// ! 如果需要长期保存一个数据,那么需要向expires传入0。
|
// ! 如果需要长期保存一个数据,那么需要向expires传入0。
|
||||||
func Cache[T interface{}](key string, value *T, expires time.Duration) error {
|
func Cache[T interface{}](key string, value *T, expires time.Duration) error {
|
||||||
setCmd := global.RedisConn.B().Set().
|
var err error
|
||||||
Key(key).Value(rueidis.JSON(value)).
|
if expires > 0 {
|
||||||
ExSeconds(int64(expires.Seconds())).
|
setCmd := global.RedisConn.B().Set().
|
||||||
Build()
|
Key(key).Value(rueidis.JSON(value)).
|
||||||
err := global.RedisConn.Do(global.Ctx, setCmd).Error()
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
cache/entity.go
vendored
2
cache/entity.go
vendored
|
@ -19,7 +19,7 @@ func assembleEntityKey(entityName, id string) string {
|
||||||
// 缓存模型名称明确的,使用ID进行检索的实体内容。
|
// 缓存模型名称明确的,使用ID进行检索的实体内容。
|
||||||
func CacheEntity[T any](instance T, relationNames []string, entityName, id string) error {
|
func CacheEntity[T any](instance T, relationNames []string, entityName, id string) error {
|
||||||
entityKey := assembleEntityKey(entityName, id)
|
entityKey := assembleEntityKey(entityName, id)
|
||||||
err := Cache(entityKey, &instance, 0)
|
err := Cache(entityKey, &instance, -1)
|
||||||
for _, relationName := range relationNames {
|
for _, relationName := range relationNames {
|
||||||
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
|
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
|
||||||
}
|
}
|
||||||
|
|
2
cache/exists.go
vendored
2
cache/exists.go
vendored
|
@ -33,7 +33,7 @@ func CheckExists(entityName string, condtions ...string) (bool, error) {
|
||||||
existsKey := assembleExistsKey(entityName)
|
existsKey := assembleExistsKey(entityName)
|
||||||
identification := assembleExistsIdentification(condtions...)
|
identification := assembleExistsIdentification(condtions...)
|
||||||
cmd := global.RedisConn.B().Sismember().Key(existsKey).Member(identification).Build()
|
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
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
cache/search.go
vendored
2
cache/search.go
vendored
|
@ -20,7 +20,7 @@ func assembleSearchKey(entityName string, additional ...string) string {
|
||||||
// 缓存模型名称明确的,使用或者包含非ID检索条件的实体内容。
|
// 缓存模型名称明确的,使用或者包含非ID检索条件的实体内容。
|
||||||
func CacheSearch[T any](instance T, relationNames []string, entityName string, conditions ...string) error {
|
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, 0)
|
err := Cache(searchKey, &instance, -1)
|
||||||
for _, relationName := range relationNames {
|
for _, relationName := range relationNames {
|
||||||
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
|
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user