feat: record pdf cache (#823)

Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachybase#823
This commit is contained in:
sealday 2024-04-24 02:39:51 +08:00
parent 72bb3b43fe
commit 068b44793c
5 changed files with 44 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"@hera/plugin-rental": patch
---
record pdf cache

View File

@ -16,6 +16,7 @@
"exceljs": "^4.4.0", "exceljs": "^4.4.0",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"flatted": "^3.2.9", "flatted": "^3.2.9",
"get-stream": "^6.0.1",
"lodash": "4.17.21", "lodash": "4.17.21",
"qrcode": "^1.5.1", "qrcode": "^1.5.1",
"qrcode.react": "^3.1.0", "qrcode.react": "^3.1.0",
@ -26,6 +27,7 @@
"peerDependencies": { "peerDependencies": {
"@hera/plugin-core": "workspace:*", "@hera/plugin-core": "workspace:*",
"@nocobase/actions": "workspace:*", "@nocobase/actions": "workspace:*",
"@nocobase/cache": "workspace:*",
"@nocobase/client": "workspace:*", "@nocobase/client": "workspace:*",
"@nocobase/database": "workspace:*", "@nocobase/database": "workspace:*",
"@nocobase/plugin-collection-manager": "workspace:*", "@nocobase/plugin-collection-manager": "workspace:*",

View File

@ -7,6 +7,10 @@ import { Movement } from '../../utils/constants';
import _ from 'lodash'; import _ from 'lodash';
import { FilterParser, Repository } from '@nocobase/database'; import { FilterParser, Repository } from '@nocobase/database';
import { CollectionRepository } from '@nocobase/plugin-collection-manager'; import { CollectionRepository } from '@nocobase/plugin-collection-manager';
import { Cache } from '@nocobase/cache';
import { getStreamAsBuffer } from 'get-stream';
import { stringify } from 'flatted';
@Controller('records') @Controller('records')
export class RecordPreviewController { export class RecordPreviewController {
@Inject(() => SqlLoader) @Inject(() => SqlLoader)
@ -221,7 +225,24 @@ export class RecordPreviewController {
? settingType ? settingType
: pdfExplain[0]?.record_print_setup; : pdfExplain[0]?.record_print_setup;
const double = isDouble === '0' || isDouble === '1' ? isDouble : pdfExplain[0].record_columns; 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') @Action('unused')

View File

@ -11,7 +11,10 @@ import { DetailCheckService } from './services/detail-check-service';
import { CollectionRepository } from '@nocobase/plugin-collection-manager'; import { CollectionRepository } from '@nocobase/plugin-collection-manager';
import { Repository } from '@nocobase/database'; import { Repository } from '@nocobase/database';
import { VehiclesService } from './services/vehicles-service'; import { VehiclesService } from './services/vehicles-service';
import { Cache } from '@nocobase/cache';
export class PluginRentalServer extends Plugin { export class PluginRentalServer extends Plugin {
cache: Cache;
async afterAdd() {} async afterAdd() {}
beforeLoad() {} beforeLoad() {}
@ -25,6 +28,12 @@ export class PluginRentalServer extends Plugin {
Container.get(ProjectService).load(); Container.get(ProjectService).load();
Container.get(DetailCheckService).load(); Container.get(DetailCheckService).load();
Container.get(ContractService).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[]) { async syncCollections(collectionName: string, categoryNames: string[]) {

View File

@ -1629,6 +1629,9 @@ importers:
'@nocobase/actions': '@nocobase/actions':
specifier: workspace:* specifier: workspace:*
version: link:../../../core/actions version: link:../../../core/actions
'@nocobase/cache':
specifier: workspace:*
version: link:../../../core/cache
'@nocobase/client': '@nocobase/client':
specifier: workspace:* specifier: workspace:*
version: link:../../../core/client version: link:../../../core/client
@ -1675,6 +1678,9 @@ importers:
flatted: flatted:
specifier: ^3.2.9 specifier: ^3.2.9
version: 3.2.9 version: 3.2.9
get-stream:
specifier: ^6.0.1
version: 6.0.1
lodash: lodash:
specifier: 4.17.21 specifier: 4.17.21
version: 4.17.21 version: 4.17.21