enhance(cache):现在一个缓存内容可以被关联到多个主题了。
This commit is contained in:
parent
455213f744
commit
cb492a31f7
6
cache/count.go
vendored
6
cache/count.go
vendored
|
@ -17,11 +17,13 @@ func assembleCountIdentification(additional ...string) string {
|
|||
}
|
||||
|
||||
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
|
||||
func CacheCount(relationName, entityName string, count int64, conditions ...string) error {
|
||||
func CacheCount(relationNames []string, 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)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_HASH, countKey, identification)
|
||||
}
|
||||
return result.Err()
|
||||
}
|
||||
|
||||
|
|
6
cache/entity.go
vendored
6
cache/entity.go
vendored
|
@ -18,10 +18,12 @@ func assembleEntityKey(entityName, id string) string {
|
|||
}
|
||||
|
||||
// 缓存模型名称明确的,使用ID进行检索的实体内容。
|
||||
func CacheEntity[T any](instance T, relationName, entityName, id string) error {
|
||||
func CacheEntity[T any](instance T, relationNames []string, entityName, id string) error {
|
||||
entityKey := assembleEntityKey(entityName, id)
|
||||
err := Cache(entityKey, &instance, 0)
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
6
cache/exists.go
vendored
6
cache/exists.go
vendored
|
@ -18,11 +18,13 @@ func assembleExistsIdentification(additional ...string) string {
|
|||
}
|
||||
|
||||
// 缓存模型名称明确的、包含指定ID以及一些附加条件的记录
|
||||
func CacheExists(relationName, entityName string, conditions ...string) error {
|
||||
func CacheExists(relationNames []string, 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)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_SET, existskey, identification)
|
||||
}
|
||||
return result.Err()
|
||||
}
|
||||
|
||||
|
|
6
cache/search.go
vendored
6
cache/search.go
vendored
|
@ -19,10 +19,12 @@ func assembleSearchKey(entityName string, additional ...string) string {
|
|||
}
|
||||
|
||||
// 缓存模型名称明确的,使用或者包含非ID检索条件的实体内容。
|
||||
func CacheSearch[T any](instance T, relationName, entityName string, conditions ...string) error {
|
||||
func CacheSearch[T any](instance T, relationNames []string, entityName string, conditions ...string) error {
|
||||
searchKey := assembleSearchKey(entityName, conditions...)
|
||||
err := Cache(searchKey, &instance, 0)
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -215,7 +215,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
|||
if err != nil {
|
||||
return nil, -1, err
|
||||
}
|
||||
cache.CacheCount("charge", "charge_with_name", total, condition...)
|
||||
cache.CacheCount([]string{"charge"}, "charge_with_name", total, condition...)
|
||||
}
|
||||
charges := make([]model.ChargeWithName, 0)
|
||||
if cachedCharges, _ := cache.RetreiveSearch[[]model.ChargeWithName]("charge_with_name", condition...); cachedCharges != nil {
|
||||
|
@ -228,7 +228,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
|||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
||||
NoAutoCondition().
|
||||
Find(&charges)
|
||||
cache.CacheSearch(charges, "charge", "charge_with_name", condition...)
|
||||
cache.CacheSearch(charges, []string{"charge"}, "charge_with_name", condition...)
|
||||
return charges, total, err
|
||||
}
|
||||
|
||||
|
@ -257,6 +257,6 @@ func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) {
|
|||
return acc
|
||||
}
|
||||
}, veryBlankTime)
|
||||
cache.CacheSearch(lastValid, "charge", "last_valid_charge", uid)
|
||||
cache.CacheSearch(lastValid, []string{"charge"}, "last_valid_charge", uid)
|
||||
return lastValid, nil
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ func (_MaintenanceFeeService) ListMaintenanceFees(pid []string) ([]model.Mainten
|
|||
if err != nil {
|
||||
return make([]model.MaintenanceFee, 0), err
|
||||
}
|
||||
cache.CacheSearch(fees, "maintenance_fee", "maintenance_fee", pid...)
|
||||
cache.CacheSearch(fees, []string{"maintenance_fee", "park"}, "maintenance_fee", pid...)
|
||||
return fees, nil
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ func (_MaintenanceFeeService) EnsureFeeBelongs(uid, mid string) (bool, error) {
|
|||
return false, exceptions.NewNotFoundError("指定维护费所属园区未找到。")
|
||||
}
|
||||
if park[0].UserId == uid {
|
||||
cache.CacheExists("maintenance_fee", "maintenance_fee", mid, uid)
|
||||
cache.CacheExists([]string{"maintenance_fee", "park"}, "maintenance_fee", mid, uid)
|
||||
}
|
||||
return park[0].UserId == uid, nil
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ func (_Meter04kVService) ListMeterDetail(park, keyword string, page int) ([]mode
|
|||
if err != nil {
|
||||
return make([]model.Meter04KV, 0), -1, err
|
||||
}
|
||||
cache.CacheCount("meter_04kv", "meter_04kv", total, condition...)
|
||||
cache.CacheCount([]string{"meter_04kv", "park"}, "meter_04kv", total, condition...)
|
||||
}
|
||||
var meters = make([]model.Meter04KV, 0)
|
||||
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||
|
@ -54,7 +54,7 @@ func (_Meter04kVService) ListMeterDetail(park, keyword string, page int) ([]mode
|
|||
Where(cond).
|
||||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
||||
Find(&meters)
|
||||
cache.CacheSearch(meters, "meter_04kv", "meter_04kv", condition...)
|
||||
cache.CacheSearch(meters, []string{"meter_04kv", "park"}, "meter_04kv", condition...)
|
||||
return meters, total, err
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ func (_Meter04kVService) Get04kVMeterDetail(park, code string) (*model.Meter04KV
|
|||
if !has {
|
||||
return nil, nil
|
||||
}
|
||||
cache.CacheEntity(meter, fmt.Sprintf("meter_04kv_%s_%s", park, code), "meter_04kv", fmt.Sprintf("%s_%s", park, code))
|
||||
cache.CacheEntity(meter, []string{fmt.Sprintf("meter_04kv_%s_%s", park, code), "park"}, "meter_04kv", fmt.Sprintf("%s_%s", park, code))
|
||||
return meter, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ func (_ParkService) ListAllParkBelongsTo(uid string) ([]model.Park, error) {
|
|||
if err != nil {
|
||||
return make([]model.Park, 0), err
|
||||
}
|
||||
cache.CacheSearch(parks, "park", "park", "belong", uid)
|
||||
cache.CacheSearch(parks, []string{"park"}, "park", "belong", uid)
|
||||
return parks, nil
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,7 @@ func (_ParkService) FetchParkDetail(pid string) (*model.Park, error) {
|
|||
if !has {
|
||||
return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。")
|
||||
}
|
||||
cache.CacheEntity(park, "park", "park", pid)
|
||||
cache.CacheEntity(park, []string{"park"}, "park", pid)
|
||||
return park, nil
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ func (_ParkService) EnsurePark(uid, pid string) (bool, error) {
|
|||
}
|
||||
has, err := global.DBConn.Table(&model.Park{}).Where(builder.Eq{"user_id": uid, "id": pid}).Exist()
|
||||
if has {
|
||||
cache.CacheExists("park", "park", pid, uid)
|
||||
cache.CacheExists([]string{"park"}, "park", pid, uid)
|
||||
}
|
||||
return has, err
|
||||
}
|
||||
|
@ -129,6 +129,6 @@ func (_ParkService) AllParkIds(uid string) ([]string, error) {
|
|||
if err != nil {
|
||||
return make([]string, 0), err
|
||||
}
|
||||
cache.CacheSearch(ids, "park", "park", "belong", uid)
|
||||
cache.CacheSearch(ids, []string{"park"}, "park", "belong", uid)
|
||||
return ids, nil
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ func (_RegionService) FetchSubRegions(parent string) ([]model.Region, error) {
|
|||
if err != nil {
|
||||
return make([]model.Region, 0), err
|
||||
}
|
||||
cache.CacheSearch(regions, "region", "region", "parent", parent)
|
||||
cache.CacheSearch(regions, []string{"region"}, "region", "parent", parent)
|
||||
return regions, err
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ func (_RegionService) fetchRegion(code string) (*model.Region, error) {
|
|||
region := new(model.Region)
|
||||
has, err := global.DBConn.ID(code).NoAutoCondition().Get(region)
|
||||
if has {
|
||||
cache.CacheSearch(region, "region", "region", code)
|
||||
cache.CacheSearch(region, []string{"region"}, "region", code)
|
||||
}
|
||||
return _postProcessSingle(region, has, err)
|
||||
}
|
||||
|
|
|
@ -162,7 +162,7 @@ func (_UserService) IsUserExists(uid string) (bool, error) {
|
|||
}
|
||||
has, err := global.DBConn.ID(uid).Exist(&model.User{})
|
||||
if has {
|
||||
cache.CacheExists(fmt.Sprintf("user_%s", uid), "user", uid)
|
||||
cache.CacheExists([]string{fmt.Sprintf("user_%s", uid)}, "user", uid)
|
||||
}
|
||||
return has, err
|
||||
}
|
||||
|
@ -173,7 +173,7 @@ func (_UserService) IsUsernameExists(username string) (bool, error) {
|
|||
}
|
||||
has, err := global.DBConn.Where(builder.Eq{"username": username}).Exist(&model.User{})
|
||||
if has {
|
||||
cache.CacheExists("user", "user", username)
|
||||
cache.CacheExists([]string{"user"}, "user", username)
|
||||
}
|
||||
return has, err
|
||||
}
|
||||
|
@ -262,7 +262,7 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU
|
|||
if err != nil {
|
||||
return make([]model.JoinedUserDetail, 0), err
|
||||
}
|
||||
cache.CacheSearch(users, "user", "join_user_detail", keyword, strconv.Itoa(limit))
|
||||
cache.CacheSearch(users, []string{"user"}, "join_user_detail", keyword, strconv.Itoa(limit))
|
||||
return users, nil
|
||||
}
|
||||
|
||||
|
@ -273,7 +273,7 @@ func (_UserService) findUserByUsername(username string) (*model.User, error) {
|
|||
user := new(model.User)
|
||||
has, err := global.DBConn.Where(builder.Eq{"username": username}).NoAutoCondition().Get(user)
|
||||
if has {
|
||||
cache.CacheSearch(*user, "user", "user", username)
|
||||
cache.CacheSearch(*user, []string{"user"}, "user", username)
|
||||
}
|
||||
return _postProcessSingle(user, has, err)
|
||||
}
|
||||
|
@ -285,7 +285,7 @@ func (_UserService) retreiveUserDetail(uid string) (*model.UserDetail, error) {
|
|||
user := new(model.UserDetail)
|
||||
has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user)
|
||||
if has {
|
||||
cache.CacheEntity(*user, fmt.Sprintf("user_%s", uid), "user_detail", uid)
|
||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user_%s", uid)}, "user_detail", uid)
|
||||
}
|
||||
return _postProcessSingle(user, has, err)
|
||||
}
|
||||
|
@ -298,7 +298,7 @@ func (_UserService) findUserByID(uid string) (*model.User, error) {
|
|||
user := new(model.User)
|
||||
has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user)
|
||||
if has {
|
||||
cache.CacheEntity(*user, fmt.Sprintf("user_%s", uid), "user", uid)
|
||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user_%s", uid)}, "user", uid)
|
||||
}
|
||||
return _postProcessSingle(user, has, err)
|
||||
}
|
||||
|
@ -341,7 +341,7 @@ func (_UserService) ListUserDetail(keyword string, userType int, userState *bool
|
|||
if err != nil {
|
||||
return nil, -1, err
|
||||
}
|
||||
cache.CacheCount("user", "join_user_detail", total, cacheConditions...)
|
||||
cache.CacheCount([]string{"user"}, "join_user_detail", total, cacheConditions...)
|
||||
}
|
||||
users := make([]model.JoinedUserDetail, 0)
|
||||
if cachedUsers, _ := cache.RetreiveSearch[[]model.JoinedUserDetail]("join_user_detail", cacheConditions...); cachedUsers != nil {
|
||||
|
@ -353,7 +353,7 @@ func (_UserService) ListUserDetail(keyword string, userType int, userState *bool
|
|||
Where(cond).
|
||||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
||||
Find(&users)
|
||||
cache.CacheSearch(users, "user", "join_user_detail", cacheConditions...)
|
||||
cache.CacheSearch(users, []string{"user"}, "join_user_detail", cacheConditions...)
|
||||
return users, total, err
|
||||
}
|
||||
|
||||
|
@ -369,7 +369,7 @@ func (_UserService) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, er
|
|||
NoAutoCondition().
|
||||
Get(user)
|
||||
if has {
|
||||
cache.CacheEntity(*user, fmt.Sprintf("user_%s", uid), "full_join_user_detail", uid)
|
||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user_%s", uid)}, "full_join_user_detail", uid)
|
||||
return user, nil
|
||||
}
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in New Issue
Block a user