Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving to es6 class from prototype #33

Merged
merged 4 commits into from May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions .eslintrc
Expand Up @@ -5,12 +5,8 @@
"node": true,
"jest": true
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"no-multi-assign": 1,
"func-names": 1,
"no-underscore-dangle": 1
"arrow-parens": ["error", "as-needed"],
"no-underscore-dangle": 0
}
}
3 changes: 2 additions & 1 deletion .prettierrc
Expand Up @@ -2,5 +2,6 @@
"trailingComma": "none",
"tabWidth": 2,
"semi": false,
"singleQuote": true
"singleQuote": true,
"arrowParens": "avoid"
}
28 changes: 14 additions & 14 deletions docs/global.html
Expand Up @@ -294,7 +294,7 @@ <h5>Parameters:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line128">line 128</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line126">line 126</a>
</span>
</p>

Expand Down Expand Up @@ -467,7 +467,7 @@ <h5>Parameters:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line135">line 135</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line133">line 133</a>
</span>
</p>

Expand Down Expand Up @@ -640,7 +640,7 @@ <h5>Parameters:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line114">line 114</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line112">line 112</a>
</span>
</p>

Expand Down Expand Up @@ -762,7 +762,7 @@ <h4 class="name" id="getName">
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line50">line 50</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line48">line 48</a>
</span>
</p>

Expand Down Expand Up @@ -884,7 +884,7 @@ <h4 class="name" id="getPoolOptions">
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line62">line 62</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line60">line 60</a>
</span>
</p>

Expand Down Expand Up @@ -1006,7 +1006,7 @@ <h4 class="name" id="getRedisOptions">
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line56">line 56</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line54">line 54</a>
</span>
</p>

Expand Down Expand Up @@ -1229,7 +1229,7 @@ <h5>Parameters:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line104">line 104</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line102">line 102</a>
</span>
</p>

Expand Down Expand Up @@ -1351,7 +1351,7 @@ <h4 class="name" id="getTtlInSeconds">
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line74">line 74</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line72">line 72</a>
</span>
</p>

Expand Down Expand Up @@ -1638,7 +1638,7 @@ <h5>Parameters:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line21">line 21</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line19">line 19</a>
</span>
</p>

Expand Down Expand Up @@ -1786,7 +1786,7 @@ <h5>Parameters:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line121">line 121</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line119">line 119</a>
</span>
</p>

Expand Down Expand Up @@ -2009,7 +2009,7 @@ <h5>Parameters:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line92">line 92</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line90">line 90</a>
</span>
</p>

Expand Down Expand Up @@ -2131,7 +2131,7 @@ <h4 class="name" id="setTtlInSeconds">
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line80">line 80</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line78">line 78</a>
</span>
</p>

Expand Down Expand Up @@ -2253,7 +2253,7 @@ <h4 class="name" id="status">
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line68">line 68</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line66">line 66</a>
</span>
</p>

Expand Down Expand Up @@ -2528,7 +2528,7 @@ <h5 class="subsection-title">Properties:</h5>
<p class="tag-source">
<a href="redisCache.js.html" class="button">View Source</a>
<span>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line145">line 145</a>
<a href="redisCache.js.html">redisCache.js</a>, <a href="redisCache.js.html#line143">line 143</a>
</span>
</p>

Expand Down
20 changes: 8 additions & 12 deletions docs/redisCache.js.html
Expand Up @@ -107,14 +107,12 @@ <h1>redisCache.js</h1>

const RedisStore = require('./redisStore')
const createLogger = require('./createLogger')
const genRandomStr = require('./genRandomStr')
const NotInitialisedError = require('./NotInitialisedError')
const { genRandomStr, getPositiveNumOr0 } = require('./util')

let options = {}
let store = null

const getNumber = num => !Number.isNaN(num) &amp;&amp; num >= 0 ? num : null

/**
* @param {object} options
* @param {string} options.name - Name your store
Expand All @@ -137,7 +135,7 @@ <h1>redisCache.js</h1>
redisOptions,
poolOptions,
logger: createLogger(logger),
ttlInSeconds
ttlInSeconds: getPositiveNumOr0(ttlInSeconds)
}

store = new RedisStore(options)
Expand Down Expand Up @@ -183,7 +181,7 @@ <h1>redisCache.js</h1>
* @returns {number} ttl
*/
exports.setTtlInSeconds = ttl => {
options.ttlInSeconds = getNumber(ttl)
options.ttlInSeconds = getPositiveNumOr0(ttl)
return options.ttlInSeconds
}

