diff --git a/.changeset/poor-bears-boil.md b/.changeset/poor-bears-boil.md new file mode 100644 index 000000000..a5251729a --- /dev/null +++ b/.changeset/poor-bears-boil.md @@ -0,0 +1,5 @@ +--- +"@hera/plugin-rental": patch +--- + +record pdf cache diff --git a/packages/plugins/@hera/plugin-rental/package.json b/packages/plugins/@hera/plugin-rental/package.json index ca2753b12..79aee0c32 100644 --- a/packages/plugins/@hera/plugin-rental/package.json +++ b/packages/plugins/@hera/plugin-rental/package.json @@ -16,6 +16,7 @@ "exceljs": "^4.4.0", "file-saver": "^2.0.5", "flatted": "^3.2.9", + "get-stream": "^6.0.1", "lodash": "4.17.21", "qrcode": "^1.5.1", "qrcode.react": "^3.1.0", @@ -26,6 +27,7 @@ "peerDependencies": { "@hera/plugin-core": "workspace:*", "@nocobase/actions": "workspace:*", + "@nocobase/cache": "workspace:*", "@nocobase/client": "workspace:*", "@nocobase/database": "workspace:*", "@nocobase/plugin-collection-manager": "workspace:*", diff --git a/packages/plugins/@hera/plugin-rental/src/server/actions/records-controller.ts b/packages/plugins/@hera/plugin-rental/src/server/actions/records-controller.ts index f79631a2f..11c211061 100644 --- a/packages/plugins/@hera/plugin-rental/src/server/actions/records-controller.ts +++ b/packages/plugins/@hera/plugin-rental/src/server/actions/records-controller.ts @@ -7,6 +7,10 @@ import { Movement } from '../../utils/constants'; import _ from 'lodash'; import { FilterParser, Repository } from '@nocobase/database'; import { CollectionRepository } from '@nocobase/plugin-collection-manager'; +import { Cache } from '@nocobase/cache'; +import { getStreamAsBuffer } from 'get-stream'; +import { stringify } from 'flatted'; + @Controller('records') export class RecordPreviewController { @Inject(() => SqlLoader) @@ -221,7 +225,24 @@ export class RecordPreviewController { ? settingType : pdfExplain[0]?.record_print_setup; const double = isDouble === '0' || isDouble === '1' ? isDouble : pdfExplain[0].record_columns; - ctx.body = await this.recordPdfService.transformPdfV2(record, leaseData, feeData, { isDouble: double, printSetup }); + + const cache = ctx.app.cacheManager.getCache('@hera/plugin-rental') as Cache; + const key = stringify({ record, leaseData, feeData, settings: { isDouble: double, printSetup } }); + + const result = await cache.get(key); + if (result) { + if (Buffer.isBuffer(result)) { + ctx.body = result; + } else { + ctx.body = Buffer.from(result.data); + } + } else { + const buf = await getStreamAsBuffer( + await this.recordPdfService.transformPdfV2(record, leaseData, feeData, { isDouble: double, printSetup }), + ); + ctx.body = buf; + await cache.set(key, buf); + } } @Action('unused') diff --git a/packages/plugins/@hera/plugin-rental/src/server/plugin.ts b/packages/plugins/@hera/plugin-rental/src/server/plugin.ts index 96bd8b6ac..57cd896e7 100644 --- a/packages/plugins/@hera/plugin-rental/src/server/plugin.ts +++ b/packages/plugins/@hera/plugin-rental/src/server/plugin.ts @@ -11,7 +11,10 @@ import { DetailCheckService } from './services/detail-check-service'; import { CollectionRepository } from '@nocobase/plugin-collection-manager'; import { Repository } from '@nocobase/database'; import { VehiclesService } from './services/vehicles-service'; +import { Cache } from '@nocobase/cache'; + export class PluginRentalServer extends Plugin { + cache: Cache; async afterAdd() {} beforeLoad() {} @@ -25,6 +28,12 @@ export class PluginRentalServer extends Plugin { Container.get(ProjectService).load(); Container.get(DetailCheckService).load(); Container.get(ContractService).load(); + + this.cache = await this.app.cacheManager.createCache({ + name: '@hera/plugin-rental', + prefix: '@hera/plugin-rental', + store: process.env.CACHE_DEFAULT_STORE || 'memory', + }); } async syncCollections(collectionName: string, categoryNames: string[]) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f39b6f2b9..9b32f087b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1629,6 +1629,9 @@ importers: '@nocobase/actions': specifier: workspace:* version: link:../../../core/actions + '@nocobase/cache': + specifier: workspace:* + version: link:../../../core/cache '@nocobase/client': specifier: workspace:* version: link:../../../core/client @@ -1675,6 +1678,9 @@ importers: flatted: specifier: ^3.2.9 version: 3.2.9 + get-stream: + specifier: ^6.0.1 + version: 6.0.1 lodash: specifier: 4.17.21 version: 4.17.21