Enhance dashboard service with widget caching and improved query handling
- Introduced a new `widgetCache` structure to manage caching for various dashboard widgets, improving performance and reducing redundant data retrieval. - Updated the `ClosingSoon` and `Trend` methods in the service to utilize the new caching mechanism, ensuring efficient data access and reducing load on the database. - Enhanced the `NoticeTypes` and `Countries` methods to implement caching, further optimizing the dashboard's responsiveness. - Added a new `searchListCache` implementation in the tender service to cache search results, improving the efficiency of tender searches and reducing database load. - Implemented robust error handling and logging for cache operations, ensuring better visibility into potential issues. This update significantly enhances the performance and scalability of the dashboard and tender services by integrating effective caching strategies, leading to improved user experience and resource management.
This commit is contained in:
+175
-68
@@ -60,19 +60,30 @@ type service struct {
|
||||
statisticsMu sync.Mutex
|
||||
statistics map[int]cacheEntry[*StatisticsReportResponse]
|
||||
statisticsGroup singleflight.Group
|
||||
trendCache *widgetCache[*TrendResponse]
|
||||
countriesCache *widgetCache[*CountriesResponse]
|
||||
noticeTypesCache *widgetCache[*NoticeTypesResponse]
|
||||
closingSoonCache *widgetCache[*ClosingSoonResponse]
|
||||
recentCache *widgetCache[*RecentResponse]
|
||||
}
|
||||
|
||||
// NewService creates a dashboard service.
|
||||
func NewService(repo Repository, log logger.Logger, redisClient redis.Client) Service {
|
||||
s := &service{
|
||||
repo: repo,
|
||||
logger: log,
|
||||
redis: redisClient,
|
||||
summary: make(map[string]cacheEntry[*SummaryResponse]),
|
||||
statistics: make(map[int]cacheEntry[*StatisticsReportResponse]),
|
||||
repo: repo,
|
||||
logger: log,
|
||||
redis: redisClient,
|
||||
summary: make(map[string]cacheEntry[*SummaryResponse]),
|
||||
statistics: make(map[int]cacheEntry[*StatisticsReportResponse]),
|
||||
trendCache: newWidgetCache[*TrendResponse](redisClient, log, "trend"),
|
||||
countriesCache: newWidgetCache[*CountriesResponse](redisClient, log, "countries"),
|
||||
noticeTypesCache: newWidgetCache[*NoticeTypesResponse](redisClient, log, "notice-types"),
|
||||
closingSoonCache: newWidgetCache[*ClosingSoonResponse](redisClient, log, "closing-soon"),
|
||||
recentCache: newWidgetCache[*RecentResponse](redisClient, log, "recent"),
|
||||
}
|
||||
go s.warmStatisticsCache()
|
||||
go s.warmSummaryCache()
|
||||
go s.warmWidgetCaches()
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -129,102 +140,134 @@ func (s *service) Summary(ctx context.Context, query SummaryQuery) (*SummaryResp
|
||||
func (s *service) Trend(ctx context.Context, query TrendQuery) (*TrendResponse, error) {
|
||||
days := trendDays(query.Days)
|
||||
metric := normalizeTrendMetric(query.Metric)
|
||||
startUnix := trendStartUnix(days)
|
||||
cacheKey := fmt.Sprintf("%d:%s", days, metric)
|
||||
|
||||
s.logger.Info("Fetching dashboard trend", map[string]interface{}{
|
||||
"days": days,
|
||||
"metric": metric,
|
||||
})
|
||||
return s.trendCache.Get(ctx, cacheKey, func(loadCtx context.Context) (*TrendResponse, error) {
|
||||
startUnix := trendStartUnix(days)
|
||||
|
||||
counts, err := s.repo.Trend(ctx, days, metric, startUnix)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard trend", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
s.logger.Info("Fetching dashboard trend", map[string]interface{}{
|
||||
"days": days,
|
||||
"metric": metric,
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard trend: %w", err)
|
||||
}
|
||||
|
||||
series := fillTrendSeries(days, counts)
|
||||
counts, err := s.repo.Trend(loadCtx, days, metric, startUnix)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard trend", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard trend: %w", err)
|
||||
}
|
||||
|
||||
return &TrendResponse{
|
||||
Metric: metric,
|
||||
Days: days,
|
||||
Series: series,
|
||||
}, nil
|
||||
return &TrendResponse{
|
||||
Metric: metric,
|
||||
Days: days,
|
||||
Series: fillTrendSeries(days, counts),
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) Countries(ctx context.Context, query CountriesQuery) (*CountriesResponse, error) {
|
||||
limit := countriesLimit(query.Limit)
|
||||
cacheKey := strconv.Itoa(limit)
|
||||
|
||||
s.logger.Info("Fetching dashboard countries", map[string]interface{}{
|
||||
"limit": limit,
|
||||
})
|
||||
|
||||
out, err := s.repo.Countries(ctx, limit)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard countries", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
return s.countriesCache.Get(ctx, cacheKey, func(loadCtx context.Context) (*CountriesResponse, error) {
|
||||
s.logger.Info("Fetching dashboard countries", map[string]interface{}{
|
||||
"limit": limit,
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard countries: %w", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
out, err := s.repo.Countries(loadCtx, limit)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard countries", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard countries: %w", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) NoticeTypes(ctx context.Context) (*NoticeTypesResponse, error) {
|
||||
s.logger.Info("Fetching dashboard notice types", nil)
|
||||
return s.noticeTypesCache.Get(ctx, "all", func(loadCtx context.Context) (*NoticeTypesResponse, error) {
|
||||
s.logger.Info("Fetching dashboard notice types", nil)
|
||||
|
||||
out, err := s.repo.NoticeTypes(ctx)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard notice types", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard notice types: %w", err)
|
||||
}
|
||||
out, err := s.repo.NoticeTypes(loadCtx)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard notice types", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard notice types: %w", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
return out, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) ClosingSoon(ctx context.Context, query ClosingSoonQuery) (*ClosingSoonResponse, error) {
|
||||
limit := listLimit(query.Limit)
|
||||
windowHours := closingWindowHours(query.Window)
|
||||
windowSec := int64(windowHours) * 3600
|
||||
cacheKey := fmt.Sprintf("%d:%d", limit, windowSec)
|
||||
|
||||
s.logger.Info("Fetching dashboard closing soon", map[string]interface{}{
|
||||
"limit": limit,
|
||||
"window": windowHours,
|
||||
})
|
||||
|
||||
items, err := s.repo.ClosingSoon(ctx, limit, windowSec)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard closing soon", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
return s.closingSoonCache.Get(ctx, cacheKey, func(loadCtx context.Context) (*ClosingSoonResponse, error) {
|
||||
s.logger.Info("Fetching dashboard closing soon", map[string]interface{}{
|
||||
"limit": limit,
|
||||
"window": windowHours,
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard closing soon: %w", err)
|
||||
}
|
||||
|
||||
return &ClosingSoonResponse{Items: items}, nil
|
||||
items, err := s.repo.ClosingSoon(loadCtx, limit, windowSec)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard closing soon", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard closing soon: %w", err)
|
||||
}
|
||||
|
||||
return &ClosingSoonResponse{Items: items}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) Recent(ctx context.Context, query RecentQuery) (*RecentResponse, error) {
|
||||
limit := listLimit(query.Limit)
|
||||
|
||||
s.logger.Info("Fetching dashboard recent tenders", map[string]interface{}{
|
||||
"limit": limit,
|
||||
})
|
||||
|
||||
items, nextCursor, err := s.repo.Recent(ctx, limit, strings.TrimSpace(query.Cursor))
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard recent tenders", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
cursor := strings.TrimSpace(query.Cursor)
|
||||
if cursor != "" {
|
||||
s.logger.Info("Fetching dashboard recent tenders", map[string]interface{}{
|
||||
"limit": limit,
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard recent: %w", err)
|
||||
|
||||
items, nextCursor, err := s.repo.Recent(ctx, limit, cursor)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard recent tenders", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard recent: %w", err)
|
||||
}
|
||||
|
||||
return &RecentResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &RecentResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
}, nil
|
||||
cacheKey := strconv.Itoa(limit)
|
||||
return s.recentCache.Get(ctx, cacheKey, func(loadCtx context.Context) (*RecentResponse, error) {
|
||||
s.logger.Info("Fetching dashboard recent tenders", map[string]interface{}{
|
||||
"limit": limit,
|
||||
})
|
||||
|
||||
items, nextCursor, err := s.repo.Recent(loadCtx, limit, "")
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to fetch dashboard recent tenders", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("dashboard recent: %w", err)
|
||||
}
|
||||
|
||||
return &RecentResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) Statistics(ctx context.Context, query StatisticsQuery) (*StatisticsReportResponse, error) {
|
||||
@@ -339,6 +382,70 @@ func (s *service) loadStatistics(ctx context.Context, days int) (*StatisticsRepo
|
||||
return out.(*StatisticsReportResponse), nil
|
||||
}
|
||||
|
||||
func (s *service) warmWidgetCaches() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
defaultWindowSec := int64(defaultClosingWindowHours) * 3600
|
||||
warmed := 0
|
||||
|
||||
if s.trendCache.WarmFromRedis(ctx, fmt.Sprintf("%d:%s", defaultTrendDays, "created")) {
|
||||
warmed++
|
||||
}
|
||||
if s.countriesCache.WarmFromRedis(ctx, strconv.Itoa(defaultCountriesLimit)) {
|
||||
warmed++
|
||||
}
|
||||
if s.noticeTypesCache.WarmFromRedis(ctx, "all") {
|
||||
warmed++
|
||||
}
|
||||
if s.closingSoonCache.WarmFromRedis(ctx, fmt.Sprintf("%d:%d", defaultListLimit, defaultWindowSec)) {
|
||||
warmed++
|
||||
}
|
||||
if s.recentCache.WarmFromRedis(ctx, strconv.Itoa(defaultListLimit)) {
|
||||
warmed++
|
||||
}
|
||||
|
||||
if warmed > 0 {
|
||||
s.logger.Info("Dashboard widget caches warmed from Redis", map[string]interface{}{
|
||||
"warmed": warmed,
|
||||
})
|
||||
}
|
||||
|
||||
go s.trendCache.Reload(fmt.Sprintf("%d:%s", defaultTrendDays, "created"), func(loadCtx context.Context) (*TrendResponse, error) {
|
||||
return s.loadTrend(loadCtx, TrendQuery{Days: defaultTrendDays, Metric: "created"})
|
||||
})
|
||||
go s.countriesCache.Reload(strconv.Itoa(defaultCountriesLimit), func(loadCtx context.Context) (*CountriesResponse, error) {
|
||||
return s.repo.Countries(loadCtx, defaultCountriesLimit)
|
||||
})
|
||||
go s.noticeTypesCache.Reload("all", func(loadCtx context.Context) (*NoticeTypesResponse, error) {
|
||||
return s.repo.NoticeTypes(loadCtx)
|
||||
})
|
||||
go s.closingSoonCache.Reload(fmt.Sprintf("%d:%d", defaultListLimit, defaultWindowSec), func(loadCtx context.Context) (*ClosingSoonResponse, error) {
|
||||
items, err := s.repo.ClosingSoon(loadCtx, defaultListLimit, defaultWindowSec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ClosingSoonResponse{Items: items}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) loadTrend(ctx context.Context, query TrendQuery) (*TrendResponse, error) {
|
||||
days := trendDays(query.Days)
|
||||
metric := normalizeTrendMetric(query.Metric)
|
||||
startUnix := trendStartUnix(days)
|
||||
|
||||
counts, err := s.repo.Trend(ctx, days, metric, startUnix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TrendResponse{
|
||||
Metric: metric,
|
||||
Days: days,
|
||||
Series: fillTrendSeries(days, counts),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) warmSummaryCache() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user