Expand All @@ -195,7 +193,7 @@ <h1>redisCache.js</h1>
* @returns {string} 'OK' if successful
*/
exports.set = async (key, value, ttlInSeconds) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
const ttl = getPositiveNumOr0(ttlInSeconds) || options.ttlInSeconds
return ttl === 0 ? value : this.getStore().set(key, value, ttl)
}

Expand All @@ -207,7 +205,7 @@ <h1>redisCache.js</h1>
* @returns {string} 'OK' if successful
*/
exports.getset = async (key, value, ttlInSeconds) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
const ttl = getPositiveNumOr0(ttlInSeconds) || options.ttlInSeconds
return ttl === 0 ? value : this.getStore().getset(key, value, ttl)
}

Expand Down Expand Up @@ -247,12 +245,10 @@ <h1>redisCache.js</h1>
* @property {number} opts.ttlInSeconds - time to live in seconds
* @returns {string} 'OK' if successful
*/
exports.wrap = async (key, fn, {
ttlInSeconds
} = {}) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
exports.wrap = async (key, fn, { ttlInSeconds } = {}) => {
const ttl = getPositiveNumOr0(ttlInSeconds) || options.ttlInSeconds

if (ttl &amp;&amp; ttl === 0) {
if (ttl === 0) {
debug(`Not caching, invalid ttl: ${ttlInSeconds}`)
return fn()
}
Expand Down
1 change: 0 additions & 1 deletion lib/genRandomStr.js

This file was deleted.

20 changes: 9 additions & 11 deletions lib/redisCache.js
Expand Up @@ -2,14 +2,12 @@ const debug = require('debug')('nodeCacheRedis')

const RedisStore = require('./redisStore')
const createLogger = require('./createLogger')
const genRandomStr = require('./genRandomStr')
const NotInitialisedError = require('./NotInitialisedError')
const { genRandomStr, getPositiveNumOr0 } = require('./util')

let options = {}
let store = null

const getNumber = (num) => (!Number.isNaN(num) && num >= 0 ? num : null)

/**
* @param {object} options
* @param {string} options.name - Name your store
Expand All @@ -32,7 +30,7 @@ exports.init = ({
redisOptions,
poolOptions,
logger: createLogger(logger),
ttlInSeconds
ttlInSeconds: getPositiveNumOr0(ttlInSeconds)
}

store = new RedisStore(options)
Expand Down Expand Up @@ -77,8 +75,8 @@ exports.getTtlInSeconds = () => options.ttlInSeconds
* Sets the ttlInSeconds
* @returns {number} ttl
*/
exports.setTtlInSeconds = (ttl) => {
options.ttlInSeconds = getNumber(ttl)
exports.setTtlInSeconds = ttl => {
options.ttlInSeconds = getPositiveNumOr0(ttl)
return options.ttlInSeconds
}

Expand All @@ -90,7 +88,7 @@ exports.setTtlInSeconds = (ttl) => {
* @returns {string} 'OK' if successful
*/
exports.set = async (key, value, ttlInSeconds) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
const ttl = getPositiveNumOr0(ttlInSeconds) || options.ttlInSeconds
return ttl === 0 ? value : this.getStore().set(key, value, ttl)
}

Expand All @@ -102,7 +100,7 @@ exports.set = async (key, value, ttlInSeconds) => {
* @returns {string} 'OK' if successful
*/
exports.getset = async (key, value, ttlInSeconds) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
const ttl = getPositiveNumOr0(ttlInSeconds) || options.ttlInSeconds
return ttl === 0 ? value : this.getStore().getset(key, value, ttl)
}

Expand All @@ -111,7 +109,7 @@ exports.getset = async (key, value, ttlInSeconds) => {
* @param {string} key - key for the value stored
* @returns {any} value or null when the key is missing
*/
exports.get = (key) => this.getStore().get(key)
exports.get = key => this.getStore().get(key)

/**
* Returns all keys matching pattern
Expand Down Expand Up @@ -143,9 +141,9 @@ exports.deleteAll = (pattern = '*') => this.getStore().deleteAll(pattern)
* @returns {string} 'OK' if successful
*/
exports.wrap = async (key, fn, { ttlInSeconds } = {}) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
const ttl = getPositiveNumOr0(ttlInSeconds) || options.ttlInSeconds

if (ttl && ttl === 0) {
if (ttl === 0) {
debug(`Not caching, invalid ttl: ${ttlInSeconds}`)
return fn()
}
Expand Down