@byted-apaas/server-sdk-node 1.1.26-beta.1 → 1.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -404,6 +404,8 @@ async function handleCriterion(criterion) {
404
404
  return criterion;
405
405
  }
406
406
  exports.handleCriterion = handleCriterion;
407
+ const NodeCache = require('node-cache');
408
+ const localCache = new NodeCache();
407
409
  async function handleLookupCondition(conditions) {
408
410
  if (!(conditions && conditions[0] && conditions[0].left && conditions[0].left.settings && conditions[0].left.settings.fieldPath)) {
409
411
  return null;
@@ -422,15 +424,30 @@ async function handleLookupCondition(conditions) {
422
424
  let lookupMap = new Map(), dateSet = new Set();
423
425
  let firstObjApiName = conditions[0].left.settings.fieldPath[0].objectApiName;
424
426
  let fields = [];
425
- // 获取对象的字段
426
- if (queryMap.size === 1) {
427
+ const needReadCache = server_common_node_1.utils.getLaneID() !== ''; // 仅打包发布 2.0 支持走 cache
428
+ let cacheResult = null;
429
+ if (needReadCache) { // 打包发布 2.0 读缓存
430
+ cacheResult = getLookupObjectAPINamesFromCache(firstObjApiName, queryMap);
431
+ }
432
+ // 命中缓存
433
+ if (cacheResult !== null && cacheResult.missFields.length === 0) {
434
+ fields = cacheResult.successFields;
435
+ }
436
+ // 未命中缓存 / 不读缓存
437
+ if ((cacheResult !== null && cacheResult.missFields.length === 1) || (!needReadCache && queryMap.size === 1)) {
438
+ if (cacheResult !== null && cacheResult.missFields.length === 1) {
439
+ fieldName = cacheResult.missFields[0];
440
+ }
427
441
  fields.push(await Request.GetInstance().getField(firstObjApiName, fieldName));
428
442
  }
429
- else {
443
+ if ((cacheResult !== null && cacheResult.missFields.length > 1) || (!needReadCache && queryMap.size > 1)) {
430
444
  fields = await Request.GetInstance().getFields(firstObjApiName);
431
445
  }
432
446
  for (const field of fields) {
433
447
  parseFieldInfo(field, lookupMap, dateSet);
448
+ if (needReadCache) {
449
+ setFieldCache(firstObjApiName, field.api_name, field);
450
+ }
434
451
  }
435
452
  return {
436
453
  lookupMap: lookupMap,
@@ -571,3 +588,30 @@ function dateAdd(date, num) {
571
588
  return date;
572
589
  }
573
590
  }
591
+ function setFieldCache(objectApiName, fieldName, field) {
592
+ localCache.set(getFieldCacheKey(objectApiName, fieldName), field, 60); // 缓存60s
593
+ }
594
+ function getFieldCache(objectApiName, fieldName) {
595
+ const field = localCache.get(getFieldCacheKey(objectApiName, fieldName));
596
+ return field;
597
+ }
598
+ function getFieldCacheKey(objectApiName, fieldName) {
599
+ return `${objectApiName}_${fieldName}`;
600
+ }
601
+ function getLookupObjectAPINamesFromCache(firstObjApiName, queryMap) {
602
+ var missFields = [];
603
+ var successFields = [];
604
+ for (const [fieldName] of queryMap) {
605
+ const field = getFieldCache(firstObjApiName, fieldName);
606
+ if (field) {
607
+ successFields.push(field);
608
+ }
609
+ else {
610
+ missFields.push(fieldName);
611
+ }
612
+ }
613
+ if (successFields.length === 0 && missFields.length === 0) {
614
+ return null;
615
+ }
616
+ return { successFields, missFields };
617
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byted-apaas/server-sdk-node",
3
- "version": "1.1.26-beta.1",
3
+ "version": "1.1.26",
4
4
  "description": "aPaaS Server SDK",
5
5
  "author": "zhouwexin <zhouwexin@bytedance.com>",
6
6
  "homepage": "",
@@ -13,7 +13,7 @@
13
13
  "clean": "tsc --build --clean && rm -rf **/*.js.map"
14
14
  },
15
15
  "dependencies": {
16
- "@byted-apaas/server-common-node": "^2.0.25-beta.1",
16
+ "@byted-apaas/server-common-node": "^2.0.24",
17
17
  "@jorgeferrero/stream-to-buffer": "^2.0.6",
18
18
  "dayjs": "^1.9.6",
19
19
  "form-data": "^3.0.0",
@@ -1409,6 +1409,7 @@ async function getExecutionUserTaskInfo(executionId) {
1409
1409
  let options = commonHttp.getOptions(null, constants_2.openapiHttpPath.getExecutionUserTaskInfo);
1410
1410
  let urlPath = options._reqPath.replace(replaceKeys.namespace, await (0, common_1.getNamespaceForOpenAndFaaSSDK)()).replace(replaceKeys.id, `${executionId}`);
1411
1411
  urlPath += `?operator=${utils.getUserIDFromCtx()}`;
1412
+ options.headers.User = utils.getUserIDFromCtx();
1412
1413
  let res = await openapi.doRequest(null, urlPath, options);
1413
1414
  if (!res) {
1414
1415
  throw new exceptions.InternalError('getExecutionUserTaskInfo result is empty');
@@ -1422,6 +1423,7 @@ async function executeFlow(apiName, option, async) {
1422
1423
  }
1423
1424
  let options = commonHttp.getOptions(null, constants_2.openapiHttpPath.executeFlow);
1424
1425
  const urlPath = options._reqPath.replace(replaceKeys.namespace, await (0, common_1.getNamespaceForOpenAndFaaSSDK)()).replace(replaceKeys.apiName, apiName);
1426
+ options.headers.User = utils.getUserIDFromCtx();
1425
1427
  options.json = {
1426
1428
  'operator': utils.getUserIDFromCtx(),
1427
1429
  'variables': (0, common_1.transMapToFlowVariable)(option.params),
@@ -1446,6 +1448,7 @@ async function revokeExecution(executionId, revokeOptions) {
1446
1448
  }
1447
1449
  let options = commonHttp.getOptions(null, constants_2.openapiHttpPath.revokeExecution);
1448
1450
  let urlPath = options._reqPath.replace(replaceKeys.namespace, await (0, common_1.getNamespaceForOpenAndFaaSSDK)()).replace(replaceKeys.id, `${executionId}`);
1451
+ options.headers.User = utils.getUserIDFromCtx();
1449
1452
  let reason = undefined;
1450
1453
  if (revokeOptions && revokeOptions.reason) {
1451
1454
  reason = {
@@ -1467,6 +1470,7 @@ async function getExecutionInfo(executionId) {
1467
1470
  let options = commonHttp.getOptions(null, constants_2.openapiHttpPath.getExecutionInfo);
1468
1471
  let urlPath = options._reqPath.replace(replaceKeys.namespace, await (0, common_1.getNamespaceForOpenAndFaaSSDK)()).replace(replaceKeys.id, `${executionId}`);
1469
1472
  urlPath += `?operator=${utils.getUserIDFromCtx()}`;
1473
+ options.headers.User = utils.getUserIDFromCtx();
1470
1474
  let res = await openapi.doRequest(null, urlPath, options);
1471
1475
  if (!res || !res.executionInfo) {
1472
1476
  throw new exceptions.InternalError(`getExecutionInfo result is empty`);