Files
kami-parse-server/src/Adapters/Cache/RedisCacheAdapter.js
Florent Vilmart ddb0fb8a27 Adds redis cache for distributed environments (#2691)
* Makes schemaCache clearning promise-based

* Adds redis cache adapter for distributed systems

* Adds redis service to travis

* allow pg to fail
2016-09-17 13:52:02 -07:00

70 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import redis from 'redis';
import logger from '../../logger';
function debug() {
logger.debug.apply(logger, ['RedisCacheAdapter', ...arguments]);
}
export class RedisCacheAdapter {
constructor(ctx) {
this.client = redis.createClient(ctx);
this.p = Promise.resolve();
}
get(key) {
debug('get', key);
this.p = this.p.then(() => {
return new Promise((resolve, _) => {
this.client.get(key, function(err, res) {
debug('-> get', key, res);
if(!res) {
return resolve(null);
}
resolve(JSON.parse(res));
});
});
});
return this.p;
}
put(key, value, ttl) {
value = JSON.stringify(value);
debug('put', key, value, ttl);
this.p = this.p.then(() => {
return new Promise((resolve, _) => {
this.client.set(key, value, function(err, res) {
resolve();
});
});
});
return this.p;
}
del(key) {
debug('del', key);
this.p = this.p.then(() => {
return new Promise((resolve, _) => {
this.client.del(key, function(err, res) {
resolve();
});
});
});
return this.p;
}
clear() {
debug('clear');
this.p = this.p.then(() => {
return new Promise((resolve, _) => {
this.client.flushall(function(err, res) {
resolve();
});
});
});
return this.p;
}
}
export default RedisCacheAdapter;