enhance(cache):放开缓存管理中对于构造缓存键的方法。
This commit is contained in:
parent
fb388c53c7
commit
24f2c26d86
6
cache/count.go
vendored
6
cache/count.go
vendored
|
@ -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
|
||||
|
|
8
cache/entity.go
vendored
8
cache/entity.go
vendored
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
6
cache/exists.go
vendored
6
cache/exists.go
vendored
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
10
cache/relation.go
vendored
10
cache/relation.go
vendored
|
@ -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 {
|
||||
|
|
14
cache/search.go
vendored
14
cache/search.go
vendored
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user