Skip to content

Commit cf82348

Browse files
committed
feat(be): redis caching
1 parent bda6f7d commit cf82348

File tree

9 files changed

+144
-7
lines changed

9 files changed

+144
-7
lines changed

.env.docker.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# get yours @ https://api.golemio.cz/api-keys/auth/sign-up
22
GOLEMIO_API_KEY=
33

4-
4+
# postgres
55
POSTGRES_USER=pg_user
66
POSTGRES_PASSWORD=pg_password
77
POSTGRES_DB=metro-now
@@ -11,6 +11,11 @@ DB_HOST=host.docker.internal
1111
DB_PORT=5432
1212
DB_SCHEMA=public
1313

14+
15+
# redis
16+
REDIS_HOST=host.docker.internal
17+
REDIS_PORT=6379
18+
1419
# This was inserted by `prisma init`:
1520
# Environment variables declared in this file are automatically made available to Prisma.
1621
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

apps/backend/.env.local.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# get yours @ https://api.golemio.cz/api-keys/auth/sign-up
22
GOLEMIO_API_KEY=
33

4-
4+
# postgres
55
POSTGRES_USER=pg_user
66
POSTGRES_PASSWORD=pg_password
77
POSTGRES_DB=metro-now
@@ -21,3 +21,6 @@ DB_SCHEMA=public
2121

2222
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}:${DB_PORT}/${POSTGRES_DB}?schema=${DB_SCHEMA}"
2323

24+
# redis
25+
REDIS_HOST=localhost
26+
REDIS_PORT=6379

apps/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@nestjs/swagger": "^7.4.2",
3636
"@prisma/client": "5.20.0",
3737
"cache-manager": "^5.7.6",
38+
"cache-manager-redis-yet": "^5.1.5",
3839
"graphql": "^16.9.0",
3940
"radash": "^12.1.0",
4041
"reflect-metadata": "^0.2.2",

apps/backend/src/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { StopModule } from "src/modules/stop/stop.module";
2525
LoggerModule,
2626
ConfigModule.forRoot(configModuleConfig),
2727
ScheduleModule.forRoot(),
28-
CacheModule.register(cacheModuleConfig),
28+
CacheModule.registerAsync(cacheModuleConfig),
2929
GraphQLModule.forRoot<ApolloDriverConfig>({
3030
driver: ApolloDriver,
3131
playground: true,
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
import type { CacheModuleOptions } from "@nestjs/cache-manager";
1+
import type { CacheModuleAsyncOptions } from "@nestjs/cache-manager";
2+
import { redisStore } from "cache-manager-redis-yet";
23

3-
export const cacheModuleConfig: CacheModuleOptions = {
4+
export const cacheModuleConfig: CacheModuleAsyncOptions = {
45
isGlobal: true,
6+
useFactory: async () => ({
7+
store: await redisStore({
8+
socket: {
9+
host: process.env.REDIS_HOST || "localhost",
10+
port: parseInt(process.env.REDIS_PORT || "6379"),
11+
},
12+
}),
13+
max: 1_000,
14+
}),
515
};

apps/backend/src/schema/env.schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ export const envSchema = z.object({
88
DB_HOST: z.string(),
99
DB_PORT: z.coerce.number().int().positive(),
1010
DB_SCHEMA: z.string(),
11+
REDIS_PORT: z.coerce.number().int().positive().optional(),
12+
REDIS_HOST: z.string().optional(),
1113
PORT: z.coerce.number().int().positive().optional(),
1214
});
15+
16+
export type EnvSchema = z.infer<typeof envSchema>;

compose.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@ services:
1919
timeout: 5s
2020
retries: 5
2121

22+
redis-stack:
23+
image: redis/redis-stack:latest
24+
container_name: redis-stack
25+
restart: always
26+
ports:
27+
- 6379:6379
28+
- 8001:8001
29+
extra_hosts:
30+
- "host.docker.internal:host-gateway"
31+
2232
postgres:
2333
container_name: postgres
2434
image: postgres:16-alpine
2535
restart: always
2636
ports:
27-
- 5432:${POSTGRES_PORT:-5432}
37+
- 5432:5432
2838
volumes:
2939
- postgres-data:/var/lib/postgresql/data/
3040
env_file:
@@ -44,6 +54,7 @@ services:
4454
pnpm start:prod
4555
depends_on:
4656
- postgres
57+
- redis-stack
4758
build:
4859
context: .
4960
dockerfile: ./Dockerfile

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"precommit": "pnpm format",
1212
"format": "pnpm -r format && pnpm exec prettier . --write",
1313
"format:check": "pnpm -r format:check && pnpm exec prettier . --check",
14-
"docker:db:up": "docker compose up postgres",
14+
"docker:up:dev": "docker compose up postgres redis-stack",
1515
"docker:up": "docker compose up -d --build",
1616
"docker:down": "docker compose down --remove-orphans --volumes"
1717
},

pnpm-lock.yaml

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)