diff --git a/internal/controller/v3/dataset.go b/internal/controller/v3/dataset.go index 8a4fe70b..15ddba25 100644 --- a/internal/controller/v3/dataset.go +++ b/internal/controller/v3/dataset.go @@ -42,7 +42,7 @@ func (c *Dataset) AggregatedItem(ctx *fiber.Ctx) error { accountId.Valid = true } - queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResults(ctx.Context(), server, "", ctx.Params("itemId"), accountId) + queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResultsForV3(ctx.Context(), server, "", ctx.Params("itemId"), accountId) if err != nil { return err } @@ -68,7 +68,7 @@ func (c *Dataset) AggregatedStage(ctx *fiber.Ctx) error { accountId.Valid = true } - queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResults(ctx.Context(), server, "stageId", "", accountId) + queryResult, err := c.DropMatrixService.GetMaxAccumulableDropMatrixResultsForV3(ctx.Context(), server, "stageId", "", accountId) if err != nil { return err } diff --git a/internal/model/cache/caches.go b/internal/model/cache/caches.go index 803ecc3e..d36e6fd8 100644 --- a/internal/model/cache/caches.go +++ b/internal/model/cache/caches.go @@ -23,6 +23,7 @@ var ( ItemDropSetByStageIDAndRangeID *cache.Set[[]int] ItemDropSetByStageIdAndTimeRange *cache.Set[[]int] + MaxAccumulableDropMatrixResults *cache.Set[model.DropMatrixQueryResult] ShimMaxAccumulableDropMatrixResults *cache.Set[modelv2.DropMatrixQueryResult] Formula *cache.Singular[json.RawMessage] @@ -121,8 +122,10 @@ func initializeCaches() { SetMap["itemDropSet#server|stageId|startTime|endTime"] = ItemDropSetByStageIdAndTimeRange.Flush // drop_matrix + MaxAccumulableDropMatrixResults = cache.NewSet[model.DropMatrixQueryResult]("maxAccumulableDropMatrixResults#server|sourceCategory") ShimMaxAccumulableDropMatrixResults = cache.NewSet[modelv2.DropMatrixQueryResult]("shimMaxAccumulableDropMatrixResults#server|showClosedZoned") + SetMap["maxAccumulableDropMatrixResults#server|sourceCategory"] = MaxAccumulableDropMatrixResults.Flush SetMap["shimMaxAccumulableDropMatrixResults#server|showClosedZoned"] = ShimMaxAccumulableDropMatrixResults.Flush // formula diff --git a/internal/service/drop_matrix.go b/internal/service/drop_matrix.go index 720e75de..916cdc3a 100644 --- a/internal/service/drop_matrix.go +++ b/internal/service/drop_matrix.go @@ -69,12 +69,13 @@ func NewDropMatrix( } } +// FIXME: this will be used for v3 api // Cache: maxAccumulableDropMatrixResults#server:{server}, 24 hrs, records last modified time -func (s *DropMatrix) GetMaxAccumulableDropMatrixResults( +func (s *DropMatrix) GetMaxAccumulableDropMatrixResultsForV3( ctx context.Context, server string, stageFilterStr string, itemFilterStr string, accountId null.Int, ) (*modelv2.DropMatrixQueryResult, error) { valueFunc := func() (*modelv2.DropMatrixQueryResult, error) { - savedDropMatrixResults, err := s.getMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll) + savedDropMatrixResults, err := s.GetMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll) if err != nil { return nil, err } @@ -105,7 +106,7 @@ func (s *DropMatrix) GetShimMaxAccumulableDropMatrixResults( ctx context.Context, server string, showClosedZones bool, stageFilterStr string, itemFilterStr string, accountId null.Int, ) (*modelv2.DropMatrixQueryResult, error) { valueFunc := func() (*modelv2.DropMatrixQueryResult, error) { - savedDropMatrixResults, err := s.getMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll) + savedDropMatrixResults, err := s.GetMaxAccumulableDropMatrixResults(ctx, server, accountId, constant.SourceCategoryAll) if err != nil { return nil, err } @@ -172,6 +173,14 @@ func (s *DropMatrix) RefreshAllDropMatrixElements(ctx context.Context, server st if err := cache.ShimMaxAccumulableDropMatrixResults.Delete(server + constant.CacheSep + "false"); err != nil { return err } + for _, sourceCategory := range sourceCategories { + if err := cache.MaxAccumulableDropMatrixResults.Delete(server + constant.CacheSep + sourceCategory); err != nil { + return err + } + } + + //TODO: call GetMaxAccumulableDropMatrixResults() here to send results and generation num to LiveHouse + return nil } @@ -188,12 +197,27 @@ func (s *DropMatrix) QueryDropMatrix( } // calc DropMatrixQueryResult for max accumulable timeranges -func (s *DropMatrix) getMaxAccumulableDropMatrixResults(ctx context.Context, server string, accountId null.Int, sourceCategory string) (*model.DropMatrixQueryResult, error) { - dropMatrixElements, err := s.getDropMatrixElements(ctx, server, accountId, sourceCategory) - if err != nil { - return nil, err +// Cache: maxAccumulableDropMatrixResults#server|sourceCategory:{server}|{sourceCategory}, 24 hrs +func (s *DropMatrix) GetMaxAccumulableDropMatrixResults(ctx context.Context, server string, accountId null.Int, sourceCategory string) (*model.DropMatrixQueryResult, error) { + valueFunc := func() (*model.DropMatrixQueryResult, error) { + dropMatrixElements, err := s.getDropMatrixElements(ctx, server, accountId, sourceCategory) + if err != nil { + return nil, err + } + return s.convertDropMatrixElementsToMaxAccumulableDropMatrixQueryResult(ctx, server, dropMatrixElements) + } + + var results model.DropMatrixQueryResult + if !accountId.Valid { + key := server + constant.CacheSep + sourceCategory + _, err := cache.MaxAccumulableDropMatrixResults.MutexGetSet(key, &results, valueFunc, 24*time.Hour) + if err != nil { + return nil, err + } + return &results, nil + } else { + return valueFunc() } - return s.convertDropMatrixElementsToMaxAccumulableDropMatrixQueryResult(ctx, server, dropMatrixElements) } // For global, get elements from DB; For personal, calc elements