Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions initialize/migrate/database/02126_system_log_idx.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX idx_type_date ON system_logs;
1 change: 1 addition & 0 deletions initialize/migrate/database/02126_system_log_idx.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX idx_type_date ON system_logs (type, date);
24 changes: 22 additions & 2 deletions internal/logic/admin/console/queryRevenueStatisticsLogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package console

import (
"context"
"encoding/json"
"os"
"strings"
"time"
Expand All @@ -13,6 +14,9 @@ import (
"github.com/pkg/errors"
)

const consoleRevenueStatisticsCacheKey = "console:revenue_statistics"
const consoleRevenueStatisticsCacheTTL = 60 * time.Second

type QueryRevenueStatisticsLogic struct {
logger.Logger
ctx context.Context
Expand All @@ -33,6 +37,15 @@ func (l *QueryRevenueStatisticsLogic) QueryRevenueStatistics() (resp *types.Reve
return l.mockRevenueStatistics(), nil
}

// Try cache first
cached, cacheErr := l.svcCtx.Redis.Get(l.ctx, consoleRevenueStatisticsCacheKey).Result()
if cacheErr == nil && cached != "" {
var result types.RevenueStatisticsResponse
if json.Unmarshal([]byte(cached), &result) == nil {
return &result, nil
}
}

var today, monthly, all types.OrdersStatistics
now := time.Now()
// Get today's revenue statistics
Expand Down Expand Up @@ -111,11 +124,18 @@ func (l *QueryRevenueStatisticsLogic) QueryRevenueStatistics() (resp *types.Reve
all.List = allList
}

return &types.RevenueStatisticsResponse{
resp = &types.RevenueStatisticsResponse{
Today: today,
Monthly: monthly,
All: all,
}, nil
}

// Cache the result
if data, marshalErr := json.Marshal(resp); marshalErr == nil {
l.svcCtx.Redis.Set(l.ctx, consoleRevenueStatisticsCacheKey, data, consoleRevenueStatisticsCacheTTL)
}

return resp, nil
}

// mockRevenueStatistics is a mock function to simulate revenue statistics data.
Expand Down
Loading
Loading