simple-redis-cache
Version:
Simplistic node redis cache ready can scale with generic-pool support
208 lines (166 loc) • 8.21 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>redis_cache.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_cache.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const RedisStore = require("./redis_store");
const pick = require("lodash.pick");
const debug = require("debug")("simpleRedisCache");
/**
* @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 RedisCache = module.exports = function (options) {
options = pick(options, ["name", "redisOptions", "poolOptions", "logger"]);
this.name = options.name || `redisCache-${Math.random().toString(36).substr(2, 10)}`;
this.redisOptions = options.redisOptions;
this.poolOptions = options.poolOptions;
this.logger = require("./logger")(options.logger);
this.store = new RedisStore({
name: this.name,
redisOptions: this.redisOptions,
poolOptions: this.poolOptions,
logger: this.logger
});
};
/**
* 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
*/
RedisCache.prototype.set = function (key, value, ttlInSeconds) {
if (!this.store || ttlInSeconds === 0) return Promise.resolve(value);
return this.store.set(key, value, ttlInSeconds);
};
/**
* Returns value or null when the key is missing
*
* @param {string} key - key for the value stored
* @returns {string} value or null when the key is missing
*/
RedisCache.prototype.get = function (key) {
if (!this.store) return;
return this.store.get(key);
};
/**
* Returns all keys matching pattern
*
* @param {string} pattern - glob-style patterns/default '*'
* @returns {array} all keys matching pattern
*/
RedisCache.prototype.keys = function (pattern) {
if (!this.store) return;
if (!pattern || pattern === "") {
pattern = "*";
}
return this.store.keys();
};
/**
* Deletes all keys matching pattern
*
* @param {string} pattern - glob-style patterns/default '*'
* @returns {array} The number of keys that were removed.
*/
RedisCache.prototype.deleteAll = function (pattern) {
if (!this.store) return;
if (!pattern || pattern === "") {
pattern = "*";
}
return this.store.deleteAll();
};
/**
* Wraps promise to set its value if not exists.
*
* @param {string} key - key for the value stored
* @param {string} promise / value - value to stored
* @param {number} ttlInSeconds - time to live in seconds
* @returns {string} 'OK' if successful
*/
RedisCache.prototype.wrap = function (key, promise, ttlInSeconds) {
if (!this.store || ttlInSeconds === 0) {
return Promise.resolve(promise);
}
return this.store.get(key)
.then(value => {
if (!value) {
debug("MISS", {key: key});
return Promise.resolve(promise)
.then(value => this.set(key, value, ttlInSeconds).then(() => value));
} else {
debug("HIT", {key: key});
return Promise.resolve(value);
}
});
};
/**
* Returns factory.name for this pool
*
* @returns {string} Name of the pool
*/
RedisCache.prototype.getName = function () {
return this.store.getName();
};
/**
* Returns this.redisOptions for this pool
*
* @returns {object} redis options given
*/
RedisCache.prototype.getRedisOptions = function () {
return this.store.getRedisOptions();
};
/**
* Returns this.poolOptions for this pool
*
* @returns {object} pool options given
*/
RedisCache.prototype.getPoolOptions = function () {
return this.store.getPoolOptions();
};
/**
* Returns pool status and stats
*
* @returns {object} cache and its store status and stats
*/
RedisCache.prototype.status = function () {
return this.store.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>