-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.ts
More file actions
277 lines (231 loc) · 7.89 KB
/
cache.ts
File metadata and controls
277 lines (231 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import type {
FastifyInstance,
FastifyPluginAsync,
FastifyReply,
FastifyRequest,
} from 'fastify';
import fp from 'fastify-plugin';
import { ENV } from '../../env';
import { encode } from '../../libs/encode';
import { createRedisConnection } from '../../libs/utils/redis';
const cacheRedisConnection = createRedisConnection(
ENV.REDIS_URL,
ENV.REDIS_CLUSTER_MODE,
{
keyPrefix: 'indexer-cache:',
},
);
type RedisClient = ReturnType<typeof createRedisConnection>;
export type RouteCacheOptions = {
/** Enable/disable cache for this route (default: true if cache is set) */
enabled?: boolean;
/** TTL for this route in seconds (default: plugin defaultTtlSeconds or 60) */
ttlSeconds?: number;
/**
* Serve stale responses for this many extra seconds while
* a background refresh runs (stale-while-revalidate window).
*/
staleTtlSeconds?: number;
/**
* Enable/disable background revalidation (default: true if staleTtlSeconds set).
*/
backgroundRevalidate?: boolean;
/** Build a custom cache key based on request */
key?: (req: FastifyRequest) => string;
};
export type RedisCachePluginOptions = {
/** Existing Redis client instance (if you already manage it elsewhere) */
redisClient?: RedisClient;
/** Or pass Redis URL, e.g. redis://localhost:6379 */
redisUrl?: string;
/** Default TTL in seconds for all cached routes */
defaultTtlSeconds?: number;
/** Default stale TTL in seconds for all cached routes */
defaultStaleTtlSeconds?: number;
/** Prefix for all cache keys */
keyPrefix?: string;
};
type CacheEntry = {
payload: unknown;
headers?: Record<string, unknown>;
statusCode: number;
storedAt: number; // ms since epoch
ttlSeconds: number;
};
declare module 'fastify' {
interface FastifyInstance {
cacheRedis: RedisClient;
}
interface FastifyRequest {
__cacheKey?: string;
__cacheHit?: boolean;
}
// interface RouteShorthandOptions {
// /**
// * If present, enables response caching for this route (default false).
// * - `true` -> use defaults
// * - `false` -> no cache
// * - object -> fine-grained control
// */
// cache?: RouteCacheOptions | boolean;
// }
interface FastifyContextConfig {
/**
* If present, enables response caching for this route (default false).
* - `true` -> use defaults
* - `false` -> no cache
* - object -> fine-grained control
*/
cache?: RouteCacheOptions | boolean;
}
}
const redisCachePlugin: FastifyPluginAsync<RedisCachePluginOptions> = async (
fastify: FastifyInstance,
opts: RedisCachePluginOptions,
) => {
const redis = opts.redisClient ?? cacheRedisConnection;
const defaultTtl = opts.defaultTtlSeconds ?? 60;
const defaultStaleTtl = opts.defaultStaleTtlSeconds ?? 600; // 10 minutes
const keyPrefix = opts.keyPrefix ?? 'route-cache';
// @ts-expect-error declare decorator
fastify.decorate('cacheRedis', redis);
const getRouteCacheConfig = (
req: FastifyRequest,
): RouteCacheOptions | null => {
const rawCfg = req.routeOptions.config.cache;
if (!rawCfg) return null;
if (typeof rawCfg === 'boolean') {
if (!rawCfg) return null;
return { enabled: true };
}
if (rawCfg.enabled === false) return null;
return { enabled: true, ...rawCfg };
};
// 1) Try to serve from cache
fastify.addHook(
'preHandler',
async (req: FastifyRequest, reply: FastifyReply) => {
const cfg = getRouteCacheConfig(req);
if (!cfg) return;
// Internal revalidation request: do not serve from cache
if (req.headers['x-cache-revalidate'] === '1') {
return;
}
const ttl = cfg.ttlSeconds ?? defaultTtl;
const staleTtl = cfg.staleTtlSeconds ?? defaultStaleTtl;
const routeUrl =
req.routeOptions.url ?? req.raw.url?.split('?')[0] ?? 'unknown';
const key =
keyPrefix +
':' +
(cfg.key?.(req) ??
encode.sha256(
`${routeUrl}:${req.raw.method}:` +
`${JSON.stringify(req.query ?? {})}:${JSON.stringify(req.body ?? {})}`,
));
req.__cacheKey = key;
const cached = await redis.get(key);
if (!cached) return;
const entry: CacheEntry = JSON.parse(cached);
const ageSec = (Date.now() - entry.storedAt) / 1000;
const isFresh = ageSec <= entry.ttlSeconds;
const isWithinStale =
!isFresh && staleTtl > 0 && ageSec <= entry.ttlSeconds + staleTtl;
if (!isFresh && !isWithinStale) {
// Hard expired: ignore cache
return;
}
req.__cacheHit = true;
if (entry.headers) {
for (const [hKey, hVal] of Object.entries(entry.headers)) {
// do not override critical hop-by-hop headers if you don't want to
if (hKey.toLowerCase() === 'content-length') continue;
if (hKey.toLowerCase() === 'x-cache') continue;
reply.header(hKey, hVal);
}
}
reply.header('x-cache', isFresh ? 'HIT' : 'HIT-STALE');
reply.code(entry.statusCode);
reply.send(entry.payload);
// Background revalidation for stale entries
if (isWithinStale && (cfg.backgroundRevalidate ?? true)) {
const lockKey = `${key}:revalidate-lock`;
const lockTtl = Math.max(5, Math.floor(ttl / 2)); // seconds
// Try to acquire revalidation lock
try {
const lockResult = await redis.setnx(lockKey, '1');
if (lockResult === 1) {
await redis.expire(lockKey, lockTtl);
// Fire-and-forget background refresh
(async () => {
try {
await fastify.inject({
// @ts-expect-error bad type
method: req.raw.method,
url: req.raw.url ?? routeUrl,
// @ts-expect-error bad type
payload: req.body,
// @ts-expect-error bad type
query: req.query,
headers: {
...req.headers,
'x-cache-revalidate': '1',
},
});
} finally {
// Let the lock expire naturally; optional explicit delete:
// await redis.del(lockKey);
}
})().catch((err) => {
fastify.log.error({ err }, 'cache revalidation failed');
});
}
} catch (err) {
fastify.log.error({ err }, 'failed to acquire revalidate lock');
}
}
},
);
// 2) Store response into cache
fastify.addHook(
'onSend',
async (req: FastifyRequest, reply: FastifyReply, payload) => {
const rawCfg = req.routeOptions.config.cache;
if (!rawCfg) return payload;
const cfg: RouteCacheOptions =
typeof rawCfg === 'boolean' ? { enabled: rawCfg } : rawCfg;
if (cfg.enabled === false) return payload;
if (req.__cacheHit) {
return payload;
}
const key = req.__cacheKey;
if (!key) return payload;
// By default, don't cache server error responses (5xx)
if (reply.statusCode >= 500) return payload;
const ttl = cfg.ttlSeconds ?? defaultTtl;
const headers = reply.getHeaders() as Record<string, unknown>;
for (const h of Object.keys(headers)) {
if (h.toLowerCase() === 'x-cache') {
delete headers[h];
}
}
const entry: CacheEntry = {
payload,
headers,
statusCode: reply.statusCode,
storedAt: Date.now(),
ttlSeconds: ttl,
};
const expireSeconds = ttl + (cfg.staleTtlSeconds ?? defaultStaleTtl);
await redis.setex(key, expireSeconds, JSON.stringify(entry));
// For "real" client requests (not internal revalidation), set MISS header
if (req.headers['x-cache-revalidate'] !== '1') {
reply.header('x-cache', 'MISS');
}
return payload;
},
);
};
export default fp(redisCachePlugin, {
name: 'cache-plugin',
});