simple-redis-cache
Version:
Simplistic node redis cache ready can scale with generic-pool support
277 lines (230 loc) • 10.5 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>redis_store.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="RedisCache.html">RedisCache</a><ul class='methods'><li data-type='method'><a href="RedisCache.html#deleteAll">deleteAll</a></li><li data-type='method'><a href="RedisCache.html#get">get</a></li><li data-type='method'><a href="RedisCache.html#getName">getName</a></li><li data-type='method'><a href="RedisCache.html#getPoolOptions">getPoolOptions</a></li><li data-type='method'><a href="RedisCache.html#getRedisOptions">getRedisOptions</a></li><li data-type='method'><a href="RedisCache.html#keys">keys</a></li><li data-type='method'><a href="RedisCache.html#set">set</a></li><li data-type='method'><a href="RedisCache.html#status">status</a></li><li data-type='method'><a href="RedisCache.html#wrap">wrap</a></li></ul></li><li><a href="RedisPool.html">RedisPool</a><ul class='methods'><li data-type='method'><a href="RedisPool.html#acquire">acquire</a></li><li data-type='method'><a href="RedisPool.html#availableCount">availableCount</a></li><li data-type='method'><a href="RedisPool.html#destroy">destroy</a></li><li data-type='method'><a href="RedisPool.html#drain">drain</a></li><li data-type='method'><a href="RedisPool.html#getName">getName</a></li><li data-type='method'><a href="RedisPool.html#getPoolOptions">getPoolOptions</a></li><li data-type='method'><a href="RedisPool.html#getPoolSize">getPoolSize</a></li><li data-type='method'><a href="RedisPool.html#getRedisOptions">getRedisOptions</a></li><li data-type='method'><a href="RedisPool.html#pendingCount">pendingCount</a></li><li data-type='method'><a href="RedisPool.html#release">release</a></li><li data-type='method'><a href="RedisPool.html#sendCommand">sendCommand</a></li><li data-type='method'><a href="RedisPool.html#status">status</a></li></ul></li><li><a href="RedisStore.html">RedisStore</a><ul class='methods'><li data-type='method'><a href="RedisStore.html#del">del</a></li><li data-type='method'><a href="RedisStore.html#deleteAll">deleteAll</a></li><li data-type='method'><a href="RedisStore.html#expire">expire</a></li><li data-type='method'><a href="RedisStore.html#get">get</a></li><li data-type='method'><a href="RedisStore.html#getName">getName</a></li><li data-type='method'><a href="RedisStore.html#getPoolOptions">getPoolOptions</a></li><li data-type='method'><a href="RedisStore.html#getRedisOptions">getRedisOptions</a></li><li data-type='method'><a href="RedisStore.html#keys">keys</a></li><li data-type='method'><a href="RedisStore.html#ping">ping</a></li><li data-type='method'><a href="RedisStore.html#sendCommand">sendCommand</a></li><li data-type='method'><a href="RedisStore.html#set">set</a></li><li data-type='method'><a href="RedisStore.html#status">status</a></li><li data-type='method'><a href="RedisStore.html#ttlInSeconds">ttlInSeconds</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">redis_store.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const RedisPool = require("./redis_pool");
const pick = require("lodash.pick");
const debug = require("debug")("simpleRedisStore");
/**
* @constructor
* @param {object} options - Accepts properties ["name", "redisOptions", "poolOptions", "logger"]
* @param {string} options.name - Name your store
* @param {object} options.redisOptions - opts from [node_redis#options-object-properties]{@link https://github.com/NodeRedis/node_redis#options-object-properties}
* @param {object} options.poolOptions - opts from [node-pool#createpool]{@link https://github.com/coopernurse/node-pool#createpool}
* @param {object} options.logger - Inject your custom logger
*/
const RedisStore = module.exports = function (options) {
options = pick(options, ["name", "redisOptions", "poolOptions", "logger"]);
this.name = options.name || `redisStore-${Math.random().toString(36).substr(2, 10)}`;
this.redisOptions = options.redisOptions;
this.poolOptions = options.poolOptions;
this.logger = require("./logger")(options.logger);
this.pool = null;
try {
this.pool = new RedisPool({
name: this.name,
redisOptions: this.redisOptions,
poolOptions: this.poolOptions,
logger: this.logger
});
// // since pool factory events are not triggered due to retry issue; a workaround
// this.testConnection()
// .then((res) => {
// console.log("#########################", res)
// debug("Redis store created.", this.pool.status())
// });
// this.pool.acquire()
} catch (e) {
debug("Failed to create", e);
this.pool = null;
throw e;
}
};
RedisStore.prototype.testConnection = function () {
debug("PING to test connection");
return this.ping()
.then(resp => {
if (resp !== "PONG") {
debug("expected PONG but got", resp);
const err = new Error("UNKNOWN_PING_RESPONSE");
err.message = "expected PONG but got : " + resp;
throw err;
}
})
.catch(e => {
debug("Failed to PING", e);
this.logger.error("Test connection failed", e);
throw e;
});
};
/**
* Returns factory.name for this pool
*
* @returns {string} Name of the pool
*/
RedisStore.prototype.getName = function () {
return this.pool.getName();
};
/**
* Returns this.redisOptions for this pool
*
* @returns {object} redis options given
*/
RedisStore.prototype.getRedisOptions = function () {
return this.pool.getRedisOptions();
};
/**
* Returns this.poolOptions for this pool
*
* @returns {object} pool options given
*/
RedisStore.prototype.getPoolOptions = function () {
return this.pool.getPoolOptions();
};
/**
* Send redis instructions
*
* @param {string} commandName - Name of the command
* @param {array} commandArgs - Args sent to the command
* @returns {promise} Promise resolve with the result or Error
*/
RedisStore.prototype.sendCommand = function () {
return this.pool.sendCommand.apply(this, arguments);
};
/**
* Returns supplied string if not return 'PONG'
*
* @param {string} str - Optional string
* @returns {string} supplied string if not return 'PONG'
*/
RedisStore.prototype.ping = function (str) {
return !str ? this.sendCommand("ping") : this.sendCommand("ping", str);
};
/**
* Returns value or null when the key is missing - See [redis get]{@link https://redis.io/commands/get}
*
* @param {string} key - key for the value stored
* @returns {string} value or null when the key is missing
*/
RedisStore.prototype.get = function (key) {
return this.sendCommand("get", key);
};
/**
* Returns 'OK' if successful
*
* @param {string} key - key for the value stored
* @param {string} value - value to stored
* @param {number} ttlInSeconds - time to live in seconds
* @returns {string} 'OK' if successful
*/
RedisStore.prototype.set = function (key, value, ttlInSeconds) {
if (ttlInSeconds) {
return this.sendCommand("setex", [key, ttlInSeconds, value]);
} else {
return this.sendCommand("set", [key, value]);
}
};
/**
* Returns the number of keys that were removed - See [redis del]{@link https://redis.io/commands/del}
*
* @param {array} keys - list of keys to delete
* @returns {number} The number of keys that were removed.
*/
RedisStore.prototype.del = function (keys) {
return this.sendCommand("del", keys);
};
/**
* Returns 1 if the timeout was set/ 0 if key does not exist or the timeout could not be set - See [redis expire]{@link https://redis.io/commands/expire}
*
* @param {string} key - key to set expire
* @param {number} ttlInSeconds - time to live in seconds
* @returns {number} 1 if the timeout was set successfully; if not 0
*/
RedisStore.prototype.expire = function (key, ttlInSeconds) {
return this.sendCommand("expire", [key, ttlInSeconds]);
};
/**
* Returns TTL in seconds, or a negative value in order to signal an error - See [redis ttl]{@link https://redis.io/commands/ttl}
*
* @param {string} key - list of keys to delete
* @returns {number} TTL in seconds, or a negative value in order to signal an error
*/
RedisStore.prototype.ttlInSeconds = function (key) {
return this.sendCommand("ttl", key);
};
/**
* Returns all keys matching pattern - See [redis keys]{@link https://redis.io/commands/keys}
*
* @param {string} pattern - glob-style patterns/default '*'
* @returns {array} all keys matching pattern
*/
RedisStore.prototype.keys = function (pattern) {
if (!pattern || pattern === "") {
pattern = "*";
}
return this.sendCommand("keys", pattern);
};
/**
* Deletes all keys matching pattern
*
* @param {string} pattern - glob-style patterns/default '*'
* @returns {array} The number of keys that were removed.
*/
RedisStore.prototype.deleteAll = function (pattern) {
if (!pattern || pattern === "") {
pattern = "*";
}
debug("clearing redis keys: ", pattern);
return this.keys(pattern)
.then(keys => {
if (keys.length > 0) {
debug("deleting keys ", keys);
return this.del(keys);
} else {
debug("no keys exists with pattern: ", pattern);
return Promise.resolve(true);
}
});
};
/**
* Returns pool status and stats
*
* @returns {object} store status and stats
*/
RedisStore.prototype.status = function () {
return this.pool.status();
};
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>