Refactor recommendation page cache handling and configuration
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Removed the `RecommendationPageCacheLanguages` configuration from `AISummarizerConfig` to streamline cache management. - Updated the `companyService` and `tenderService` to utilize the new `InvalidateRecommendedTendersPageCache` method for cache invalidation, enhancing clarity and efficiency. - Refactored the `invalidateRecommendedTendersPageCache` method to eliminate unnecessary context parameters, simplifying the function signature. - Improved the handling of page cache refresh logic by consolidating language handling within the `tenderService`, ensuring consistent behavior across services. - Cleaned up related tests and removed deprecated functions to maintain code quality and readability. This update enhances the maintainability of the recommendation caching system by simplifying configuration and improving cache invalidation logic.
This commit is contained in:
@@ -4,4 +4,5 @@ package company
|
||||
// Implemented by the tender service on the web API; optional on the worker.
|
||||
type RecommendedTendersPageCacheRefresher interface {
|
||||
ScheduleRefreshRecommendedTendersPageCache(companyID string)
|
||||
InvalidateRecommendedTendersPageCache(companyID string)
|
||||
}
|
||||
|
||||
@@ -467,7 +467,7 @@ func (s *companyService) cacheAIRecommendations(ctx context.Context, companyID s
|
||||
return
|
||||
}
|
||||
|
||||
s.invalidateRecommendedTendersPageCache(ctx, companyID)
|
||||
s.invalidateRecommendedTendersPageCache(companyID)
|
||||
s.scheduleRecommendedTendersPageCacheRefresh(companyID)
|
||||
}
|
||||
|
||||
@@ -489,7 +489,7 @@ func (s *companyService) invalidateAIRecommendationCache(ctx context.Context, co
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
s.invalidateRecommendedTendersPageCache(ctx, companyID)
|
||||
s.invalidateRecommendedTendersPageCache(companyID)
|
||||
}
|
||||
|
||||
func (s *companyService) buildOnboardingDocuments(fileIDs []string) (string, error) {
|
||||
|
||||
@@ -1,30 +1,10 @@
|
||||
package company
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const recommendedTendersPageCacheKeyPrefix = "ai:recommendations:page:"
|
||||
|
||||
func recommendedTendersPageCacheKey(companyID, language string) string {
|
||||
return recommendedTendersPageCacheKeyPrefix +
|
||||
strings.TrimSpace(companyID) + ":" +
|
||||
strings.ToLower(strings.TrimSpace(language))
|
||||
}
|
||||
|
||||
func (s *companyService) invalidateRecommendedTendersPageCache(ctx context.Context, companyID string) {
|
||||
if s.redisClient == nil {
|
||||
func (s *companyService) invalidateRecommendedTendersPageCache(companyID string) {
|
||||
if s.pageCacheRefresher == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, language := range s.pageCacheLanguages {
|
||||
lang := strings.ToLower(strings.TrimSpace(language))
|
||||
if lang == "" {
|
||||
continue
|
||||
}
|
||||
_ = s.redisClient.Del(ctx, recommendedTendersPageCacheKey(companyID, lang))
|
||||
}
|
||||
s.pageCacheRefresher.InvalidateRecommendedTendersPageCache(companyID)
|
||||
}
|
||||
|
||||
func (s *companyService) scheduleRecommendedTendersPageCacheRefresh(companyID string) {
|
||||
@@ -38,34 +18,3 @@ func (s *companyService) scheduleRecommendedTendersPageCacheRefresh(companyID st
|
||||
func (s *companyService) SetRecommendedTendersPageCacheRefresher(refresher RecommendedTendersPageCacheRefresher) {
|
||||
s.pageCacheRefresher = refresher
|
||||
}
|
||||
|
||||
// SetPageCacheLanguages configures which language variants are invalidated in Redis.
|
||||
func (s *companyService) SetPageCacheLanguages(languages []string) {
|
||||
s.pageCacheLanguages = normalizePageCacheLanguages("", languages)
|
||||
}
|
||||
|
||||
func normalizePageCacheLanguages(defaultLanguage string, languages []string) []string {
|
||||
seen := make(map[string]struct{})
|
||||
out := make([]string, 0, len(languages)+1)
|
||||
|
||||
add := func(lang string) {
|
||||
lang = strings.ToLower(strings.TrimSpace(lang))
|
||||
if lang == "" {
|
||||
return
|
||||
}
|
||||
if _, ok := seen[lang]; ok {
|
||||
return
|
||||
}
|
||||
seen[lang] = struct{}{}
|
||||
out = append(out, lang)
|
||||
}
|
||||
|
||||
add(defaultLanguage)
|
||||
for _, lang := range languages {
|
||||
add(lang)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
out = append(out, "en")
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -63,7 +63,6 @@ type companyService struct {
|
||||
pageCacheRefresher RecommendedTendersPageCacheRefresher
|
||||
redisClient redis.Client
|
||||
recommendationCacheTTL time.Duration
|
||||
pageCacheLanguages []string
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user