forked from UnblockNeteaseMusic/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
198 lines (169 loc) · 4.19 KB
/
cache.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const { EventEmitter } = require('events');
const { logScope } = require('./logger');
const logger = logScope('cache');
const CacheStorageEvents = {
CLEANUP: 'cs@cleanup',
};
/**
* @typedef {{data: any, expireAt: Date}} CacheData
*/
/**
* A cache storage for storing any type of data.
*/
class CacheStorage extends EventEmitter {
/**
* @type {string}
*/
id = 'Default Cache Storage';
/**
* @type {Map<any, CacheData>}
* @readonly
*/
cacheMap = new Map();
aliveDuration = 30 * 60 * 1000; // will expire after 30 minutes.
/**
* Construct a cache storage.
*
* @param {string?} id The ID of this cache storage.
*/
constructor(id) {
super();
// Set the ID of this cache storage.
if (id) this.id = id;
// Register the CLEANUP event. It will clean up
// the expired cache when emitting "CLEANUP" event.
this.on(CacheStorageEvents.CLEANUP, async () =>
this.removeExpiredCache()
);
}
/**
* Get the absolute UNIX timestamp the cache will be ended.
* @return {number}
* @constructor
*/
get WillExpireAt() {
return Date.now() + this.aliveDuration;
}
/**
* Get the context for logger().
*
* @param {Record<string, string>?} customContext The additional context.
* @return {Record<string, string>}
* @private
*/
getLoggerContext(customContext = {}) {
return {
...customContext,
cacheStorageId: this.id,
};
}
/**
* Remove the expired cache.
*/
removeExpiredCache() {
logger.debug(
this.getLoggerContext(),
'Cleaning up the expired caches...'
);
this.cacheMap.forEach((cachedData, key) => {
if (cachedData.expireAt <= Date.now()) this.cacheMap.delete(key);
});
}
/**
* Cache the response.
*
* @template T
* @param {any} key the unique key of action to be cached.
* @param {() => Promise<T>} action the action to do and be cached.
* @param {number=} expireAt customize the expireAt of this key.
* @return {Promise<T>}
*/
async cache(key, action, expireAt) {
// Disable the cache when the NO_CACHE = true.
if (process.env.NO_CACHE === 'true') {
return action();
}
// Push the CLEANUP task to the event loop - "polling",
// so that it won't block the cache() task.
this.emit(CacheStorageEvents.CLEANUP);
// Check if we have cached it before.
// If true, we return the cached value.
const cachedData = this.cacheMap.get(key);
// Object.toString() can't bring any useful information,
// we show "Something" instead.
const logKey = typeof key === 'object' ? 'Something' : key;
// Get the logger context with getLoggerContext
const logCtx = this.getLoggerContext({
logKey,
});
if (cachedData) {
logger.debug(logCtx, `${logKey} hit!`);
return cachedData.data;
}
// Cache the response of action() and
// register into our cache map.
logger.debug(
logCtx,
`${logKey} did not hit. Storing the execution result...`
);
const sourceResponse = await action();
this.cacheMap.set(key, {
data: sourceResponse,
expireAt: new Date(expireAt || this.WillExpireAt),
});
return sourceResponse;
}
}
/**
* The group which aimed to manage all CacheStorage and
* call the common method such as `removeExpiredCache()`.
*/
class CacheStorageGroup {
/**
* @type {CacheStorageGroup | undefined}
*/
static instance = undefined;
/** @type {Set<CacheStorage>} */
cacheStorages = new Set();
/** @private */
constructor() {}
/**
* @return {CacheStorageGroup}
*/
static getInstance() {
if (!CacheStorageGroup.instance)
CacheStorageGroup.instance = new CacheStorageGroup();
return CacheStorageGroup.instance;
}
cleanup() {
this.cacheStorages.forEach((storage) => storage.removeExpiredCache());
}
}
/**
* The CacheStorageGroup instance that is used internally.
*
* Don't export it!
*
* @type {CacheStorageGroup}
*/
const csgInstance = CacheStorageGroup.getInstance();
/**
* Get the managed CacheStorage.
*
* “Managed” means that this CacheStorage has been
* added to CacheStorageGroup.
*
* @param {string} id
* @return {CacheStorage}
*/
function getManagedCacheStorage(id) {
const cs = new CacheStorage(id);
csgInstance.cacheStorages.add(cs);
return cs;
}
module.exports = {
CacheStorage,
CacheStorageEvents,
CacheStorageGroup,
getManagedCacheStorage,
};