Skip to content

Commit ccfcae1

Browse files
committed
use cloudfront to cache events route
1 parent a4fc884 commit ccfcae1

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

cloudformation/main.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,28 @@ Resources:
602602
Forward: none
603603
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6 # caching-optimized
604604
CacheBehaviors:
605+
- PathPattern: "/api/v1/events"
606+
TargetOriginId: ApiGatewayOrigin
607+
ViewerProtocolPolicy: redirect-to-https
608+
AllowedMethods:
609+
- GET
610+
- HEAD
611+
- OPTIONS
612+
- PUT
613+
- POST
614+
- DELETE
615+
- PATCH
616+
CachedMethods:
617+
- GET
618+
- HEAD
619+
ForwardedValues:
620+
QueryString: true
621+
QueryStringCacheKeys:
622+
- host
623+
- ts
624+
- upcomingOnly
625+
Cookies:
626+
Forward: none
605627
- PathPattern: "/api/*"
606628
TargetOriginId: ApiGatewayOrigin
607629
ViewerProtocolPolicy: redirect-to-https

src/api/routes/events.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import { IUpdateDiscord, updateDiscord } from "../functions/discord.js";
2626
import rateLimiter from "api/plugins/rateLimiter.js";
2727

2828
const repeatOptions = ["weekly", "biweekly"] as const;
29-
const EVENT_CACHE_SECONDS = 90;
3029
const CLIENT_HTTP_CACHE_POLICY =
31-
"public, max-age=180, stale-while-revalidate=420, stale-if-error=3600";
30+
"public, max-age=120, stale-while-revalidate=420, stale-if-error=3600";
3231
export type EventRepeatOptions = (typeof repeatOptions)[number];
3332

3433
const baseSchema = z.object({
@@ -121,21 +120,6 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
121120
const upcomingOnly = request.query?.upcomingOnly || false;
122121
const host = request.query?.host;
123122
const ts = request.query?.ts; // we only use this to disable cache control
124-
const cachedResponse = fastify.nodeCache.get(
125-
`events-upcoming_only=${upcomingOnly}`,
126-
) as EventsGetResponse;
127-
if (cachedResponse) {
128-
if (!ts) {
129-
reply.header("Cache-Control", CLIENT_HTTP_CACHE_POLICY);
130-
}
131-
let filteredResponse = cachedResponse;
132-
if (host) {
133-
filteredResponse = cachedResponse.filter((x) => x["host"] == host);
134-
}
135-
return reply
136-
.header("x-acm-cache-status", "hit")
137-
.send(filteredResponse);
138-
}
139123
try {
140124
let command;
141125
if (host) {
@@ -188,13 +172,6 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
188172
}
189173
});
190174
}
191-
if (!host) {
192-
fastify.nodeCache.set(
193-
`events-upcoming_only=${upcomingOnly}`,
194-
parsedItems,
195-
EVENT_CACHE_SECONDS,
196-
);
197-
}
198175
if (!ts) {
199176
reply.header("Cache-Control", CLIENT_HTTP_CACHE_POLICY);
200177
}
@@ -301,9 +278,6 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
301278
}
302279
throw new DiscordEventError({});
303280
}
304-
fastify.nodeCache.del("events-upcoming_only=true");
305-
fastify.nodeCache.del("events-upcoming_only=false");
306-
fastify.nodeCache.del(`event-${entryUUID}`);
307281
reply.status(201).send({
308282
id: entryUUID,
309283
resource: `/api/v1/events/${entryUUID}`,
@@ -354,9 +328,6 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
354328
id,
355329
resource: `/api/v1/events/${id}`,
356330
});
357-
fastify.nodeCache.del("events-upcoming_only=true");
358-
fastify.nodeCache.del("events-upcoming_only=false");
359-
fastify.nodeCache.del(`event-${id}`);
360331
} catch (e: unknown) {
361332
if (e instanceof Error) {
362333
request.log.error("Failed to delete from DynamoDB: " + e.toString());
@@ -387,14 +358,6 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
387358
async (request: FastifyRequest<EventGetRequest>, reply) => {
388359
const id = request.params.id;
389360
const ts = request.query?.ts;
390-
const cachedResponse = fastify.nodeCache.get(`event-${id}`);
391-
if (cachedResponse) {
392-
if (!ts) {
393-
reply.header("Cache-Control", CLIENT_HTTP_CACHE_POLICY);
394-
}
395-
return reply.header("x-acm-cache-status", "hit").send(cachedResponse);
396-
}
397-
398361
try {
399362
const response = await fastify.dynamoClient.send(
400363
new GetItemCommand({
@@ -406,7 +369,6 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
406369
if (!item) {
407370
throw new NotFoundError({ endpointName: request.url });
408371
}
409-
fastify.nodeCache.set(`event-${id}`, item, EVENT_CACHE_SECONDS);
410372

411373
if (!ts) {
412374
reply.header("Cache-Control", CLIENT_HTTP_CACHE_POLICY);

0 commit comments

Comments
 (0)