From 47658da81ec6f4cd001bf705056f84ef324ee693 Mon Sep 17 00:00:00 2001 From: Anmol Gangwar Date: Wed, 28 Aug 2024 00:53:11 +0530 Subject: [PATCH] build: add redis storage cache --- src/infra/redis/redis.client.ts | 5 ++-- src/infra/redis/redis.service.ts | 8 ++++++ .../view-image/view-image.usecase.ts | 26 +++++++++++++------ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/infra/redis/redis.client.ts b/src/infra/redis/redis.client.ts index 36d46c3..8744e25 100644 --- a/src/infra/redis/redis.client.ts +++ b/src/infra/redis/redis.client.ts @@ -1,6 +1,5 @@ import { RedisClient } from './dal.redis' -// Creating Redis clients for different URLs -const redisdb = RedisClient.getInstance(process.env.EXERCISEDB_CACHE!, 'REDIS_DB') +const redisStorageCache = RedisClient.getInstance(process.env.EXERCISEDB_STORAGE_CACHE!, 'EXERCISEDB_STORAGE_CACHE') -export { redisdb } +export { redisStorageCache } diff --git a/src/infra/redis/redis.service.ts b/src/infra/redis/redis.service.ts index 0af8d97..4930030 100644 --- a/src/infra/redis/redis.service.ts +++ b/src/infra/redis/redis.service.ts @@ -20,4 +20,12 @@ export class RedisService implements RedisRepositoryInterface { async setWithExpiry(prefix: string, key: string, value: any, expiry: number): Promise { await this.redisClient.set(`${prefix}:${key}`, JSON.stringify(value), 'EX', expiry) } + async getBuffer(prefix: string, key: string): Promise { + const value = await this.redisClient.getBuffer(`${prefix}:${key}`) + return value || null + } + + async setBuffer(prefix: string, key: string, value: Buffer): Promise { + await this.redisClient.set(`${prefix}:${key}`, value) + } } diff --git a/src/modules/images/use-cases/view-image/view-image.usecase.ts b/src/modules/images/use-cases/view-image/view-image.usecase.ts index 5a36810..0d63687 100644 --- a/src/modules/images/use-cases/view-image/view-image.usecase.ts +++ b/src/modules/images/use-cases/view-image/view-image.usecase.ts @@ -1,29 +1,39 @@ import { IUseCase } from '#common/types/use-case.type.js' +import { redisStorageCache } from '#infra/redis/redis.client.js' +import { RedisService } from '#infra/redis/redis.service.js' import axios, { AxiosResponse } from 'axios' import { HTTPException } from 'hono/http-exception' -export class ViewImageUseCase implements IUseCase { - constructor() {} +export class ViewImageUseCase implements IUseCase { + private readonly redisService: RedisService + constructor() { + this.redisService = new RedisService(redisStorageCache) + } - async execute(fileName: string): Promise { + async execute(fileName: string): Promise { + const cacheKey = fileName.endsWith('.gif') ? fileName : `${fileName}.gif` const sourceUrl = fileName.endsWith('.gif') ? `${process.env.SUPABASE_BUCKET_URL}/${process.env.SUPABASE_BUCKET_NAME}/${fileName}` : `${process.env.SUPABASE_BUCKET_URL}/${process.env.SUPABASE_BUCKET_NAME}/${fileName}.gif` try { + const cachedImage = await this.redisService.getBuffer('image', cacheKey) + + if (cachedImage) { + return cachedImage + } const response = await axios.get(sourceUrl, { responseType: 'arraybuffer' }) - if (response.status === 404 || response.status === 400) { + if (response.status === 404 || response.status === 400 || response.status !== 200) { throw new HTTPException(404, { message: 'Image not found or URL expired' }) } - if (response.status !== 200) { - throw new HTTPException(400, { message: 'Image not found or URL expired' }) - } + const imageBuffer = response.data + await this.redisService.setBuffer('image', cacheKey, imageBuffer) - return response.data + return imageBuffer } catch (error) { if (axios.isAxiosError(error)) { const statusCode = error.response?.status