-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriceCache.js
More file actions
40 lines (34 loc) · 811 Bytes
/
priceCache.js
File metadata and controls
40 lines (34 loc) · 811 Bytes
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
class PriceCache {
constructor() {
this.cache = new Map();
}
set(currencyPair, data) {
const normalizedKey = currencyPair.toUpperCase();
this.cache.set(normalizedKey, {
...data,
timestamp: Date.now()
});
}
get(currencyPair) {
const normalizedKey = currencyPair.toUpperCase();
const entry = this.cache.get(normalizedKey);
if (!entry) {
return null;
}
const age = Date.now() - entry.timestamp;
return {
data: {
amount: entry.amount,
base: entry.base,
currency: entry.currency
},
age,
isStale: age > 60000 // 60 seconds
};
}
has(currencyPair) {
const normalizedKey = currencyPair.toUpperCase();
return this.cache.has(normalizedKey);
}
}
export default new PriceCache();