@@ -1,10 +1,7 @@
|
||||
const DEFAULT_CACHE_TTL = 5 * 1000;
|
||||
|
||||
|
||||
export class InMemoryCache {
|
||||
constructor({
|
||||
ttl = DEFAULT_CACHE_TTL
|
||||
}) {
|
||||
constructor({ ttl = DEFAULT_CACHE_TTL }) {
|
||||
this.ttl = ttl;
|
||||
this.cache = Object.create(null);
|
||||
}
|
||||
@@ -32,8 +29,8 @@ export class InMemoryCache {
|
||||
|
||||
var record = {
|
||||
value: value,
|
||||
expire: ttl + Date.now()
|
||||
}
|
||||
expire: ttl + Date.now(),
|
||||
};
|
||||
|
||||
if (!isNaN(record.expire)) {
|
||||
record.timeout = setTimeout(() => {
|
||||
@@ -59,7 +56,6 @@ export class InMemoryCache {
|
||||
clear() {
|
||||
this.cache = Object.create(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default InMemoryCache;
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import {LRUCache} from './LRUCache';
|
||||
import { LRUCache } from './LRUCache';
|
||||
|
||||
export class InMemoryCacheAdapter {
|
||||
|
||||
constructor(ctx) {
|
||||
this.cache = new LRUCache(ctx)
|
||||
this.cache = new LRUCache(ctx);
|
||||
}
|
||||
|
||||
get(key) {
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
import LRU from 'lru-cache';
|
||||
import defaults from '../../defaults';
|
||||
import defaults from '../../defaults';
|
||||
|
||||
export class LRUCache {
|
||||
constructor({
|
||||
ttl = defaults.cacheTTL,
|
||||
maxSize = defaults.cacheMaxSize,
|
||||
}) {
|
||||
constructor({ ttl = defaults.cacheTTL, maxSize = defaults.cacheMaxSize }) {
|
||||
this.cache = new LRU({
|
||||
max: maxSize,
|
||||
maxAge: ttl
|
||||
maxAge: ttl,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -27,7 +24,6 @@ export class LRUCache {
|
||||
clear() {
|
||||
this.cache.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LRUCache;
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
export class NullCacheAdapter {
|
||||
|
||||
constructor() {}
|
||||
|
||||
get() {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise(resolve => {
|
||||
return resolve(null);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
put() {
|
||||
|
||||
@@ -8,7 +8,6 @@ function debug() {
|
||||
}
|
||||
|
||||
export class RedisCacheAdapter {
|
||||
|
||||
constructor(redisCtx, ttl = DEFAULT_REDIS_TTL) {
|
||||
this.client = redis.createClient(redisCtx);
|
||||
this.p = Promise.resolve();
|
||||
@@ -18,10 +17,10 @@ export class RedisCacheAdapter {
|
||||
get(key) {
|
||||
debug('get', key);
|
||||
this.p = this.p.then(() => {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise(resolve => {
|
||||
this.client.get(key, function(err, res) {
|
||||
debug('-> get', key, res);
|
||||
if(!res) {
|
||||
if (!res) {
|
||||
return resolve(null);
|
||||
}
|
||||
resolve(JSON.parse(res));
|
||||
@@ -41,7 +40,7 @@ export class RedisCacheAdapter {
|
||||
ttl = DEFAULT_REDIS_TTL;
|
||||
}
|
||||
this.p = this.p.then(() => {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise(resolve => {
|
||||
if (ttl === Infinity) {
|
||||
this.client.set(key, value, function() {
|
||||
resolve();
|
||||
@@ -59,7 +58,7 @@ export class RedisCacheAdapter {
|
||||
del(key) {
|
||||
debug('del', key);
|
||||
this.p = this.p.then(() => {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise(resolve => {
|
||||
this.client.del(key, function() {
|
||||
resolve();
|
||||
});
|
||||
@@ -71,7 +70,7 @@ export class RedisCacheAdapter {
|
||||
clear() {
|
||||
debug('clear');
|
||||
this.p = this.p.then(() => {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise(resolve => {
|
||||
this.client.flushdb(function() {
|
||||
resolve();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user