Skip to content

Commit e074694

Browse files
committed
manage cache in checkout area
1 parent 1e28437 commit e074694

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/api/routes/membership.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
8888
},
8989
async (request, reply) => {
9090
const netId = request.params.netId.toLowerCase();
91-
if (fastify.nodeCache.get(`isMember_${netId}`) === true) {
91+
const cacheKey = `membership:${netId}:acmpaid`;
92+
const result = await getKey<{ isMember: boolean }>({
93+
redisClient: fastify.redisClient,
94+
key: cacheKey,
95+
logger: request.log,
96+
});
97+
if (result && result.isMember) {
9298
throw new ValidationError({
9399
message: `${netId} is already a paid member!`,
94100
});
@@ -98,11 +104,13 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
98104
fastify.dynamoClient,
99105
);
100106
if (isDynamoMember) {
101-
fastify.nodeCache.set(
102-
`isMember_${netId}`,
103-
true,
104-
MEMBER_CACHE_SECONDS,
105-
);
107+
await setKey({
108+
redisClient: fastify.redisClient,
109+
key: cacheKey,
110+
data: JSON.stringify({ isMember: true }),
111+
expiresIn: MEMBER_CACHE_SECONDS,
112+
logger: request.log,
113+
});
106114
throw new ValidationError({
107115
message: `${netId} is already a paid member!`,
108116
});
@@ -120,11 +128,13 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
120128
paidMemberGroup,
121129
);
122130
if (isAadMember) {
123-
fastify.nodeCache.set(
124-
`isMember_${netId}`,
125-
true,
126-
MEMBER_CACHE_SECONDS,
127-
);
131+
await setKey({
132+
redisClient: fastify.redisClient,
133+
key: cacheKey,
134+
data: JSON.stringify({ isMember: true }),
135+
expiresIn: MEMBER_CACHE_SECONDS,
136+
logger: request.log,
137+
});
128138
reply
129139
.header("X-ACM-Data-Source", "aad")
130140
.send({ netId, isPaidMember: true });
@@ -133,7 +143,14 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
133143
message: `${netId} is already a paid member!`,
134144
});
135145
}
136-
fastify.nodeCache.set(`isMember_${netId}`, false, MEMBER_CACHE_SECONDS);
146+
// Once the caller becomes a member, the stripe webhook will handle changing this to true
147+
await setKey({
148+
redisClient: fastify.redisClient,
149+
key: cacheKey,
150+
data: JSON.stringify({ isMember: false }),
151+
expiresIn: MEMBER_CACHE_SECONDS,
152+
logger: request.log,
153+
});
137154
const secretApiConfig =
138155
(await getSecretValue(
139156
fastify.secretsManagerClient,
@@ -185,6 +202,8 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
185202
async (request, reply) => {
186203
const netId = request.params.netId.toLowerCase();
187204
const list = request.query.list || "acmpaid";
205+
// we don't control external list as its direct upload in Dynamo, cache only for 60 seconds.
206+
const ourCacheSeconds = list === "acmpaid" ? MEMBER_CACHE_SECONDS : 60;
188207
const cacheKey = `membership:${netId}:${list}`;
189208
const result = await getKey<{ isMember: boolean }>({
190209
redisClient: fastify.redisClient,
@@ -208,7 +227,7 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
208227
redisClient: fastify.redisClient,
209228
key: cacheKey,
210229
data: JSON.stringify({ isMember }),
211-
expiresIn: MEMBER_CACHE_SECONDS,
230+
expiresIn: ourCacheSeconds,
212231
logger: request.log,
213232
});
214233
return reply.header("X-ACM-Data-Source", "dynamo").send({
@@ -226,7 +245,7 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
226245
redisClient: fastify.redisClient,
227246
key: cacheKey,
228247
data: JSON.stringify({ isMember: true }),
229-
expiresIn: MEMBER_CACHE_SECONDS,
248+
expiresIn: ourCacheSeconds,
230249
logger: request.log,
231250
});
232251
return reply
@@ -250,7 +269,7 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
250269
redisClient: fastify.redisClient,
251270
key: cacheKey,
252271
data: JSON.stringify({ isMember: true }),
253-
expiresIn: MEMBER_CACHE_SECONDS,
272+
expiresIn: ourCacheSeconds,
254273
logger: request.log,
255274
});
256275
reply
@@ -263,7 +282,7 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
263282
redisClient: fastify.redisClient,
264283
key: cacheKey,
265284
data: JSON.stringify({ isMember: false }),
266-
expiresIn: MEMBER_CACHE_SECONDS,
285+
expiresIn: ourCacheSeconds,
267286
logger: request.log,
268287
});
269288
return reply

0 commit comments

Comments
 (0)