Skip to content

Feature: Backend/cache: Allow to use redis cache instead as memory #832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 71 additions & 5 deletions backend/package-lock.json

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

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.787.0",
"@keyv/redis": "^4.4.0",
"@nestjs/cache-manager": "^3.0.1",
"@nestjs/common": "^11.0.17",
"@nestjs/config": "^4.0.2",
Expand All @@ -30,6 +31,7 @@
"argon2": "^0.41.1",
"body-parser": "^2.2.0",
"cache-manager": "^6.4.2",
"cacheable": "^1.9.0",
"clamscan": "^2.4.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down
25 changes: 22 additions & 3 deletions backend/prisma/seed/config.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ export const configVariables = {
secret: false,
},
},
cache: {
"redis-enabled": {
type: "boolean",
defaultValue: "false",
},
"redis-url": {
type: "string",
defaultValue: "redis://pingvin-redis:6379",
secret: true,
},
ttl: {
type: "number",
defaultValue: "60",
},
maxItems: {
type: "number",
defaultValue: "1000",
},
},
email: {
enableShareEmailRecipients: {
type: "boolean",
Expand Down Expand Up @@ -419,11 +438,11 @@ const prisma = new PrismaClient({

async function seedConfigVariables() {
for (const [category, configVariablesOfCategory] of Object.entries(
configVariables
configVariables,
)) {
let order = 0;
for (const [name, properties] of Object.entries(
configVariablesOfCategory
configVariablesOfCategory,
)) {
const existingConfigVariable = await prisma.config.findUnique({
where: { name_category: { name, category } },
Expand Down Expand Up @@ -469,7 +488,7 @@ async function migrateConfigVariables() {
// Update the config variable if it exists in the seed
} else {
const variableOrder = Object.keys(
configVariables[existingConfigVariable.category]
configVariables[existingConfigVariable.category],
).indexOf(existingConfigVariable.name);
await prisma.config.update({
where: {
Expand Down
6 changes: 2 additions & 4 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Module } from "@nestjs/common";
import { ScheduleModule } from "@nestjs/schedule";
import { AuthModule } from "./auth/auth.module";

import { CacheModule } from "@nestjs/cache-manager";
import { APP_GUARD } from "@nestjs/core";
import { ThrottlerGuard, ThrottlerModule } from "@nestjs/throttler";
import { AppCacheModule } from "./cache/cache.module";
import { AppController } from "./app.controller";
import { ClamScanModule } from "./clamscan/clamscan.module";
import { ConfigModule } from "./config/config.module";
Expand Down Expand Up @@ -38,9 +38,7 @@ import { UserModule } from "./user/user.module";
ClamScanModule,
ReverseShareModule,
OAuthModule,
CacheModule.register({
isGlobal: true,
}),
AppCacheModule,
],
controllers: [AppController],
providers: [
Expand Down
41 changes: 41 additions & 0 deletions backend/src/cache/cache.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Module } from "@nestjs/common";
import { CacheModule } from "@nestjs/cache-manager";
import { CacheableMemory } from "cacheable";
import { createKeyv } from "@keyv/redis";
import { Keyv } from "keyv";
import { ConfigModule } from "src/config/config.module";
import { ConfigService } from "src/config/config.service";

@Module({
imports: [
ConfigModule,
CacheModule.registerAsync({
isGlobal: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const useRedis = configService.get("cache.redis-enabled");
const ttl = configService.get("cache.ttl");
const max = configService.get("cache.maxItems");

let config = {
ttl,
max,
stores: [],
};

if (useRedis) {
const redisUrl = configService.get("cache.redis-url");
config.stores = [
new Keyv({ store: new CacheableMemory({ ttl, lruSize: 5000 }) }),
createKeyv(redisUrl),
];
}

return config;
},
}),
],
exports: [CacheModule],
})
export class AppCacheModule {}
11 changes: 10 additions & 1 deletion config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ share:
shareIdLength: "8"
#Maximum share size
maxSize: "1000000000"
#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.
#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.
zipCompressionLevel: "9"
#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.
chunkSize: "10000000"
#The share creation modal automatically appears when a user selects files, eliminating the need to manually click the button.
autoOpenShareModal: "false"
cache:
#Normally Pingvin Share caches information in memory. If you run multiple instances of Pingvin Share, you need to enable Redis caching to share the cache between the instances.
redis-enabled: "false"
#Url to connect to the Redis instance used for caching.
redis-url: redis://pingvin-redis:6379
#Time in second to keep information inside the cache.
ttl: "60"
#Maximum number of items inside the cache.
maxItems: "1000"
email:
#Whether to allow email sharing with recipients. Only enable this if SMTP is activated.
enableShareEmailRecipients: "false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import Link from "next/link";
import { Dispatch, SetStateAction } from "react";
import {
TbAt,
TbBinaryTree,
TbBucket,
TbMail,
TbScale,
TbServerBolt,
TbSettings,
TbShare,
TbSocial,
TbBucket,
TbBinaryTree,
TbSettings,
TbScale,
} from "react-icons/tb";
import { FormattedMessage } from "react-intl";

Expand All @@ -32,6 +33,7 @@ const categories = [
{ name: "LDAP", icon: <TbBinaryTree /> },
{ name: "S3", icon: <TbBucket /> },
{ name: "Legal", icon: <TbScale /> },
{ name: "Cache", icon: <TbServerBolt /> },
];

const useStyles = createStyles((theme) => ({
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/i18n/translations/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ export default {
"admin.config.title": "Configuration",
"admin.config.category.general": "General",
"admin.config.category.share": "Share",
"admin.config.category.cache": "Cache",
"admin.config.category.email": "Email",
"admin.config.category.smtp": "SMTP",
"admin.config.category.oauth": "Social Login",
Expand All @@ -446,6 +447,19 @@ export default {
"Change your logo by uploading a new image. The image must be a PNG and should have the format 1:1.",
"admin.config.general.logo.placeholder": "Pick image",

"admin.config.cache.ttl": "TTL",
"admin.config.cache.ttl.description":
"Time in second to keep information inside the cache.",
"admin.config.cache.max-items": "Maximum items",
"admin.config.cache.max-items.description":
"Maximum number of items inside the cache.",
"admin.config.cache.redis-enabled": "Redis enabled",
"admin.config.cache.redis-enabled.description":
"Normally Pingvin Share caches information in memory. If you run multiple instances of Pingvin Share, you need to enable Redis caching to share the cache between the instances.",
"admin.config.cache.redis-url": "Redis URL",
"admin.config.cache.redis-url.description":
"Url to connect to the Redis instance used for caching.",

"admin.config.email.enable-share-email-recipients":
"Enable email recipient sharing",
"admin.config.email.enable-share-email-recipients.description":
Expand Down