|
1 | | -import { S3Client } from "@aws-sdk/client-s3"; |
2 | | -import { SQSClient } from "@aws-sdk/client-sqs"; |
3 | | -import { SNSClient } from "@aws-sdk/client-sns"; |
4 | | -import { IAMClient } from "@aws-sdk/client-iam"; |
5 | 1 | import { CloudFormationClient } from "@aws-sdk/client-cloudformation"; |
6 | 2 | import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
7 | | -import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; |
8 | 3 | import { DynamoDBStreamsClient } from "@aws-sdk/client-dynamodb-streams"; |
| 4 | +import { IAMClient } from "@aws-sdk/client-iam"; |
| 5 | +import { S3Client } from "@aws-sdk/client-s3"; |
| 6 | +import { SNSClient } from "@aws-sdk/client-sns"; |
| 7 | +import { SQSClient } from "@aws-sdk/client-sqs"; |
| 8 | +import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; |
9 | 9 |
|
10 | 10 | export interface AwsClients { |
11 | | - s3: S3Client; |
12 | | - sqs: SQSClient; |
13 | | - sns: SNSClient; |
14 | | - iam: IAMClient; |
15 | | - cloudformation: CloudFormationClient; |
16 | | - dynamodb: DynamoDBClient; |
17 | | - dynamodbDocument: DynamoDBDocumentClient; |
18 | | - dynamodbStreams: DynamoDBStreamsClient; |
| 11 | + s3: S3Client; |
| 12 | + sqs: SQSClient; |
| 13 | + sns: SNSClient; |
| 14 | + iam: IAMClient; |
| 15 | + cloudformation: CloudFormationClient; |
| 16 | + dynamodb: DynamoDBClient; |
| 17 | + dynamodbDocument: DynamoDBDocumentClient; |
| 18 | + dynamodbStreams: DynamoDBStreamsClient; |
19 | 19 | } |
20 | 20 |
|
21 | 21 | const MAX_CACHE_SIZE = 20; |
22 | 22 |
|
23 | 23 | export class ClientCache { |
24 | | - private cache = new Map<string, AwsClients>(); |
| 24 | + private cache = new Map<string, AwsClients>(); |
25 | 25 |
|
26 | | - private makeKey(endpoint: string, region: string): string { |
27 | | - return `${endpoint}::${region}`; |
28 | | - } |
| 26 | + private makeKey(endpoint: string, region: string): string { |
| 27 | + return `${endpoint}::${region}`; |
| 28 | + } |
29 | 29 |
|
30 | | - private createClients(endpoint: string, region: string): AwsClients { |
31 | | - const commonConfig = { |
32 | | - endpoint, |
33 | | - region, |
34 | | - credentials: { |
35 | | - accessKeyId: "test", |
36 | | - secretAccessKey: "test", |
37 | | - }, |
38 | | - }; |
| 30 | + private createClients(endpoint: string, region: string): AwsClients { |
| 31 | + const commonConfig = { |
| 32 | + endpoint, |
| 33 | + region, |
| 34 | + credentials: { |
| 35 | + accessKeyId: "test", |
| 36 | + secretAccessKey: "test", |
| 37 | + }, |
| 38 | + }; |
39 | 39 |
|
40 | | - const dynamodb = new DynamoDBClient(commonConfig); |
| 40 | + const dynamodb = new DynamoDBClient(commonConfig); |
41 | 41 |
|
42 | | - return { |
43 | | - s3: new S3Client({ ...commonConfig, forcePathStyle: true }), |
44 | | - sqs: new SQSClient(commonConfig), |
45 | | - sns: new SNSClient(commonConfig), |
46 | | - iam: new IAMClient(commonConfig), |
47 | | - cloudformation: new CloudFormationClient(commonConfig), |
48 | | - dynamodb, |
49 | | - dynamodbDocument: DynamoDBDocumentClient.from(dynamodb, { |
50 | | - marshallOptions: { removeUndefinedValues: true }, |
51 | | - }), |
52 | | - dynamodbStreams: new DynamoDBStreamsClient(commonConfig), |
53 | | - }; |
54 | | - } |
| 42 | + return { |
| 43 | + s3: new S3Client({ ...commonConfig, forcePathStyle: true }), |
| 44 | + sqs: new SQSClient(commonConfig), |
| 45 | + sns: new SNSClient(commonConfig), |
| 46 | + iam: new IAMClient(commonConfig), |
| 47 | + cloudformation: new CloudFormationClient(commonConfig), |
| 48 | + dynamodb, |
| 49 | + dynamodbDocument: DynamoDBDocumentClient.from(dynamodb, { |
| 50 | + marshallOptions: { removeUndefinedValues: true }, |
| 51 | + }), |
| 52 | + dynamodbStreams: new DynamoDBStreamsClient(commonConfig), |
| 53 | + }; |
| 54 | + } |
55 | 55 |
|
56 | | - getClients(endpoint: string, region: string): AwsClients { |
57 | | - const key = this.makeKey(endpoint, region); |
| 56 | + getClients(endpoint: string, region: string): AwsClients { |
| 57 | + const key = this.makeKey(endpoint, region); |
58 | 58 |
|
59 | | - let clients = this.cache.get(key); |
60 | | - if (clients) return clients; |
| 59 | + let clients = this.cache.get(key); |
| 60 | + if (clients) return clients; |
61 | 61 |
|
62 | | - // LRU eviction: remove oldest entry if cache is full |
63 | | - if (this.cache.size >= MAX_CACHE_SIZE) { |
64 | | - const firstKey = this.cache.keys().next().value; |
65 | | - if (firstKey) this.cache.delete(firstKey); |
66 | | - } |
| 62 | + // LRU eviction: remove oldest entry if cache is full |
| 63 | + if (this.cache.size >= MAX_CACHE_SIZE) { |
| 64 | + const firstKey = this.cache.keys().next().value; |
| 65 | + if (firstKey) this.cache.delete(firstKey); |
| 66 | + } |
67 | 67 |
|
68 | | - clients = this.createClients(endpoint, region); |
69 | | - this.cache.set(key, clients); |
70 | | - return clients; |
71 | | - } |
| 68 | + clients = this.createClients(endpoint, region); |
| 69 | + this.cache.set(key, clients); |
| 70 | + return clients; |
| 71 | + } |
72 | 72 | } |
0 commit comments