forked from free-lancers/electricity_bill_calc_service
feat(cache):实验完成缓存控制机制。
This commit is contained in:
13
cache/abstract.go
vendored
13
cache/abstract.go
vendored
@@ -11,11 +11,12 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
TAG_ENTITY = "ENTITY"
|
||||
TAG_COUNT = "COUNT"
|
||||
TAG_SEARCH = "SEARCH"
|
||||
TAG_EXISTS = "CHECK"
|
||||
TAG_SESSION = "SESSION"
|
||||
TAG_ENTITY = "ENTITY"
|
||||
TAG_COUNT = "COUNT"
|
||||
TAG_SEARCH = "SEARCH"
|
||||
TAG_EXISTS = "CHECK"
|
||||
TAG_SESSION = "SESSION"
|
||||
TAG_RELATION = "RELATION"
|
||||
)
|
||||
|
||||
// 向Redis缓存中保存一个数据
|
||||
@@ -26,7 +27,7 @@ func Cache[T interface{}](key string, value *T, expires time.Duration) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := global.RedisConn.SetEX(global.Ctx, key, serializedValue, expires)
|
||||
cmd := global.RedisConn.Set(global.Ctx, key, serializedValue, expires)
|
||||
return cmd.Err()
|
||||
}
|
||||
|
||||
|
10
cache/count.go
vendored
10
cache/count.go
vendored
@@ -2,7 +2,6 @@ package cache
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/global"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -14,18 +13,15 @@ func assembleCountKey(entityName string) string {
|
||||
}
|
||||
|
||||
func assembleCountIdentification(additional ...string) string {
|
||||
var b strings.Builder
|
||||
for _, s := range additional {
|
||||
fmt.Fprintf(&b, ":%s", s)
|
||||
}
|
||||
return b.String()
|
||||
return strings.Join(additional, ":")
|
||||
}
|
||||
|
||||
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
|
||||
func CacheCount(entityName string, count int64, conditions ...string) error {
|
||||
func CacheCount(relationName, 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)
|
||||
return result.Err()
|
||||
}
|
||||
|
||||
|
5
cache/entity.go
vendored
5
cache/entity.go
vendored
@@ -8,9 +8,9 @@ import (
|
||||
|
||||
func assembleEntityKey(entityName, id string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, TAG_ENTITY)
|
||||
keys = append(keys, strings.ToUpper(entityName), id)
|
||||
var b strings.Builder
|
||||
b.WriteString(TAG_ENTITY)
|
||||
for _, s := range keys {
|
||||
fmt.Fprintf(&b, ":%s", s)
|
||||
}
|
||||
@@ -18,9 +18,10 @@ func assembleEntityKey(entityName, id string) string {
|
||||
}
|
||||
|
||||
// 缓存模型名称明确的,使用ID进行检索的实体内容。
|
||||
func CacheEntity[T any](instance T, entityName, id string) error {
|
||||
func CacheEntity[T any](instance T, relationName, entityName, id string) error {
|
||||
entityKey := assembleEntityKey(entityName, id)
|
||||
err := Cache(entityKey, &instance, 0)
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
|
||||
return err
|
||||
}
|
||||
|
||||
|
9
cache/exists.go
vendored
9
cache/exists.go
vendored
@@ -14,18 +14,15 @@ func assembleExistsKey(entityName string) string {
|
||||
}
|
||||
|
||||
func assembleExistsIdentification(additional ...string) string {
|
||||
var b strings.Builder
|
||||
for _, s := range additional {
|
||||
fmt.Fprintf(&b, ":%s", s)
|
||||
}
|
||||
return b.String()
|
||||
return strings.Join(additional, ":")
|
||||
}
|
||||
|
||||
// 缓存模型名称明确的、包含指定ID以及一些附加条件的记录
|
||||
func CacheExists(entityName string, conditions ...string) error {
|
||||
func CacheExists(relationName, 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)
|
||||
return result.Err()
|
||||
}
|
||||
|
||||
|
59
cache/relation.go
vendored
Normal file
59
cache/relation.go
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/tools"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
STORE_TYPE_KEY = "KEY"
|
||||
STORE_TYPE_SET = "SET"
|
||||
STORE_TYPE_HASH = "HASH"
|
||||
)
|
||||
|
||||
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 {
|
||||
var identity = make([]string, 0)
|
||||
identity = append(identity, storeType, key)
|
||||
identity = append(identity, field...)
|
||||
return strings.Join(identity, ";")
|
||||
}
|
||||
|
||||
// 向缓存中保存与指定关联名称相关联的键的名称以及键的类型和子字段的组成。
|
||||
func CacheRelation(relationName, storeType, key string, field ...string) error {
|
||||
relationKey := assembleRelationKey(relationName)
|
||||
relationIdentity := assembleRelationIdentity(storeType, key, field...)
|
||||
result := global.RedisConn.SAdd(global.Ctx, relationKey, relationIdentity)
|
||||
return result.Err()
|
||||
}
|
||||
|
||||
// 从缓存中清理指定的关联键
|
||||
func AbolishRelation(relationName string) error {
|
||||
relationKey := assembleRelationKey(relationName)
|
||||
result := global.RedisConn.SMembers(global.Ctx, relationKey)
|
||||
if result.Err() != nil {
|
||||
return result.Err()
|
||||
}
|
||||
relationItems := result.Val()
|
||||
pipeline := global.RedisConn.Pipeline()
|
||||
for _, item := range relationItems {
|
||||
separated := strings.Split(item, ";")
|
||||
switch separated[0] {
|
||||
case STORE_TYPE_KEY:
|
||||
pipeline.Del(global.Ctx, separated[1])
|
||||
case STORE_TYPE_HASH:
|
||||
pipeline.HDel(global.Ctx, separated[1], separated[2:]...)
|
||||
case STORE_TYPE_SET:
|
||||
pipeline.SRem(global.Ctx, separated[1], tools.ConvertSliceToInterfaceSlice(separated[2:])...)
|
||||
}
|
||||
}
|
||||
pipeline.Del(global.Ctx, relationKey)
|
||||
_, err := pipeline.Exec(global.Ctx)
|
||||
return err
|
||||
}
|
5
cache/search.go
vendored
5
cache/search.go
vendored
@@ -8,10 +8,10 @@ import (
|
||||
|
||||
func assembleSearchKey(entityName string, additional ...string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, TAG_SEARCH)
|
||||
keys = append(keys, strings.ToUpper(entityName))
|
||||
keys = append(keys, additional...)
|
||||
var b strings.Builder
|
||||
b.WriteString(TAG_SEARCH)
|
||||
for _, s := range keys {
|
||||
fmt.Fprintf(&b, ":%s", s)
|
||||
}
|
||||
@@ -19,9 +19,10 @@ func assembleSearchKey(entityName string, additional ...string) string {
|
||||
}
|
||||
|
||||
// 缓存模型名称明确的,使用或者包含非ID检索条件的实体内容。
|
||||
func CacheSearch[T any](instance T, entityName string, conditions ...string) error {
|
||||
func CacheSearch[T any](instance T, relationName, entityName string, conditions ...string) error {
|
||||
searchKey := assembleSearchKey(entityName, conditions...)
|
||||
err := Cache(searchKey, &instance, 0)
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
|
||||
return err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user