index.js
2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var LRU_1 = require("./utils/LRU");
var CACHE_SIZE = 1000;
/**
* Inspired node-lru-cache[https://github.com/isaacs/node-lru-cache]
*/
var EndpointCache = /** @class */ (function () {
function EndpointCache(maxSize) {
if (maxSize === void 0) { maxSize = CACHE_SIZE; }
this.maxSize = maxSize;
this.cache = new LRU_1.LRUCache(maxSize);
}
;
Object.defineProperty(EndpointCache.prototype, "size", {
get: function () {
return this.cache.length;
},
enumerable: true,
configurable: true
});
EndpointCache.prototype.put = function (key, value) {
var keyString = typeof key !== 'string' ? EndpointCache.getKeyString(key) : key;
var endpointRecord = this.populateValue(value);
this.cache.put(keyString, endpointRecord);
};
EndpointCache.prototype.get = function (key) {
var keyString = typeof key !== 'string' ? EndpointCache.getKeyString(key) : key;
var now = Date.now();
var records = this.cache.get(keyString);
if (records) {
for (var i = 0; i < records.length; i++) {
var record = records[i];
if (record.Expire < now) {
this.cache.remove(keyString);
return undefined;
}
}
}
return records;
};
EndpointCache.getKeyString = function (key) {
var identifiers = [];
var identifierNames = Object.keys(key).sort();
for (var i = 0; i < identifierNames.length; i++) {
var identifierName = identifierNames[i];
if (key[identifierName] === undefined)
continue;
identifiers.push(key[identifierName]);
}
return identifiers.join(' ');
};
EndpointCache.prototype.populateValue = function (endpoints) {
var now = Date.now();
return endpoints.map(function (endpoint) { return ({
Address: endpoint.Address || '',
Expire: now + (endpoint.CachePeriodInMinutes || 1) * 60 * 1000
}); });
};
EndpointCache.prototype.empty = function () {
this.cache.empty();
};
EndpointCache.prototype.remove = function (key) {
var keyString = typeof key !== 'string' ? EndpointCache.getKeyString(key) : key;
this.cache.remove(keyString);
};
return EndpointCache;
}());
exports.EndpointCache = EndpointCache;