-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (105 loc) · 2.87 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module.exports = class SimpleCache {
static #cache = {};
/**
* Get cache key value
* @method get
* @static
* @param {string} key - Key of the cached value
* @param {any} default_value - Value to be returned if the key is not set yet
* @returns any
* @example
* // Get cache value without fallback value
* SimpleCache.get('key');
* @example
* // Get cache value with fallback value
* SimpleCache.get('key', 'default value');
*/
static get = function (key, default_value = undefined) {
SimpleCache.#removeExpiredValues();
return SimpleCache.#cache[key]?.value ?? default_value;
}
/**
* Set cache key value
* @method set
* @static
* @param {string} key - Key to set cache
* @param {any} value - Value to be set
* @param {(number|null)} ttl - Time in seconds to expire the cache
* @returns void
* @example
* // Populates cache
* SimpleCache.set('key', 'value');
* @example
* // Populates cache with an specific TTL
* SimpleCache.set('key', 'value', 30);
*/
static set = function (key, value, ttl = null) {
SimpleCache.#removeExpiredValues();
SimpleCache.get()
let ttlDate = null;
if (isFinite(ttl) && ttl > 0) {
ttlDate = new Date();
ttlDate.setSeconds(ttlDate.getSeconds() + parseFloat(ttl));
}
SimpleCache.#cache[key] = {
value,
ttl: ttlDate,
};
}
/**
* Unset cache key value
* @method unset
* @static
* @param {string} key - Key to set cache
* @returns void
* @example
* // Unset cache
* SimpleCache.unset('key');
*/
static unset = function (key) {
delete SimpleCache.#cache[key];
}
/**
* Verify whether cache key is setted
* @method isset
* @static
* @param {string} key - Key to verify
* @returns Boolean
* @example
* // Verify cache is set
* SimpleCache.isset('key');
*/
static isset = function (key) {
return !!SimpleCache.#cache[key]?.value;
}
/**
* Unset every cache key
* @method clear
* @static
* @param {string} key - Key to verify
* @returns void
* @example
* // Verify cache is set
* SimpleCache.isset('key');
*/
static clear = function () {
SimpleCache.#cache = {};
}
/**
* Unset expired cache keys
* @method #removeExpiredValues
* @static
* @private
* @returns void
* @example
* SimpleCache.#removeExpiredValues();
*/
static #removeExpiredValues = function () {
const now = new Date();
for (const [key, { value, ttl }] of Object.entries(SimpleCache.#cache)) {
if (ttl && ttl <= now) {
delete SimpleCache.#cache[key];
}
}
}
}