Skip to content

Commit 413699a

Browse files
author
Jules Lefebvre
committed
feat(backend/cache): allow to use redis cache instead as memory
1 parent 3d16566 commit 413699a

File tree

6 files changed

+144
-10
lines changed

6 files changed

+144
-10
lines changed

backend/package-lock.json

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

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"dependencies": {
1616
"@aws-sdk/client-s3": "^3.787.0",
17+
"@keyv/redis": "^4.4.0",
1718
"@nestjs/cache-manager": "^3.0.1",
1819
"@nestjs/common": "^11.0.17",
1920
"@nestjs/config": "^4.0.2",
@@ -30,6 +31,7 @@
3031
"argon2": "^0.41.1",
3132
"body-parser": "^2.2.0",
3233
"cache-manager": "^6.4.2",
34+
"cacheable": "^1.9.0",
3335
"clamscan": "^2.4.0",
3436
"class-transformer": "^0.5.1",
3537
"class-validator": "^0.14.1",

backend/prisma/seed/config.seed.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ export const configVariables = {
7676
secret: false,
7777
},
7878
},
79+
cache: {
80+
ttl: {
81+
type: "number",
82+
defaultValue: "60",
83+
},
84+
maxItems: {
85+
type: "number",
86+
defaultValue: "1000"
87+
},
88+
"redis-enabled": {
89+
type: "boolean",
90+
defaultValue: "false"
91+
},
92+
"redis-url": {
93+
type: "string",
94+
defaultValue: "redis://pingvin-redis:6379",
95+
}
96+
},
7997
email: {
8098
enableShareEmailRecipients: {
8199
type: "boolean",

backend/src/app.module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Module } from "@nestjs/common";
33
import { ScheduleModule } from "@nestjs/schedule";
44
import { AuthModule } from "./auth/auth.module";
55

6-
import { CacheModule } from "@nestjs/cache-manager";
76
import { APP_GUARD } from "@nestjs/core";
87
import { ThrottlerGuard, ThrottlerModule } from "@nestjs/throttler";
8+
import { AppCacheModule } from "./cache/cache.module";
99
import { AppController } from "./app.controller";
1010
import { ClamScanModule } from "./clamscan/clamscan.module";
1111
import { ConfigModule } from "./config/config.module";
@@ -38,9 +38,7 @@ import { UserModule } from "./user/user.module";
3838
ClamScanModule,
3939
ReverseShareModule,
4040
OAuthModule,
41-
CacheModule.register({
42-
isGlobal: true,
43-
}),
41+
AppCacheModule,
4442
],
4543
controllers: [AppController],
4644
providers: [

backend/src/cache/cache.module.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Global, Module } from "@nestjs/common";
2+
import { CacheModule } from "@nestjs/cache-manager";
3+
import { ConfigModule, ConfigService } from "@nestjs/config";
4+
import { CacheableMemory } from "cacheable";
5+
import { createKeyv } from "@keyv/redis";
6+
import { Keyv } from "keyv";
7+
8+
@Global()
9+
@Module({
10+
imports: [
11+
ConfigModule,
12+
CacheModule.registerAsync({
13+
isGlobal: true,
14+
imports: [ConfigModule],
15+
inject: [ConfigService],
16+
useFactory: async (configService: ConfigService) => {
17+
const useRedis = configService.get<boolean>("cache.redis-enabled");
18+
const ttl = configService.get<number>("cache.ttl");
19+
const max = configService.get<number>("cache.maxItems");
20+
21+
let config = {
22+
ttl,
23+
max,
24+
stores: [],
25+
};
26+
27+
if (useRedis) {
28+
const redisUrl = configService.get<string>("cache.redis-url");
29+
config.stores = [
30+
new Keyv({ store: new CacheableMemory({ ttl, lruSize: 5000 }) }),
31+
createKeyv(redisUrl),
32+
];
33+
}
34+
35+
return config;
36+
},
37+
}),
38+
],
39+
exports: [CacheModule],
40+
})
41+
export class AppCacheModule {}

config.example.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ share:
2323
shareIdLength: "8"
2424
#Maximum share size
2525
maxSize: "1000000000"
26-
#Adjust the level to balance between file size and compression speed. Valid values range from 0 to 9, with 0 being no compression and 9 being maximum compression.
26+
#Adjust the level to balance between file size and compression speed. Valid values range from 0 to 9, with 0 being no compression and 9 being maximum compression.
2727
zipCompressionLevel: "9"
2828
#Adjust the chunk size for your uploads to balance efficiency and reliability according to your internet connection. Smaller chunks can enhance success rates for unstable connections, while larger chunks make uploads faster for stable connections.
2929
chunkSize: "10000000"
3030
#The share creation modal automatically appears when a user selects files, eliminating the need to manually click the button.
3131
autoOpenShareModal: "false"
32+
cache:
33+
# Adjuject the len
34+
ttl: 60
35+
#Maxiumum number of items
36+
maxItems: 1000
37+
#Whether to enable redis share cache
38+
redis-enabled: false
39+
#Url to the redis instance
40+
redis-url: redis://pingvin-redis:6379
3241
email:
3342
#Whether to allow email sharing with recipients. Only enable this if SMTP is activated.
3443
enableShareEmailRecipients: "false"

0 commit comments

Comments
 (0)