Commit 48edb4d5899c4dbe2ebd26b0e6da9948251b2ef1
1 parent
4a1706c99f
Exists in
master
Prefix TTL_SECS as a constant
Showing 1 changed file with 3 additions and 1 deletions Inline Diff
lib/prefixes/index.js
1 | 'use strict'; | 1 | 'use strict'; |
2 | 2 | ||
3 | const TTL_SECS = 30; | ||
4 | |||
3 | const coreapi = require('komodo-sdk/coreapi'); | 5 | const coreapi = require('komodo-sdk/coreapi'); |
4 | const NodeCache = require( "node-cache" ); | 6 | const NodeCache = require( "node-cache" ); |
5 | 7 | ||
6 | const cache = new NodeCache({ | 8 | const cache = new NodeCache({ |
7 | stdTTL: 60, | 9 | stdTTL: TTL_SECS, |
8 | }); | 10 | }); |
9 | 11 | ||
10 | async function lookup(number) { | 12 | async function lookup(number) { |
11 | if (!number) return; | 13 | if (!number) return; |
12 | 14 | ||
13 | if (cache.get(number)) { | 15 | if (cache.get(number)) { |
14 | return cache.get(number); | 16 | return cache.get(number); |
15 | } | 17 | } |
16 | 18 | ||
17 | const [err, lookupResult] = await coreapi({ | 19 | const [err, lookupResult] = await coreapi({ |
18 | path: '/prefixes/lookup', | 20 | path: '/prefixes/lookup', |
19 | method: 'GET', | 21 | method: 'GET', |
20 | qs: { | 22 | qs: { |
21 | number, | 23 | number, |
22 | } | 24 | } |
23 | }); | 25 | }); |
24 | 26 | ||
25 | if (err || !lookupResult || lookupResult.error) { | 27 | if (err || !lookupResult || lookupResult.error) { |
26 | cache.del(number); | 28 | cache.del(number); |
27 | return; | 29 | return; |
28 | } | 30 | } |
29 | 31 | ||
30 | if (lookupResult.prefix) { | 32 | if (lookupResult.prefix) { |
31 | cache.set(number, lookupResult.prefix); | 33 | cache.set(number, lookupResult.prefix); |
32 | } else { | 34 | } else { |
33 | cache.del(number); | 35 | cache.del(number); |
34 | } | 36 | } |
35 | 37 | ||
36 | return lookupResult.prefix; | 38 | return lookupResult.prefix; |
37 | } | 39 | } |
38 | 40 | ||
39 | exports.lookup = lookup; | 41 | exports.lookup = lookup; |
40 | 42 |