-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
js-storage.js
71 lines (66 loc) · 1.52 KB
/
js-storage.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
/**
* @locus Client
* @class JSStorage
* @summary JavaScript Object-driven storage
*/
function JSStorage(clientStorage) {
if (clientStorage) {
this.data = clientStorage.data;
this.ttlData = clientStorage.ttlData;
} else {
this.data = {};
this.ttlData = {};
}
}
/**
* @locus Client
* @memberOf JSStorage
* @name set
* @param {String} key - The key to create/overwrite
* @param {String} value - The value
* @param {Number} ttl - TTL (e.g. max-age) in seconds
* @summary Create/overwrite a record.
* @returns {Boolean}
*/
JSStorage.prototype.set = function (key, value, ttl) {
if (typeof key === 'string') {
this.data[key] = value;
if (typeof ttl === 'number') {
this.ttlData[key] = Date.now() + (ttl * 1000);
}
return true;
}
return false;
};
/**
* @locus Client
* @memberOf JSStorage
* @name remove
* @param {String} key - The key of the record to remove
* @summary Remove record(s).
* @returns {Boolean}
*/
JSStorage.prototype.remove = function (key) {
if (typeof key === 'string' && this.data.hasOwnProperty(key)) {
delete this.data[key];
delete this.ttlData[key];
return true;
}
if (key === void 0 && Object.keys(this.data).length) {
this.data = {};
this.ttlData = {};
return true;
}
return false;
};
/**
* @locus Client
* @memberOf JSStorage
* @name isSupported
* @summary Returns `true` is this storage driver is supported
* @returns {Boolean}
*/
JSStorage.isSupported = function () {
return true;
};
module.exports = JSStorage;