feat: allow to use redis cache instead of memory cache (#832)
* feat(backend/cache): allow to use redis cache instead as memory * feat(frontend/admin): add cache section Add a new section for cache attributes. Also add US translation. --------- Co-authored-by: Jules Lefebvre <jules.lefebvre@diabolocom.com>
This commit is contained in:
41
backend/src/cache/cache.module.ts
vendored
Normal file
41
backend/src/cache/cache.module.ts
vendored
Normal file
@@ -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 {}
|
||||
Reference in New Issue
Block a user