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

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,32 @@ 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
+ let 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
+ // todo wby 方便 qa 测试的日志
432
+ console.log(`\n=====>[handleLookupCondition] getLookupObjectAPINamesFromCache: ${JSON.stringify(cacheResult)}\n`);
433
+ }
434
+ // 命中缓存
435
+ if (cacheResult !== null && cacheResult.missFields.length === 0) {
436
+ fields = cacheResult.successFields;
437
+ }
438
+ // 未命中缓存 / 不读缓存
439
+ if ((cacheResult !== null && cacheResult.missFields.length === 1) || (!needReadCache && queryMap.size === 1)) {
440
+ if (cacheResult !== null && cacheResult.missFields.length === 1) {
441
+ fieldName = cacheResult.missFields[0];
442
+ }
427
443
  fields.push(await Request.GetInstance().getField(firstObjApiName, fieldName));
428
444
  }
429
- else {
445
+ if ((cacheResult !== null && cacheResult.missFields.length > 1) || (!needReadCache && queryMap.size > 1)) {
430
446
  fields = await Request.GetInstance().getFields(firstObjApiName);
431
447
  }
432
448
  for (const field of fields) {
433
449
  parseFieldInfo(field, lookupMap, dateSet);
450
+ if (needReadCache) {
451
+ setFieldCache(firstObjApiName, field.api_name, field);
452
+ }
434
453
  }
435
454
  return {
436
455
  lookupMap: lookupMap,
@@ -571,3 +590,30 @@ function dateAdd(date, num) {
571
590
  return date;
572
591
  }
573
592
  }
593
+ function setFieldCache(objectApiName, fieldName, field) {
594
+ localCache.set(getFieldCacheKey(objectApiName, fieldName), field, 60); // 缓存60s
595
+ }
596
+ function getFieldCache(objectApiName, fieldName) {
597
+ let field = localCache.get(getFieldCacheKey(objectApiName, fieldName));
598
+ return field;
599
+ }
600
+ function getFieldCacheKey(objectApiName, fieldName) {
601
+ return `${objectApiName}_${fieldName}`;
602
+ }
603
+ function getLookupObjectAPINamesFromCache(firstObjApiName, queryMap) {
604
+ var missFields = [];
605
+ var successFields = [];
606
+ for (const [fieldName, _] of queryMap) {
607
+ let field = getFieldCache(firstObjApiName, fieldName);
608
+ if (field) {
609
+ successFields.push(field);
610
+ }
611
+ else {
612
+ missFields.push(fieldName);
613
+ }
614
+ }
615
+ if (successFields.length === 0 && missFields.length === 0) {
616
+ return null;
617
+ }
618
+ return { successFields, missFields };
619
+ }
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-beta.2",
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`);