Skip to content

Commit

Permalink
cache with providers
Browse files Browse the repository at this point in the history
  • Loading branch information
tokdaniel committed Nov 3, 2023
1 parent a5cea92 commit 056bfc1
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
3 changes: 3 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@bull-board/nestjs": "^5.9.1",
"@golevelup/nestjs-stripe": "^0.6.3",
"@nestjs/bullmq": "^10.0.1",
"@nestjs/cache-manager": "^2.1.1",
"@nestjs/common": "^10.2.7",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.7",
Expand All @@ -60,6 +61,8 @@
"@superfluid-finance/widget": "^0.4.7",
"@types/lodash": "^4.14.200",
"bullmq": "^4.12.6",
"cache-manager": "^5.2.4",
"cache-manager-redis-store": "^3.0.1",
"date-fns": "^2.30.0",
"lodash": "^4.17.21",
"openapi-fetch": "^0.8.1",
Expand Down
111 changes: 109 additions & 2 deletions apps/backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Module } from '@nestjs/common';

import { ConfigModule, ConfigService } from '@nestjs/config';
import { BullModule } from '@nestjs/bullmq';
import { RedisStore, redisStore } from 'cache-manager-redis-store';
import { CacheInterceptor, CacheModule, CacheStore } from '@nestjs/cache-manager';
import { QueueDashboardModule } from './queue-dashboard/queue-dashboard.module';
import { CheckoutSessionModule } from './checkout-session/checkout-session.module';
import { StripeListenerModule } from './stripe-listener/stripe-listener.module';
Expand All @@ -10,6 +12,7 @@ import { SuperTokenAccountingModule } from './super-token-accounting/super-token
import { SuperfluidStripeConverterModule } from './superfluid-stripe-converter/superfluid-stripe-converter.module';
import { HealthModule } from './health/health.module';
import { registerStripeModule } from './stripe-module-config';
import { APP_INTERCEPTOR } from '@nestjs/core';

const registerConfigModule = () =>
ConfigModule.forRoot({
Expand All @@ -36,8 +39,30 @@ const registerBullModule = () =>
}),
});

const registerCacheModule = () =>
CacheModule.registerAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => {
const store = await redisStore({
socket: {
host: config.get('REDIS_HOST'),
port: +config.get('REDIS_PORT'),
},
// password: config.get('REDIS_PASSWORD'),
});

return {
isGlobal: true,
store: store as unknown as CacheStore,
ttl: 60 * 60 * 24 * 7,
};
},
inject: [ConfigService],
});

@Module({
imports: [
registerCacheModule(),
registerConfigModule(),
registerStripeModule(),
registerBullModule(),
Expand All @@ -50,6 +75,11 @@ const registerBullModule = () =>
HealthModule,
],
controllers: [],
providers: [],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CacheInterceptor,
},
],
})
export class AppModule {}

0 comments on commit 056bfc1

Please sign in to comment.