-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.js
More file actions
85 lines (76 loc) · 2.2 KB
/
memory.js
File metadata and controls
85 lines (76 loc) · 2.2 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const Cache = require('./cache');
class MemoryCache extends Cache {
constructor(config, name) {
super({ ttl: true }, config, name);
this._types = {};
}
/**
* Stores the payload in cache.
* @param {string} type The name of the type to cache for
* @param {string|number} id The id of the document to store
* @param {*} payload The data to cache
* @returns {Promise.<Payload>}
* @private
*/
async _store(type, id, payload) {
this._types[type] = this._types[type] || {};
this._types[type][id] = payload;
return payload;
}
/**
* Fetches payload from cache.
* @param {string} type
* @param {string|number} id
* @returns {Promise.<Payload>}
* @private
*/
async _fetch(type, id) {
return this._types[type] && this._types[type][id];
}
/**
* Returns all cached messages and listeners
* @param {string} type The id/name of the type for which to fetch data
* @returns {Promise.<Payload[]>}
* @private
*/
async _list(type) {
let list = [];
for (let id in this._types[type]) {
list.push(this._types[type][id]);
}
return list;
}
/**
* Returns a map of all stored entries for a type
* @param {string} type
* @returns {Promise.<Object<String, Payload>>}
* @private
*/
async _map(type) {
return this._types[type];
}
/**
* Removes all cached data from a type.
* @param {string} type The id/name of the type to clear
* @returns {Promise.<Object<String, Payload>>}
* @private
*/
async _clear(type) {
let entries = this._types[type];
delete this._types[type];
return entries;
}
/**
* Remove an entry from cache.
* @param {string} type The id/name of the type from which to remove the message
* @param {string} id The id of the message to remove
* @returns {Promise.<Payload>}
* @private
*/
async _remove(type, id) {
let payload = this._types[type][id];
delete this._types[type][id];
return payload;
}
}
module.exports = MemoryCache;