chore(server): cache blob list result (#6297)

This commit is contained in:
liuyi 2024-03-26 13:49:24 +08:00 committed by LongYinan
parent 0731872347
commit b8e6d7d6cb
No known key found for this signature in database
GPG Key ID: C3666B7FC82ADAD7

View File

@ -1,13 +1,13 @@
import { Injectable } from '@nestjs/common';
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import type {
BlobInputType,
EventPayload,
StorageProvider,
} from '../../../fundamentals';
import {
BlobInputType,
Cache,
EventEmitter,
type EventPayload,
ListObjectsMetadata,
OnEvent,
StorageProvider,
StorageProviderFactory,
} from '../../../fundamentals';
@ -17,13 +17,15 @@ export class WorkspaceBlobStorage {
constructor(
private readonly event: EventEmitter,
private readonly storageFactory: StorageProviderFactory
private readonly storageFactory: StorageProviderFactory,
@Inject(forwardRef(() => Cache)) private readonly cache: Cache
) {
this.provider = this.storageFactory.create('blob');
}
async put(workspaceId: string, key: string, blob: BlobInputType) {
await this.provider.put(`${workspaceId}/${key}`, blob);
await this.cache.delete(`blobs:${workspaceId}`);
}
async get(workspaceId: string, key: string) {
@ -31,6 +33,16 @@ export class WorkspaceBlobStorage {
}
async list(workspaceId: string) {
const cachedList = await this.cache.list<ListObjectsMetadata>(
`blobs:${workspaceId}`,
0,
-1
);
if (cachedList.length > 0) {
return cachedList;
}
const blobs = await this.provider.list(workspaceId + '/');
blobs.forEach(item => {
@ -38,6 +50,8 @@ export class WorkspaceBlobStorage {
item.key = item.key.slice(workspaceId.length + 1);
});
await this.cache.pushBack(`blobs:${workspaceId}`, ...blobs);
return blobs;
}