33 checkPaidMembershipFromEntra ,
44 checkPaidMembershipFromTable ,
55 setPaidMembershipInTable ,
6+ MEMBER_CACHE_SECONDS ,
67} from "api/functions/membership.js" ;
78import { validateNetId } from "api/functions/validation.js" ;
89import { FastifyPluginAsync } from "fastify" ;
@@ -26,9 +27,7 @@ import rawbody from "fastify-raw-body";
2627import { FastifyZodOpenApiTypeProvider } from "fastify-zod-openapi" ;
2728import { z } from "zod" ;
2829import { withTags } from "api/components/index.js" ;
29-
30- const NONMEMBER_CACHE_SECONDS = 60 ; // 1 minute
31- const MEMBER_CACHE_SECONDS = 43200 ; // 12 hours
30+ import { getKey , setKey } from "api/functions/redisCache.js" ;
3231
3332const membershipPlugin : FastifyPluginAsync = async ( fastify , _options ) => {
3433 await fastify . register ( rawbody , {
@@ -134,11 +133,7 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
134133 message : `${ netId } is already a paid member!` ,
135134 } ) ;
136135 }
137- fastify . nodeCache . set (
138- `isMember_${ netId } ` ,
139- false ,
140- NONMEMBER_CACHE_SECONDS ,
141- ) ;
136+ fastify . nodeCache . set ( `isMember_${ netId } ` , false , MEMBER_CACHE_SECONDS ) ;
142137 const secretApiConfig =
143138 ( await getSecretValue (
144139 fastify . secretsManagerClient ,
@@ -190,11 +185,17 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
190185 async ( request , reply ) => {
191186 const netId = request . params . netId . toLowerCase ( ) ;
192187 const list = request . query . list || "acmpaid" ;
193- if ( fastify . nodeCache . get ( `isMember_${ netId } _${ list } ` ) !== undefined ) {
188+ const cacheKey = `membership:${ netId } :${ list } ` ;
189+ const result = await getKey < { isMember : boolean } > ( {
190+ redisClient : fastify . redisClient ,
191+ key : cacheKey ,
192+ logger : request . log ,
193+ } ) ;
194+ if ( result ) {
194195 return reply . header ( "X-ACM-Data-Source" , "cache" ) . send ( {
195196 netId,
196197 list : list === "acmpaid" ? undefined : list ,
197- isPaidMember : fastify . nodeCache . get ( `isMember_ ${ netId } _ ${ list } ` ) ,
198+ isPaidMember : result . isMember ,
198199 } ) ;
199200 }
200201 if ( list !== "acmpaid" ) {
@@ -203,11 +204,13 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
203204 list ,
204205 fastify . dynamoClient ,
205206 ) ;
206- fastify . nodeCache . set (
207- `isMember_${ netId } _${ list } ` ,
208- isMember ,
209- MEMBER_CACHE_SECONDS ,
210- ) ;
207+ await setKey ( {
208+ redisClient : fastify . redisClient ,
209+ key : cacheKey ,
210+ data : JSON . stringify ( { isMember } ) ,
211+ expiresIn : MEMBER_CACHE_SECONDS ,
212+ logger : request . log ,
213+ } ) ;
211214 return reply . header ( "X-ACM-Data-Source" , "dynamo" ) . send ( {
212215 netId,
213216 list,
@@ -219,11 +222,13 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
219222 fastify . dynamoClient ,
220223 ) ;
221224 if ( isDynamoMember ) {
222- fastify . nodeCache . set (
223- `isMember_${ netId } _${ list } ` ,
224- true ,
225- MEMBER_CACHE_SECONDS ,
226- ) ;
225+ await setKey ( {
226+ redisClient : fastify . redisClient ,
227+ key : cacheKey ,
228+ data : JSON . stringify ( { isMember : true } ) ,
229+ expiresIn : MEMBER_CACHE_SECONDS ,
230+ logger : request . log ,
231+ } ) ;
227232 return reply
228233 . header ( "X-ACM-Data-Source" , "dynamo" )
229234 . send ( { netId, isPaidMember : true } ) ;
@@ -241,22 +246,26 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
241246 paidMemberGroup ,
242247 ) ;
243248 if ( isAadMember ) {
244- fastify . nodeCache . set (
245- `isMember_${ netId } _${ list } ` ,
246- true ,
247- MEMBER_CACHE_SECONDS ,
248- ) ;
249+ await setKey ( {
250+ redisClient : fastify . redisClient ,
251+ key : cacheKey ,
252+ data : JSON . stringify ( { isMember : true } ) ,
253+ expiresIn : MEMBER_CACHE_SECONDS ,
254+ logger : request . log ,
255+ } ) ;
249256 reply
250257 . header ( "X-ACM-Data-Source" , "aad" )
251258 . send ( { netId, isPaidMember : true } ) ;
252259 await setPaidMembershipInTable ( netId , fastify . dynamoClient ) ;
253260 return ;
254261 }
255- fastify . nodeCache . set (
256- `isMember_${ netId } _${ list } ` ,
257- false ,
258- NONMEMBER_CACHE_SECONDS ,
259- ) ;
262+ await setKey ( {
263+ redisClient : fastify . redisClient ,
264+ key : cacheKey ,
265+ data : JSON . stringify ( { isMember : false } ) ,
266+ expiresIn : MEMBER_CACHE_SECONDS ,
267+ logger : request . log ,
268+ } ) ;
260269 return reply
261270 . header ( "X-ACM-Data-Source" , "aad" )
262271 . send ( { netId, isPaidMember : false } ) ;
@@ -315,6 +324,7 @@ const membershipPlugin: FastifyPluginAsync = async (fastify, _options) => {
315324 ) {
316325 const customerEmail = event . data . object . customer_email ;
317326 if ( ! customerEmail ) {
327+ request . log . info ( "No customer email found." ) ;
318328 return reply
319329 . code ( 200 )
320330 . send ( { handled : false , requestId : request . id } ) ;
0 commit comments