Skip to content

Commit

Permalink
Feature: Cache with Redis (#20)
Browse files Browse the repository at this point in the history
* cache with providers

* linting, and ordering fixes

* use redis password and set ttl to much shorter time

---------

Co-authored-by: Kaspar Kallas <[email protected]>
  • Loading branch information
tokdaniel and kasparkallas authored Nov 3, 2023
1 parent 05d0060 commit c40936a
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 8 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.

43 changes: 37 additions & 6 deletions 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 } 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 @@ -19,12 +22,12 @@ const registerConfigModule = () =>
const registerBullModule = () =>
BullModule.forRootAsync({
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
useFactory: async (config: ConfigService) => ({
connection: {
host: configService.getOrThrow('REDIS_HOST'),
port: configService.getOrThrow('REDIS_PORT'),
password: configService.get('REDIS_PASSWORD'),
username: configService.get('REDIS_USER'),
host: config.getOrThrow('REDIS_HOST'),
port: config.getOrThrow('REDIS_PORT'),
password: config.get('REDIS_PASSWORD'),
username: config.get('REDIS_USER'),
},
defaultJobOptions: {
attempts: 3,
Expand All @@ -36,9 +39,32 @@ const registerBullModule = () =>
}),
});

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

return {
isGlobal: true,
store: store as unknown as CacheStore, // Nest.js hasn't caught up with right types
ttl: 3000, // In cache-manager v5, TTL is configured in milliseconds: https://docs.nestjs.com/techniques/caching
};
},
});

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

0 comments on commit c40936a

Please sign in to comment.