implement redis cache for context
This commit is contained in:
@@ -2,6 +2,7 @@ import { Global, MiddlewareConsumer, Module } from '@nestjs/common';
|
||||
|
||||
import { AdminNoteModule } from '../AdminNote/adminnote.module';
|
||||
import { ApolloDriverConfig } from '@nestjs/apollo';
|
||||
import { AppConfigModule } from '../AppConfig/appconfig.module';
|
||||
import { Builder } from './graphql.builder';
|
||||
import { CategoryModule } from '../Category/category.module';
|
||||
import { CenterMentorModule } from '../CenterMentor/centermentor.module';
|
||||
@@ -21,6 +22,8 @@ import { PothosModule } from '@smatch-corp/nestjs-pothos';
|
||||
import { PrismaCrudGenerator } from './graphql.generator';
|
||||
import { PrismaModule } from '../Prisma/prisma.module';
|
||||
import { PrismaService } from '../Prisma/prisma.service';
|
||||
import { RedisModule } from 'src/Redis/redis.module';
|
||||
import { RedisService } from 'src/Redis/redis.service';
|
||||
import { RefundTicketModule } from '../RefundTicket/refundticket.module';
|
||||
import { Request } from 'express';
|
||||
import { ResumeModule } from '../Resume/resume.module';
|
||||
@@ -42,6 +45,8 @@ import { initContextCache } from '@pothos/core';
|
||||
imports: [
|
||||
CommonModule,
|
||||
PrismaModule,
|
||||
RedisModule,
|
||||
AppConfigModule,
|
||||
UserModule,
|
||||
CenterModule,
|
||||
ServiceModule,
|
||||
@@ -93,8 +98,9 @@ import { initContextCache } from '@pothos/core';
|
||||
providers: [
|
||||
{
|
||||
provide: GraphqlService,
|
||||
useFactory: (prisma: PrismaService) => new GraphqlService(prisma),
|
||||
inject: [PrismaService],
|
||||
useFactory: (prisma: PrismaService, redis: RedisService) =>
|
||||
new GraphqlService(prisma, redis),
|
||||
inject: [PrismaService, 'REDIS_CLIENT'],
|
||||
},
|
||||
{
|
||||
provide: Builder,
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
import { Injectable, Logger, UnauthorizedException } from '@nestjs/common';
|
||||
import {
|
||||
Inject,
|
||||
Injectable,
|
||||
Logger,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { PrismaService } from '../Prisma/prisma.service';
|
||||
import { Request } from 'express';
|
||||
import { clerkClient } from '@clerk/express';
|
||||
|
||||
import { RedisService } from '../Redis/redis.service';
|
||||
|
||||
@Injectable()
|
||||
export class GraphqlService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
@Inject('REDIS_CLIENT') private readonly redis: RedisService,
|
||||
) {}
|
||||
|
||||
async acquireContext(req: Request) {
|
||||
// get x-session-id from headers
|
||||
@@ -24,6 +34,11 @@ export class GraphqlService {
|
||||
if (disableAuth) {
|
||||
return null;
|
||||
}
|
||||
// redis context cache
|
||||
const cachedUser = await this.redis.getUser(sessionId);
|
||||
if (cachedUser) {
|
||||
return cachedUser;
|
||||
}
|
||||
// check if the token is valid
|
||||
const session = await clerkClient.sessions.getSession(sessionId as string);
|
||||
if (!session) {
|
||||
@@ -35,7 +50,7 @@ export class GraphqlService {
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('User not found');
|
||||
}
|
||||
Logger.log(`User ${user.name} with id ${user.id} acquired context`);
|
||||
await this.redis.setUser(sessionId, user, session.expireAt);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user