enhance(cache):改用普通Key记录查询数量。
This commit is contained in:
parent
bd1033ac47
commit
2ea8443409
45
cache/count.go
vendored
45
cache/count.go
vendored
|
@ -1,49 +1,44 @@
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"electricity_bill_calc/global"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func assembleCountKey(entityName string) string {
|
func assembleCountKey(entityName string, additional ...string) string {
|
||||||
var keys = make([]string, 0)
|
var keys = make([]string, 0)
|
||||||
keys = append(keys, strings.ToUpper(entityName))
|
keys = append(keys, strings.ToUpper(entityName))
|
||||||
return CacheKey(TAG_COUNT, keys...)
|
keys = append(keys, additional...)
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(TAG_COUNT)
|
||||||
|
for _, s := range keys {
|
||||||
|
fmt.Fprintf(&b, ":%s", s)
|
||||||
}
|
}
|
||||||
|
return b.String()
|
||||||
func assembleCountIdentification(additional ...string) string {
|
|
||||||
return strings.Join(additional, ":")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
|
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
|
||||||
func CacheCount(relationNames []string, entityName string, count int64, conditions ...string) error {
|
func CacheCount(relationNames []string, entityName string, count int64, conditions ...string) error {
|
||||||
countKey := assembleCountKey(entityName)
|
countKey := assembleCountKey(entityName, conditions...)
|
||||||
identification := assembleCountIdentification(conditions...)
|
err := Cache(countKey, lo.ToPtr(count), 5*time.Minute)
|
||||||
cmd := global.RedisConn.B().Hset().Key(countKey).FieldValue().FieldValue(identification, strconv.FormatInt(count, 10)).Build()
|
|
||||||
result := global.RedisConn.Do(global.Ctx, cmd)
|
|
||||||
for _, relationName := range relationNames {
|
for _, relationName := range relationNames {
|
||||||
CacheRelation(relationName, STORE_TYPE_HASH, countKey, identification)
|
CacheRelation(relationName, STORE_TYPE_HASH, countKey)
|
||||||
}
|
}
|
||||||
return result.Error()
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从缓存中获取模型名称明确的,包含指定条件的实体记录数量
|
// 从缓存中获取模型名称明确的,包含指定条件的实体记录数量
|
||||||
func RetreiveCount(entityName string, condtions ...string) (int64, error) {
|
func RetreiveCount(entityName string, condtions ...string) (int64, error) {
|
||||||
countKey := assembleCountKey(entityName)
|
countKey := assembleCountKey(entityName, condtions...)
|
||||||
identification := assembleCountIdentification(condtions...)
|
instance, err := Retreive[int64](countKey)
|
||||||
cmd := global.RedisConn.B().Hget().Key(countKey).Field(identification).Build()
|
return *instance, err
|
||||||
result, err := global.RedisConn.Do(global.Ctx, cmd).AsInt64()
|
|
||||||
if err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除指定模型名称的数量缓存
|
// 删除指定模型名称的数量缓存
|
||||||
func AbolishCountEntity(entityName string) error {
|
func AbolishCountEntity(entityName string) error {
|
||||||
countKey := assembleCountKey(entityName)
|
pattern := fmt.Sprintf("%s:%s:*", TAG_COUNT, strings.ToUpper(entityName))
|
||||||
cmd := global.RedisConn.B().Del().Key(countKey).Build()
|
return DeleteAll(pattern)
|
||||||
err := global.RedisConn.Do(global.Ctx, cmd).Error()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user