UNPKG

jsperf-img-svc

Version:

A simple micro service for loading jsperf result charts as a static image

42 lines (35 loc) 921 B
'use strict'; const _ = require('lodash'), redis = require('redis'), Promise = require('bluebird'), config = require('./config'); Promise.promisifyAll(redis.RedisClient.prototype); exports.getClient = function(){ return redis.createClient(config.redisURL); }; exports.read = function(client, key){ return client.hgetAsync(key, 'data'); }; exports.write = function(client, key, data, ttl){ return client.hmsetAsync(key, { expires: Date.now() + (ttl || 0), data: data }); }; exports.expired = function(client, key){ return client.hgetAsync(key, 'expires') .then((expires) => { let result; if (_.parseInt(expires) >= Date.now()) { result = client.hgetAsync(key, 'data'); } else { if (expires) { logger.verbose(`${key} expired`); client.delAsync(key); } result = Promise.reject(_.assign(new Error('Not cached.'), {code: 'ENOTCACHED'})); } return result; }); };