Add bull and caching

This commit is contained in:
2023-11-20 15:25:07 -07:00
parent 5a54a9ca5b
commit 725cabefe6
7 changed files with 339 additions and 29 deletions

View File

@@ -1,4 +1,6 @@
import { Injectable } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Client, ItemBucketMetadata, UploadedObjectInfo } from 'minio';
@@ -7,7 +9,10 @@ export class MinioService {
private readonly client: Client;
public readonly defaultBucketName: string;
constructor(private readonly configService: ConfigService) {
constructor(
private readonly configService: ConfigService,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {
this.client = new Client({
endPoint: this.configService.get<string>('S3_ENDPOINT', 's3.hooli.co'),
port: this.configService.get<number>('S3_PORT', 443),
@@ -46,6 +51,21 @@ export class MinioService {
);
}
public async getCachedBuffer(
bucketName: string,
objectName: string,
cacheTtl: number = 0.1,
): Promise<Buffer> {
const cacheKey = `${bucketName}/${objectName}`;
const cachedValue = await this.cacheManager.get<Buffer>(cacheKey);
if (cachedValue) {
return Buffer.from(cachedValue);
}
const object = await this.getBuffer(bucketName, objectName);
this.cacheManager.set(cacheKey, object, cacheTtl);
return object;
}
public async getBuffer(
bucketName: string,
objectName: string,