* Adding Caching Adapter, allows caching of _Role and _User queries.
This commit is contained in:
27
src/Adapters/Cache/CacheAdapter.js
Normal file
27
src/Adapters/Cache/CacheAdapter.js
Normal file
@@ -0,0 +1,27 @@
|
||||
export class CacheAdapter {
|
||||
/**
|
||||
* Get a value in the cache
|
||||
* @param key Cache key to get
|
||||
* @return Promise that will eventually resolve to the value in the cache.
|
||||
*/
|
||||
get(key) {}
|
||||
|
||||
/**
|
||||
* Set a value in the cache
|
||||
* @param key Cache key to set
|
||||
* @param value Value to set the key
|
||||
* @param ttl Optional TTL
|
||||
*/
|
||||
put(key, value, ttl) {}
|
||||
|
||||
/**
|
||||
* Remove a value from the cache.
|
||||
* @param key Cache key to remove
|
||||
*/
|
||||
del(key) {}
|
||||
|
||||
/**
|
||||
* Empty a cache
|
||||
*/
|
||||
clear() {}
|
||||
}
|
||||
66
src/Adapters/Cache/InMemoryCache.js
Normal file
66
src/Adapters/Cache/InMemoryCache.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const DEFAULT_CACHE_TTL = 5 * 1000;
|
||||
|
||||
|
||||
export class InMemoryCache {
|
||||
constructor({
|
||||
ttl = DEFAULT_CACHE_TTL
|
||||
}) {
|
||||
this.ttl = ttl;
|
||||
this.cache = Object.create(null);
|
||||
}
|
||||
|
||||
get(key) {
|
||||
let record = this.cache[key];
|
||||
if (record == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Has Record and isnt expired
|
||||
if (isNaN(record.expire) || record.expire >= Date.now()) {
|
||||
return record.value;
|
||||
}
|
||||
|
||||
// Record has expired
|
||||
delete this.cache[key];
|
||||
return null;
|
||||
}
|
||||
|
||||
put(key, value, ttl = this.ttl) {
|
||||
if (ttl < 0 || isNaN(ttl)) {
|
||||
ttl = NaN;
|
||||
}
|
||||
|
||||
var record = {
|
||||
value: value,
|
||||
expire: ttl + Date.now()
|
||||
}
|
||||
|
||||
if (!isNaN(record.expire)) {
|
||||
record.timeout = setTimeout(() => {
|
||||
this.del(key);
|
||||
}, ttl);
|
||||
}
|
||||
|
||||
this.cache[key] = record;
|
||||
}
|
||||
|
||||
del(key) {
|
||||
var record = this.cache[key];
|
||||
if (record == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (record.timeout) {
|
||||
clearTimeout(record.timeout);
|
||||
}
|
||||
|
||||
delete this.cache[key];
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.cache = Object.create(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default InMemoryCache;
|
||||
36
src/Adapters/Cache/InMemoryCacheAdapter.js
Normal file
36
src/Adapters/Cache/InMemoryCacheAdapter.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import {InMemoryCache} from './InMemoryCache';
|
||||
|
||||
export class InMemoryCacheAdapter {
|
||||
|
||||
constructor(ctx) {
|
||||
this.cache = new InMemoryCache(ctx)
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let record = this.cache.get(key);
|
||||
if (record == null) {
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
return resolve(JSON.parse(record));
|
||||
})
|
||||
}
|
||||
|
||||
put(key, value, ttl) {
|
||||
this.cache.put(key, JSON.stringify(value), ttl);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
del(key) {
|
||||
this.cache.del(key);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.cache.clear();
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
export default InMemoryCacheAdapter;
|
||||
Reference in New Issue
Block a user