UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

162 lines (144 loc) 4.16 kB
var logger = require('../../lib/logger'); var config = require("../../lib/config"); var redis = require('redis'); var async = require('async'); var client = null; var _prefix = 'cms_'; var _host = '127.0.0.1'; var _port = 6379; var _options = {}; module.exports.setOptions = function(prefix, port, host, options) { _prefix = prefix || 'cms_'; _host = host || '127.0.0.1'; _port = port || 6379; _options = options || {}; }; module.exports.init = function(server, done) { client = redis.createClient(_port, _host, _options); client.on("error", function (err) { logger.error(err); client.end(); if (done) done(err); }); client.on("ready", function(err) { if (err) { logger.error(err); if (done) done(err); } else { logger.info("Redis client connected.", true); if (done) done(); } }); //NOTE: redis module appears to close connections automatically when node exists /*process.on('close', function () { logger.warn('Redis connection disconnected through app termination.', true); redis.quit(); });*/ // If the Node process ends, close the Redis connection /*process.on('SIGINT', function() { if (client) { logger.warn('Redis connection disconnected through app termination.'); client.end(); } });*/ }; /* callback: (err) */ module.exports.set = function(key, obj, seconds, callback) { try { //console.log("Adding to cache: " + key); var s = (typeof obj === 'string') ? obj : "json:" + JSON.stringify(obj); client.setex(_prefix + key, seconds, s, function(err, result) { if (err) { logger.error(err); if (callback) callback(err); } else { if (callback) callback(null); } }); } catch(e) { logger.error(e); if (callback) callback(e); } }; /* callback: (err, value) */ module.exports.get = function(key, callback) { if (callback) { try { client.get(_prefix + key, function (err, result) { if (err) { logger.error(err); callback(err); } else if (result) { //console.log("Found in cache: " + key); if (result.length > 5 && result.indexOf("json:") === 0) { callback(null, JSON.parse(result.substr(5))); } else callback(null, result); } else { callback(null, null); } }); } catch (e) { logger.error(e); callback(e); } } }; /* callback: (err) */ module.exports.remove = function(key, callback) { try { client.del(_prefix + key, function(err) { if (err) { logger.error(err); if (callback) callback(err); } else { if (callback) callback(null); } }); } catch(e) { logger.error(e); if (callback) callback(e); } }; /* prefix: the string prefix of the cache key or null to remove all CMS keys callback: (err) */ module.exports.removeAll = function(startsWith, callback) { startsWith = _prefix + (startsWith ? startsWith : ""); if (startsWith.substr(startsWith.length - 1, 1) !== '*') startsWith = startsWith + '*'; client.keys(startsWith, function(err, rows) { async.each(rows, function(row, callbackDelete) { logger.debug('Removing data from cache: ' + row); client.del(row, callbackDelete) }, callback) }); }; module.exports.count = function(callback) { if (callback) { client.dbsize(function (err, size) { if (err) { logger.error(err); callback(err); } else callback(null, size); }); } };