From cb492a31f7c97a3c3fafdfe26215b892ce2f4bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Thu, 25 Aug 2022 22:11:15 +0800 Subject: [PATCH] =?UTF-8?q?enhance(cache):=E7=8E=B0=E5=9C=A8=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=BC=93=E5=AD=98=E5=86=85=E5=AE=B9=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E8=A2=AB=E5=85=B3=E8=81=94=E5=88=B0=E5=A4=9A=E4=B8=AA=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/count.go | 6 ++++-- cache/entity.go | 6 ++++-- cache/exists.go | 6 ++++-- cache/search.go | 6 ++++-- service/charge.go | 6 +++--- service/maintenance_fee.go | 4 ++-- service/meter04kv.go | 6 +++--- service/park.go | 8 ++++---- service/region.go | 4 ++-- service/user.go | 18 +++++++++--------- 10 files changed, 39 insertions(+), 31 deletions(-) diff --git a/cache/count.go b/cache/count.go index 5aa675d..3b42827 100644 --- a/cache/count.go +++ b/cache/count.go @@ -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() } diff --git a/cache/entity.go b/cache/entity.go index ee99a4a..31a26e1 100644 --- a/cache/entity.go +++ b/cache/entity.go @@ -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 } diff --git a/cache/exists.go b/cache/exists.go index 8abe9a7..c3d7a38 100644 --- a/cache/exists.go +++ b/cache/exists.go @@ -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() } diff --git a/cache/search.go b/cache/search.go index 2819fa6..e02a89e 100644 --- a/cache/search.go +++ b/cache/search.go @@ -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 } diff --git a/service/charge.go b/service/charge.go index 4b2ceec..0e57c5c 100644 --- a/service/charge.go +++ b/service/charge.go @@ -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 } diff --git a/service/maintenance_fee.go b/service/maintenance_fee.go index e6e73c7..decbb84 100644 --- a/service/maintenance_fee.go +++ b/service/maintenance_fee.go @@ -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 } diff --git a/service/meter04kv.go b/service/meter04kv.go index 8c8cfc9..8bcaeb4 100644 --- a/service/meter04kv.go +++ b/service/meter04kv.go @@ -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 } diff --git a/service/park.go b/service/park.go index 4c57c38..15b1d99 100644 --- a/service/park.go +++ b/service/park.go @@ -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 } diff --git a/service/region.go b/service/region.go index d940ddb..12d9b5f 100644 --- a/service/region.go +++ b/service/region.go @@ -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) } diff --git a/service/user.go b/service/user.go index 958ddd2..f98fc94 100644 --- a/service/user.go +++ b/service/user.go @@ -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