Enhance tender recommendation caching and page refresh logic
continuous-integration/drone/push Build is passing

- Introduced a new `RecommendedTendersPageCacheRefresher` interface to manage the asynchronous refresh of recommendation pages in Redis, improving cache management.
- Updated the `companyService` to support setting page cache languages and refreshing the recommended tenders page cache based on company IDs.
- Enhanced the `tenderService` to build and invalidate recommended tenders page caches, ensuring timely updates and efficient retrieval of cached recommendations.
- Added configuration options for recommendation page cache languages in the `AISummarizerConfig`, allowing for flexible language support.
- Implemented unit tests for the new caching logic and page refresh functionality, ensuring robust validation of the recommendation caching process.

This update significantly improves the efficiency and responsiveness of the tender recommendation service by integrating enhanced caching mechanisms and page refresh capabilities.
This commit is contained in:
Mazyar
2026-07-08 02:05:36 +03:30
parent b28bc23975
commit 0b74e9ad23
11 changed files with 628 additions and 15 deletions
+19 -1
View File
@@ -16,6 +16,7 @@ import (
"tm/pkg/ai_summarizer"
"tm/pkg/logger"
orm "tm/pkg/mongo"
"tm/pkg/redis"
"tm/pkg/response"
"golang.org/x/sync/errgroup"
@@ -89,6 +90,9 @@ type tenderService struct {
logger logger.Logger
aiSummarizerClient AISummarizerClient
aiSummarizerStorage AISummarizerStorage
redisClient redis.Client
pageCacheTTL time.Duration
pageCacheLanguages []string
defaultLanguage string
}
@@ -111,6 +115,9 @@ func NewService(
logger logger.Logger,
aiClient AISummarizerClient,
aiStorage AISummarizerStorage,
redisClient redis.Client,
recommendationPageCacheTTL time.Duration,
recommendationPageCacheLanguages []string,
defaultLanguage string,
) Service {
if strings.TrimSpace(defaultLanguage) == "" {
@@ -125,6 +132,9 @@ func NewService(
logger: logger,
aiSummarizerClient: aiClient,
aiSummarizerStorage: aiStorage,
redisClient: redisClient,
pageCacheTTL: recommendationPageCacheTTL,
pageCacheLanguages: normalizePageCacheLanguages(defaultLanguage, recommendationPageCacheLanguages),
defaultLanguage: strings.ToLower(defaultLanguage),
}
}
@@ -967,6 +977,11 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
"company_ids": companyIDs,
})
lang := s.pickResponseLanguage(form.Language)
if result, ok := s.recommendFromPageCache(ctx, companyID, companyIDs, form, pagination, lang); ok {
return result, nil
}
var (
recommendations []company.RecommendedTenderResponse
err error
@@ -983,6 +998,10 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
return s.emptySearchResponse(pagination), nil
}
if len(companyIDs) == 0 && companyID != "" {
s.ScheduleRefreshRecommendedTendersPageCache(companyID)
}
excluded, excludedByCompany, err := s.loadRecommendationExclusions(ctx, form, companyID, companyIDs)
if err != nil {
if form.ExcludeRejectedTenders {
@@ -995,7 +1014,6 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
return nil, err
}
lang := s.pickResponseLanguage(form.Language)
now := time.Now().Unix()
pageResult, err := s.buildRecommendationPage(
ctx,