enhance(cache):现在一个缓存内容可以被关联到多个主题了。

This commit is contained in:
徐涛
2022-08-25 22:11:15 +08:00
parent 455213f744
commit cb492a31f7
10 changed files with 39 additions and 31 deletions

6
cache/count.go vendored
View File

@@ -17,11 +17,13 @@ func assembleCountIdentification(additional ...string) string {
}
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
func CacheCount(relationName, entityName string, count int64, conditions ...string) error {
func CacheCount(relationNames []string, entityName string, count int64, conditions ...string) error {
countKey := assembleCountKey(entityName)
identification := assembleCountIdentification(conditions...)
result := global.RedisConn.HSet(global.Ctx, countKey, map[string]interface{}{identification: count})
CacheRelation(relationName, STORE_TYPE_HASH, countKey, identification)
for _, relationName := range relationNames {
CacheRelation(relationName, STORE_TYPE_HASH, countKey, identification)
}
return result.Err()
}

6
cache/entity.go vendored
View File

@@ -18,10 +18,12 @@ func assembleEntityKey(entityName, id string) string {
}
// 缓存模型名称明确的使用ID进行检索的实体内容。
func CacheEntity[T any](instance T, relationName, entityName, id string) error {
func CacheEntity[T any](instance T, relationNames []string, entityName, id string) error {
entityKey := assembleEntityKey(entityName, id)
err := Cache(entityKey, &instance, 0)
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
for _, relationName := range relationNames {
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
}
return err
}

6
cache/exists.go vendored
View File

@@ -18,11 +18,13 @@ func assembleExistsIdentification(additional ...string) string {
}
// 缓存模型名称明确的、包含指定ID以及一些附加条件的记录
func CacheExists(relationName, entityName string, conditions ...string) error {
func CacheExists(relationNames []string, entityName string, conditions ...string) error {
existskey := assembleExistsKey(entityName)
identification := assembleExistsIdentification(conditions...)
result := global.RedisConn.SAdd(global.Ctx, existskey, identification)
CacheRelation(relationName, STORE_TYPE_SET, existskey, identification)
for _, relationName := range relationNames {
CacheRelation(relationName, STORE_TYPE_SET, existskey, identification)
}
return result.Err()
}

6
cache/search.go vendored
View File

@@ -19,10 +19,12 @@ func assembleSearchKey(entityName string, additional ...string) string {
}
// 缓存模型名称明确的使用或者包含非ID检索条件的实体内容。
func CacheSearch[T any](instance T, relationName, entityName string, conditions ...string) error {
func CacheSearch[T any](instance T, relationNames []string, entityName string, conditions ...string) error {
searchKey := assembleSearchKey(entityName, conditions...)
err := Cache(searchKey, &instance, 0)
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
for _, relationName := range relationNames {
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
}
return err
}