Skip to content

Instantly share code, notes, and snippets.

@dsternlicht
Created September 17, 2017 07:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dsternlicht/248d0a3e8e0a1950edbcb04734447397 to your computer and use it in GitHub Desktop.
Save dsternlicht/248d0a3e8e0a1950edbcb04734447397 to your computer and use it in GitHub Desktop.
Cache service wrapper for node-cache module
import NodeCache from 'node-cache';
class Cache {
constructor(ttlSeconds) {
this.cache = new NodeCache({ stdTTL: ttlSeconds, checkperiod: ttlSeconds * 0.2, useClones: false });
}
get(key, storeFunction) {
const value = this.cache.get(key);
if (value) {
return Promise.resolve(value);
}
return storeFunction().then((result) => {
this.cache.set(key, result);
return result;
});
}
del(keys) {
this.cache.del(keys);
}
delStartWith(startStr = '') {
if (!startStr) {
return;
}
const keys = this.cache.keys();
for (const key of keys) {
if (key.indexOf(startStr) === 0) {
this.del(key);
}
}
}
flush() {
this.cache.flushAll();
}
}
export default Cache;
@EgoPingvina
Copy link

image
But where is the set method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment