electricity_bill_calc_service/cache/exists.go

50 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cache
import (
"fmt"
"strings"
"time"
"github.com/samber/lo"
)
func assembleExistsKey(entityName string, additional ...string) string {
var keys = make([]string, 0)
keys = append(keys, strings.ToUpper(entityName))
keys = append(keys, additional...)
var b strings.Builder
b.WriteString(TAG_EXISTS)
for _, s := range keys {
fmt.Fprintf(&b, ":%s", s)
}
return b.String()
}
// 缓存模型名称明确的、包含指定ID以及一些附加条件的记录
func CacheExists(relationNames []string, entityName string, conditions ...string) error {
existskey := assembleExistsKey(entityName, conditions...)
err := Cache(existskey, lo.ToPtr(true), 5*time.Minute)
for _, relationName := range relationNames {
CacheRelation(relationName, STORE_TYPE_KEY, existskey)
}
return err
}
// 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记函数在返回false时不保证数据库中相关记录也不存在
func CheckExists(entityName string, condtions ...string) (bool, error) {
existsKey := assembleExistsKey(entityName, condtions...)
return Exists(existsKey)
}
// 从缓存中删除模型名称明确、包含指定ID的全部实体存在标记
func AbolishExists(entityName, id string) error {
pattern := fmt.Sprintf("%s:%s:%s:*", TAG_EXISTS, strings.ToUpper(entityName), id)
return DeleteAll(pattern)
}
// 从缓存中删除指定模型名称的全部存在标记
func AbolishExistsEntity(entityName string) error {
pattern := fmt.Sprintf("%s:%s:*", TAG_EXISTS, strings.ToUpper(entityName))
return DeleteAll(pattern)
}