refactor(v4.0.0-alpha): beginning of a new era

This commit is contained in:
Amruth Pillai
2023-11-05 12:31:42 +01:00
parent 0ba6a444e2
commit 22933bd412
505 changed files with 81829 additions and 0 deletions

View File

@ -0,0 +1,10 @@
import { Global, Module } from "@nestjs/common";
import { UtilsService } from "./utils.service";
@Global()
@Module({
providers: [UtilsService],
exports: [UtilsService],
})
export class UtilsModule {}

View File

@ -0,0 +1,56 @@
import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Inject, Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Cache } from "cache-manager";
import { Config } from "../config/schema";
@Injectable()
export class UtilsService {
logger = new Logger(UtilsService.name);
constructor(
private readonly configService: ConfigService<Config>,
@Inject(CACHE_MANAGER) private readonly cache: Cache,
) {}
getUrl(): string {
const url =
this.configService.get("NODE_ENV") === "production"
? this.configService.get("PUBLIC_URL")
: this.configService.get("__DEV__CLIENT_URL");
if (!url) {
throw new InternalServerErrorException("No PUBLIC_URL or __DEV__CLIENT_URL was found.");
}
return url;
}
async getCachedOrSet<T>(key: string, callback: () => Promise<T>, ttl?: number): Promise<T> {
// Try to get the value from the cache
const start = performance.now();
const cachedValue = await this.cache.get<T>(key);
const duration = Number(performance.now() - start).toFixed(0);
if (cachedValue === undefined) {
this.logger.debug(`Cache Key "${key}": miss`);
} else {
this.logger.debug(`Cache Key "${key}": hit - ${duration}ms`);
}
// If the value is in the cache, return it
if (cachedValue !== undefined) {
return cachedValue;
}
// If the value is not in the cache, run the callback
const value = await callback();
// Store the value in the cache
await this.cache.set(key, value, ttl);
// Return the value
return value;
}
}