-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetAccountByUserIdCache.ts
30 lines (26 loc) · 1.07 KB
/
GetAccountByUserIdCache.ts
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
import { AppSyncResolverEvent, Context } from 'aws-lambda';
import { Envelope } from '../../../../shared/core/Envelope';
import { ExeResponse, IDecorator } from '../../../../shared/decorators/IDecorator';
import { Request } from './GetAccountByUserIdDTOs';
export class GetAccountByUserIdCache implements IDecorator<Request> {
public wrapee: IDecorator<Request>['wrapee'];
private cache: Record<string, Envelope<unknown>> = {};
public constructor(wrapee: IDecorator<Request>['wrapee']) {
this.wrapee = wrapee;
}
public async execute(
event: AppSyncResolverEvent<Request>,
context: Context
): ExeResponse {
console.log(`${this.constructor.name}.execute`);
const userId = event.arguments.userId;
if (userId in this.cache) {
console.log(`Response for userId ${userId} taken from cache`); // Used in test GetAccountByUserId.int.ts
return this.cache[userId];
}
const response = await this.wrapee.execute(event, context);
this.cache[userId] = response;
console.log(`${userId} saved in cache`, this.cache);
return response;
}
}