-
Notifications
You must be signed in to change notification settings - Fork 6
/
memory.js
68 lines (61 loc) · 1.22 KB
/
memory.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
'use strict';
/**
* Stores data.
*
* @type {Object}
* @api public
*/
var data = Object.create(null);
//
// Expose a `store` like interface.
//
module.exports = {
/**
* Get data.
*
* @param {String} key Key.
* @param {Function} fn Completion callback.
* @api public
*/
get: function get(key, fn) {
setTimeout(function tick() {
fn(undefined, data[key]);
}, 0);
},
/**
* Store data.
*
* @param {String} key Key.
* @param {Mixed} value Things to store.
* @param {Number} expire Expire time in ms.
* @param {Function} fn Completion callback.
* @api public
*/
set: function set(key, value, expire, fn) {
data[key] = value;
setTimeout(fn, 0);
setTimeout(function tick() {
delete data[key];
}, expire * 1000);
},
/**
* Remove data from store.
*
* @param {String} key Key.
* @param {Function} fn Completion callback
* @api public
*/
del: function del(key, fn) {
delete data[key];
setTimeout(fn, 0);
},
/**
* Reset the data layer, this is method is only used for testing not part of
* the actual store interface requirement.
*
* @api public
*/
reset: function reset() {
data = Object.create(null);
}
};