mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-25 14:11:38 +10:00
fix(i18n): delete local translations
This commit is contained in:
@ -1,18 +1,21 @@
|
||||
import { CACHE_MANAGER } from "@nestjs/cache-manager";
|
||||
import { Inject, Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
|
||||
import { Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { Cache } from "cache-manager";
|
||||
import { RedisService } from "@songkeys/nestjs-redis";
|
||||
import Redis from "ioredis";
|
||||
|
||||
import { Config } from "../config/schema";
|
||||
|
||||
@Injectable()
|
||||
export class UtilsService {
|
||||
private readonly redis: Redis;
|
||||
logger = new Logger(UtilsService.name);
|
||||
|
||||
constructor(
|
||||
private readonly redisService: RedisService,
|
||||
private readonly configService: ConfigService<Config>,
|
||||
@Inject(CACHE_MANAGER) private readonly cache: Cache,
|
||||
) {}
|
||||
) {
|
||||
this.redis = this.redisService.getClient();
|
||||
}
|
||||
|
||||
getUrl(): string {
|
||||
const url =
|
||||
@ -27,28 +30,34 @@ export class UtilsService {
|
||||
return url;
|
||||
}
|
||||
|
||||
async getCachedOrSet<T>(key: string, callback: () => Promise<T>, ttl?: number): Promise<T> {
|
||||
async getCachedOrSet<T>(
|
||||
key: string,
|
||||
callback: () => Promise<T> | T,
|
||||
ttl: number = 1000 * 60 * 60 * 24, // 24 hours
|
||||
type: "json" | "string" = "json",
|
||||
): Promise<T> {
|
||||
// Try to get the value from the cache
|
||||
const start = performance.now();
|
||||
const cachedValue = await this.cache.get<T>(key);
|
||||
const cachedValue = await this.redis.get(key);
|
||||
const duration = Number(performance.now() - start).toFixed(0);
|
||||
|
||||
if (cachedValue === undefined) {
|
||||
if (!cachedValue) {
|
||||
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 (cachedValue) {
|
||||
return (type === "string" ? cachedValue : JSON.parse(cachedValue)) as T;
|
||||
}
|
||||
|
||||
// If the value is not in the cache, run the callback
|
||||
const value = await callback();
|
||||
const valueToCache = (type === "string" ? value : JSON.stringify(value)) as string;
|
||||
|
||||
// Store the value in the cache
|
||||
await this.cache.set(key, value, ttl);
|
||||
await this.redis.set(key, valueToCache, "PX", ttl);
|
||||
|
||||
// Return the value
|
||||
return value;
|
||||
|
||||
Reference in New Issue
Block a user