Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Implement LRU for API keys
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent 6c546cf commit 8a5c015
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Configuration {

caching: {
geolocationLruSize: number;
apiKeyLruSize: number;
};

security: {
Expand Down
1 change: 1 addition & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const configuration: Configuration = {
},
caching: {
geolocationLruSize: int(process.env.GEOLOCATION_LRU_SIZE, 100),
apiKeyLruSize: int(process.env.API_KEY_LRU_SIZE, 100),
},
security: {
saltRounds: int(process.env.SALT_ROUNDS, 10),
Expand Down
12 changes: 12 additions & 0 deletions src/modules/api-keys/api-keys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
apiKeys,
apiKeysCreateInput,
Expand All @@ -12,17 +13,23 @@ import {
apiKeysWhereInput,
apiKeysWhereUniqueInput,
} from '@prisma/client';
import QuickLRU from 'quick-lru';
import { Expose } from '../../modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { StripeService } from '../stripe/stripe.service';
import { TokensService } from '../tokens/tokens.service';

@Injectable()
export class ApiKeysService {
private lru = new QuickLRU<string, apiKeys>({
maxSize: this.configService.get<number>('caching.apiKeyLruSize') ?? 100,
});

constructor(
private prisma: PrismaService,
private tokensService: TokensService,
private stripeService: StripeService,
private configService: ConfigService,
) {}

async createApiKey(
Expand Down Expand Up @@ -72,6 +79,8 @@ export class ApiKeysService {
});
if (!apiKey)
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND);
if (this.lru.has(key)) return this.lru.get(key);
this.lru.set(key, apiKey);
return this.prisma.expose<apiKeys>(apiKey);
}

Expand All @@ -90,6 +99,7 @@ export class ApiKeysService {
where: { id },
data,
});
this.lru.delete(testApiKey.apiKey);
return this.prisma.expose<apiKeys>(apiKey);
}

Expand All @@ -108,6 +118,7 @@ export class ApiKeysService {
where: { id },
data,
});
this.lru.delete(testApiKey.apiKey);
return this.prisma.expose<apiKeys>(apiKey);
}

Expand All @@ -121,6 +132,7 @@ export class ApiKeysService {
const apiKey = await this.prisma.apiKeys.delete({
where: { id },
});
this.lru.delete(testApiKey.apiKey);
return this.prisma.expose<apiKeys>(apiKey);
}

Expand Down

0 comments on commit 8a5c015

Please sign in to comment.