Commit 4a1706c99fff59b329df83cb2f6ad86c0377e54e

Authored by Adhidarma Hadiwinoto
1 parent 8295b94e74
Exists in master

Prefixes cached

Showing 4 changed files with 41 additions and 0 deletions Side-by-side Diff

lib/prefixes/index.js
1 1 'use strict';
2 2  
3 3 const coreapi = require('komodo-sdk/coreapi');
  4 +const NodeCache = require( "node-cache" );
  5 +
  6 +const cache = new NodeCache({
  7 + stdTTL: 60,
  8 +});
4 9  
5 10 async function lookup(number) {
6 11 if (!number) return;
7 12  
  13 + if (cache.get(number)) {
  14 + return cache.get(number);
  15 + }
  16 +
8 17 const [err, lookupResult] = await coreapi({
9 18 path: '/prefixes/lookup',
10 19 method: 'GET',
... ... @@ -14,9 +23,16 @@ async function lookup(number) {
14 23 });
15 24  
16 25 if (err || !lookupResult || lookupResult.error) {
  26 + cache.del(number);
17 27 return;
18 28 }
19 29  
  30 + if (lookupResult.prefix) {
  31 + cache.set(number, lookupResult.prefix);
  32 + } else {
  33 + cache.del(number);
  34 + }
  35 +
20 36 return lookupResult.prefix;
21 37 }
22 38  
... ... @@ -524,6 +524,11 @@
524 524 "wrap-ansi": "^2.0.0"
525 525 }
526 526 },
  527 + "clone": {
  528 + "version": "2.1.2",
  529 + "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
  530 + "integrity": "sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18="
  531 + },
527 532 "code-point-at": {
528 533 "version": "1.1.0",
529 534 "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz",
... ... @@ -3055,6 +3060,15 @@
3055 3060 "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
3056 3061 "dev": true
3057 3062 },
  3063 + "node-cache": {
  3064 + "version": "4.2.1",
  3065 + "resolved": "https://registry.npmjs.org/node-cache/-/node-cache-4.2.1.tgz",
  3066 + "integrity": "sha512-BOb67bWg2dTyax5kdef5WfU3X8xu4wPg+zHzkvls0Q/QpYycIFRLEEIdAx9Wma43DxG6Qzn4illdZoYseKWa4A==",
  3067 + "requires": {
  3068 + "clone": "2.x",
  3069 + "lodash": "^4.17.15"
  3070 + }
  3071 + },
3058 3072 "node-environment-flags": {
3059 3073 "version": "1.0.5",
3060 3074 "resolved": "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.5.tgz",
... ... @@ -24,6 +24,7 @@
24 24 "komodo-center-messaging-client-lib": "git+http://gitlab.kodesumber.com/komodo/komodo-center-messaging-client-lib.git",
25 25 "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git",
26 26 "moment": "^2.24.0",
  27 + "node-cache": "^4.2.1",
27 28 "redis": "^2.8.0",
28 29 "request": "^2.88.0",
29 30 "uuid": "^3.3.2"
... ... @@ -19,5 +19,15 @@ describe('#prefixes', () => {
19 19 should.not.exist(await prefixes.lookup(''));
20 20 should.not.exist(await prefixes.lookup('9'));
21 21 });
  22 +
  23 + it('should handle cache result', async () => {
  24 + const lookupResult = await prefixes.lookup('08180000000');
  25 + await prefixes.lookup('08180000000');
  26 + await prefixes.lookup('08180000000');
  27 + await prefixes.lookup('08180000000');
  28 + await prefixes.lookup('08180000000');
  29 + await prefixes.lookup('08180000000');
  30 + lookupResult.should.equal('XL');
  31 + });
22 32 });
23 33 });
24 34 \ No newline at end of file