@@ -6,7 +6,12 @@ import { asc, eq, and, gt, gte, lte, or, sql } from "drizzle-orm";
66import { StarknetAddress } from "../utils/validation.js" ;
77import { DEFAULT_TOKEN_DECIMALS } from "../utils/codec.js" ;
88import { env } from "../config.js" ;
9- import { AnalyticsCache , buildAnalyticsCacheKey } from "../utils/analytics-cache.js" ;
9+ import Redis from "ioredis" ;
10+ import {
11+ AnalyticsCache ,
12+ RedisAnalyticsCache ,
13+ buildAnalyticsCacheKey ,
14+ } from "../utils/analytics-cache.js" ;
1015
1116export const analyticsRouter = Router ( ) ;
1217
@@ -19,6 +24,10 @@ export const ANALYTICS_ROLLUP_BATCH_SIZE = 500;
1924export const analyticsAggregationCache = new AnalyticsCache (
2025 env . ANALYTICS_CACHE_TTL_MS ?? 30_000 ,
2126) ;
27+ const redisAnalyticsCache = env . REDIS_URL
28+ ? new RedisAnalyticsCache ( new Redis ( env . REDIS_URL ) , env . ANALYTICS_CACHE_TTL_MS ?? 30_000 )
29+ : undefined ;
30+ const analyticsCache = redisAnalyticsCache ?? analyticsAggregationCache ;
2231
2332const MONTH_NAMES = [
2433 "Jan" ,
@@ -242,7 +251,7 @@ analyticsRouter.get("/analytics/:user_address", async (req, res, next) => {
242251
243252 const cacheKey = buildAnalyticsCacheKey ( userAddress , { year } ) ;
244253
245- const cached = analyticsAggregationCache . get ( cacheKey ) ;
254+ const cached = await Promise . resolve ( analyticsCache . get ( cacheKey ) ) ;
246255 if ( cached ) {
247256 res . set ( "Cache-Control" , "private, max-age=60" ) ;
248257 const etag = computeETag ( cached ) ;
@@ -482,7 +491,7 @@ analyticsRouter.get("/analytics/:user_address", async (req, res, next) => {
482491 data : chartData ,
483492 total : toDisplayNumber ( totalRaw ) ,
484493 } ;
485- analyticsAggregationCache . set ( cacheKey , responseBody ) ;
494+ await Promise . resolve ( analyticsCache . set ( cacheKey , responseBody ) ) ;
486495
487496 res . set ( "Cache-Control" , "private, max-age=60" ) ;
488497 const etag = computeETag ( responseBody ) ;
0 commit comments