49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package cache
|
|
|
|
import (
|
|
"electricity_bill_calc/global"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func assembleCountKey(entityName string) string {
|
|
var keys = make([]string, 0)
|
|
keys = append(keys, strings.ToUpper(entityName))
|
|
return CacheKey(TAG_COUNT, keys...)
|
|
}
|
|
|
|
func assembleCountIdentification(additional ...string) string {
|
|
return strings.Join(additional, ":")
|
|
}
|
|
|
|
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
|
|
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()
|
|
}
|
|
|
|
// 从缓存中获取模型名称明确的,包含指定条件的实体记录数量
|
|
func RetreiveCount(entityName string, condtions ...string) (int64, error) {
|
|
countKey := assembleCountKey(entityName)
|
|
identification := assembleCountIdentification(condtions...)
|
|
result := global.RedisConn.HGet(global.Ctx, countKey, identification)
|
|
if result.Err() != nil {
|
|
return -1, result.Err()
|
|
}
|
|
count, err := strconv.ParseInt(result.Val(), 10, 64)
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// 删除指定模型名称的数量缓存
|
|
func AbolishCountEntity(entityName string) error {
|
|
countKey := assembleCountKey(entityName)
|
|
result := global.RedisConn.Del(global.Ctx, countKey)
|
|
return result.Err()
|
|
}
|