Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/cache/custom-cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { Reflector } from '@nestjs/core';
import { GqlExecutionContext } from '@nestjs/graphql';

import { Observable, lastValueFrom } from 'rxjs';
import { Observable, from, of, switchMap, tap } from 'rxjs';

import { CUSTOM_CACHE, CustomCacheOptions } from './custom-cache.decorator';
import { CustomCacheService } from './custom-cache.service';
Expand All @@ -19,10 +19,7 @@ export class CustomCacheInterceptor implements NestInterceptor {
private readonly reflector: Reflector,
) {}

async intercept(
context: ExecutionContext,
next: CallHandler,
): Promise<Observable<any>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const handler = context.getHandler();
const options = this.reflector.get<CustomCacheOptions>(
CUSTOM_CACHE,
Expand All @@ -33,24 +30,31 @@ export class CustomCacheInterceptor implements NestInterceptor {
return next.handle();
}

const args = this.getArgs(context);

const { key, ttl, logger } = options;
const customKey = `${context.getClass().name}.${handler.name}`;
const result = async () => await lastValueFrom(next.handle());

await this.customCacheService.setCache({
options,
const args = this.getArgs(context);
const cacheKey = this.customCacheService.buildCacheKey(
key ?? customKey,
args,
result,
customKey,
});
);

return next.handle();
return from(this.customCacheService.getCache(cacheKey, logger)).pipe(
switchMap((cached) =>
cached !== undefined
? of(cached)
: next
.handle()
.pipe(
tap((data) =>
this.customCacheService.setCache(cacheKey, data, ttl, logger),
),
),
),
);
}

private getArgs(context: ExecutionContext): unknown[] {
const ctx = GqlExecutionContext.create(context);
const req = ctx.getContext().req;
return req.body;
return Object.values(ctx.getArgs());
}
}
54 changes: 33 additions & 21 deletions src/cache/custom-cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class CustomCacheService {
const methodOverride = async (...args: unknown[]) => {
const result = async () => await methodRef.apply(instance, args);

return this.setCache({ customKey, options, result, args });
return this.getOrSetCache(customKey, args, options, result);
};

Object.defineProperty(instance, methodName, {
Expand All @@ -53,32 +53,44 @@ export class CustomCacheService {
};
}

async setCache({
options,
args,
result: _result,
customKey,
}: {
options: CustomCacheOptions;
args: unknown[];
result: () => Promise<unknown>;
customKey: string;
}) {
const { key: cacheKey = customKey, ttl = Infinity, logger } = options;
async getCache(key: string, logger?: CustomCacheOptions['logger']) {
const cached = await this.cacheManager.get(key);
if (cached !== undefined) {
logger?.('Cache Hit', { cacheKey: key });
}
return cached;
}

async setCache(
key: string,
data: unknown,
ttl: number = Infinity,
logger?: CustomCacheOptions['logger'],
) {
await this.cacheManager.set(key, data, ttl);
logger?.('Cached', { cacheKey: key });
}

const argsAddedKey = cacheKey + JSON.stringify(args);
buildCacheKey(customKey: string, args: unknown[]) {
return customKey + JSON.stringify(args);
}

const cachedValue = await this.cacheManager.get(argsAddedKey);
if (Boolean(cachedValue)) {
logger?.('Cache Hit', { cacheKey });
async getOrSetCache(
customKey: string,
args: unknown[],
options: CustomCacheOptions,
resultFn: () => Promise<unknown>,
) {
const { key: cacheKey = customKey, ttl = Infinity, logger } = options;
const argsAddedKey = this.buildCacheKey(cacheKey, args);

const cachedValue = await this.getCache(argsAddedKey, logger);
if (cachedValue !== undefined) {
return cachedValue;
}

const result = await _result();

await this.cacheManager.set(argsAddedKey, result, ttl);
logger?.('Cached', { cacheKey });
const result = await resultFn();
await this.setCache(argsAddedKey, result, ttl, logger);

return result;
}
Expand Down