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

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(`=====>[handleLookupCondition] getLookupObjectAPINamesFromCache: ${JSON.stringify(cacheResult)}`);
433
+ }
434
+ // 命中缓存
435
+ if (cacheResult !== null && cacheResult.missFields.length === 0) {
436
+ fields = cacheResult.successFields;
437
+ }
438
+ // 未命中缓存 / 不读缓存
439
+ if ((cacheResult !== null && cacheResult.missFields.length === 1) || 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) || 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.25-beta.0",
3
+ "version": "1.1.26-beta.0",
4
4
  "description": "aPaaS Server SDK",
5
5
  "author": "zhouwexin <zhouwexin@bytedance.com>",
6
6
  "homepage": "",