package cache import ( "electricity_bill_calc/global" "fmt" "strings" ) func assembleExistsKey(entityName string) string { var keys = make([]string, 0) keys = append(keys, strings.ToUpper(entityName)) return CacheKey(TAG_EXISTS, keys...) } func assembleExistsIdentification(additional ...string) string { return strings.Join(additional, ":") } // 缓存模型名称明确的、包含指定ID以及一些附加条件的记录 func CacheExists(relationNames []string, entityName string, conditions ...string) error { existskey := assembleExistsKey(entityName) identification := assembleExistsIdentification(conditions...) cmd := global.RedisConn.B().Sadd().Key(existskey).Member(identification).Build() err := global.RedisConn.Do(global.Ctx, cmd).Error() for _, relationName := range relationNames { CacheRelation(relationName, STORE_TYPE_SET, existskey, identification) } return err } // 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记,函数在返回false时不保证数据库中相关记录也不存在 func CheckExists(entityName string, condtions ...string) (bool, error) { existsKey := assembleExistsKey(entityName) identification := assembleExistsIdentification(condtions...) cmd := global.RedisConn.B().Sismember().Key(existsKey).Member(identification).Build() result, err := global.RedisConn.Do(global.Ctx, cmd).AsBool() return result, err } // 从缓存中删除模型名称明确、包含指定ID的全部实体存在标记 func AbolishExists(entityName, id string) error { existsKey := assembleExistsKey(entityName) pattern := fmt.Sprintf("%s*", id) var ( err error cursor int64 elems = make([]string, 0) sElem []string ) for { cmd := global.RedisConn.B().Sscan().Key(existsKey).Cursor(cursor).Match(pattern).Count(20).Build() result := global.RedisConn.Do(global.Ctx, cmd) cursor, sElem, err = dissembleScan(result) if err != nil { return err } elems = append(elems, sElem...) if cursor == 0 { break } } cmd := global.RedisConn.B().Srem().Key(existsKey).Member(elems...).Build() err = global.RedisConn.Do(global.Ctx, cmd).Error() return err } // 从缓存中删除指定模型名称的全部存在标记 func AbolishExistsEntity(entityName string) error { existskey := assembleExistsKey(entityName) _, err := Delete(existskey) return err }