Use LRU cache as default mechanism for caching in memory (#3979)

* Use LRU cache as default mechanism for caching in memory

* Return null as what’s expected
This commit is contained in:
Florent Vilmart
2017-06-30 16:54:35 -04:00
committed by GitHub
parent 29c2fad671
commit 95430bb4e8
4 changed files with 40 additions and 3 deletions

View File

@@ -1,9 +1,9 @@
import {InMemoryCache} from './InMemoryCache';
import {LRUCache} from './LRUCache';
export class InMemoryCacheAdapter {
constructor(ctx) {
this.cache = new InMemoryCache(ctx)
this.cache = new LRUCache(ctx)
}
get(key) {

View File

@@ -0,0 +1,33 @@
import LRU from 'lru-cache';
import defaults from '../../defaults';
export class LRUCache {
constructor({
ttl = defaults.cacheTTL,
maxSize = defaults.cacheMaxSize,
}) {
this.cache = new LRU({
max: maxSize,
maxAge: ttl
});
}
get(key) {
return this.cache.get(key) || null;
}
put(key, value, ttl = this.ttl) {
this.cache.set(key, value, ttl);
}
del(key) {
this.cache.del(key);
}
clear() {
this.cache.reset();
}
}
export default LRUCache;