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

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