package cache import ( "electricity_bill_calc/global" "electricity_bill_calc/tools" "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(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() } // 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记,函数在返回false时不保证数据库中相关记录也不存在 func CheckExists(entityName string, condtions ...string) (bool, error) { existsKey := assembleExistsKey(entityName) identification := assembleExistsIdentification(condtions...) result := global.RedisConn.SIsMember(global.Ctx, existsKey, identification) return result.Val(), result.Err() } // 从缓存中删除模型名称明确、包含指定ID的全部实体存在标记 func AbolishExists(entityName, id string) error { existsKey := assembleExistsKey(entityName) pattern := fmt.Sprintf("%s*", id) var ( cursor uint64 elems = make([]string, 0) ) for { k, cursor, err := global.RedisConn.SScan(global.Ctx, existsKey, cursor, pattern, 20).Result() if err != nil { return err } elems = append(elems, k...) if cursor == 0 { break } } pipeline := global.RedisConn.Pipeline() pipeline.SRem(global.Ctx, existsKey, tools.ConvertSliceToInterfaceSlice(elems)...) _, err := pipeline.Exec(global.Ctx) return err } // 从缓存中删除指定模型名称的全部存在标记 func AbolishExistsEntity(entityName string) error { existskey := assembleExistsKey(entityName) result := global.RedisConn.Del(global.Ctx, existskey) return result.Err() }