feat: InMemoryCache implementation
This commit is contained in:
committed by
mrugesh mohapatra
parent
5dc8320dc8
commit
80c3c3ec55
34
api-server/server/utils/in-memory-cache.js
Normal file
34
api-server/server/utils/in-memory-cache.js
Normal file
@ -0,0 +1,34 @@
|
||||
function isPromiseLike(thing) {
|
||||
return !!thing && typeof thing.then === 'function';
|
||||
}
|
||||
|
||||
function InMemoryCache(initialValue) {
|
||||
const cacheKey = Symbol('cacheKey');
|
||||
const cache = new Map();
|
||||
|
||||
if (typeof initialValue !== 'undefined') {
|
||||
cache.set(cacheKey, initialValue);
|
||||
}
|
||||
|
||||
return {
|
||||
get() {
|
||||
const value = cache.get(cacheKey);
|
||||
return typeof value !== 'undefined' ? value : null;
|
||||
},
|
||||
async update(fn) {
|
||||
const maybePromisedValue = fn();
|
||||
if (isPromiseLike(maybePromisedValue)) {
|
||||
return maybePromisedValue.then(value => cache.set(cacheKey, value));
|
||||
} else {
|
||||
const value = maybePromisedValue;
|
||||
cache.set(cacheKey, value);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
clear() {
|
||||
return cache.delete(cacheKey);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default InMemoryCache;
|
Reference in New Issue
Block a user