[backend] add cache storage module (#4320)

* [backend] add cache storage module

* update docs

* update default TTL to a week
This commit is contained in:
Weiko
2024-03-07 14:07:01 +01:00
committed by GitHub
parent e7733a1b7a
commit 41bed57be9
12 changed files with 348 additions and 9 deletions
@@ -0,0 +1,46 @@
import { CacheModuleOptions } from '@nestjs/common';
import { redisStore } from 'cache-manager-redis-yet';
import { CacheStorageType } from 'src/integrations/cache-storage/types/cache-storage-type.enum';
import { EnvironmentService } from 'src/integrations/environment/environment.service';
export const cacheStorageModuleFactory = (
environmentService: EnvironmentService,
): CacheModuleOptions => {
const cacheStorageType = environmentService.getCacheStorageType();
const cacheStorageTtl = environmentService.getCacheStorageTtl();
const cacheModuleOptions: CacheModuleOptions = {
isGlobal: true,
ttl: cacheStorageTtl * 1000,
};
switch (cacheStorageType) {
case CacheStorageType.Memory: {
return cacheModuleOptions;
}
case CacheStorageType.Redis: {
const host = environmentService.getRedisHost();
const port = environmentService.getRedisPort();
if (!(host && port)) {
throw new Error(
`${cacheStorageType} cache storage requires host: ${host} and port: ${port} to be defined, check your .env file`,
);
}
return {
...cacheModuleOptions,
store: redisStore,
socket: {
host,
port,
},
};
}
default:
throw new Error(
`Invalid cache-storage (${cacheStorageType}), check your .env file`,
);
}
};