enhance(park):为园区管理系列接口增加缓存支持。

This commit is contained in:
徐涛 2022-08-25 17:14:49 +08:00
parent 9653ffb4b1
commit c86235731c

View File

@ -1,9 +1,11 @@
package service
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/exceptions"
"electricity_bill_calc/global"
"electricity_bill_calc/model"
"fmt"
"xorm.io/builder"
)
@ -17,6 +19,7 @@ func (_ParkService) SaveNewPark(park model.Park) error {
if err != nil {
return err
}
cache.AbolishRelation("park")
return nil
}
@ -32,6 +35,8 @@ func (_ParkService) UpdateParkInfo(park *model.Park) error {
return err
}
}
cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park_%s", park.Id))
return nil
}
@ -47,6 +52,8 @@ func (_ParkService) ChangeParkState(uid, pid string, state bool) error {
return err
}
}
cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park_%s", pid))
return nil
}
@ -61,10 +68,15 @@ func (_ParkService) DeletePark(uid, pid string) error {
return err
}
}
cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park_%s", pid))
return nil
}
func (_ParkService) ListAllParkBelongsTo(uid string) ([]model.Park, error) {
if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid); parks != nil {
return *parks, nil
}
parks := make([]model.Park, 0)
err := global.DBConn.
Where(builder.Eq{"user_id": uid}).
@ -73,10 +85,14 @@ func (_ParkService) ListAllParkBelongsTo(uid string) ([]model.Park, error) {
if err != nil {
return make([]model.Park, 0), err
}
cache.CacheSearch(parks, "park", "park", "belong", uid)
return parks, nil
}
func (_ParkService) FetchParkDetail(pid string) (*model.Park, error) {
if park, _ := cache.RetreiveEntity[model.Park]("park", pid); park != nil {
return park, nil
}
var park = &model.Park{}
has, err := global.DBConn.ID(pid).NoAutoCondition().Get(park)
if err != nil {
@ -85,14 +101,25 @@ func (_ParkService) FetchParkDetail(pid string) (*model.Park, error) {
if !has {
return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。")
}
cache.CacheEntity(park, "park", "park", pid)
return park, nil
}
func (_ParkService) EnsurePark(uid, pid string) (bool, error) {
return global.DBConn.Table(&model.Park{}).Where(builder.Eq{"user_id": uid, "id": pid}).Exist()
if has, _ := cache.CheckExists("park", pid, uid); has {
return has, nil
}
has, err := global.DBConn.Table(&model.Park{}).Where(builder.Eq{"user_id": uid, "id": pid}).Exist()
if has {
cache.CacheExists("park", "park", pid, uid)
}
return has, err
}
func (_ParkService) AllParkIds(uid string) ([]string, error) {
if ids, _ := cache.RetreiveSearch[[]string]("park", "belong", uid); ids != nil {
return *ids, nil
}
var ids = make([]string, 0)
err := global.DBConn.
Table(new(model.Park)).
@ -102,5 +129,6 @@ func (_ParkService) AllParkIds(uid string) ([]string, error) {
if err != nil {
return make([]string, 0), err
}
cache.CacheSearch(ids, "park", "park", "belong", uid)
return ids, nil
}