Skip to content

Commit

Permalink
Merge 5e9f337 into 4973fce
Browse files Browse the repository at this point in the history
  • Loading branch information
pasupulaphani committed May 23, 2020
2 parents 4973fce + 5e9f337 commit d029c72
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 556 deletions.
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"
}
10 changes: 4 additions & 6 deletions docs/redisCache.js.html
Expand Up @@ -113,7 +113,7 @@ <h1>redisCache.js</h1>
let options = {}
let store = null

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

/**
* @param {object} options
Expand Down Expand Up @@ -182,7 +182,7 @@ <h1>redisCache.js</h1>
* Sets the ttlInSeconds
* @returns {number} ttl
*/
exports.setTtlInSeconds = ttl => {
exports.setTtlInSeconds = (ttl) => {
options.ttlInSeconds = getNumber(ttl)
return options.ttlInSeconds
}
Expand Down Expand Up @@ -216,7 +216,7 @@ <h1>redisCache.js</h1>
* @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 @@ -247,9 +247,7 @@ <h1>redisCache.js</h1>
* @property {number} opts.ttlInSeconds - time to live in seconds
* @returns {string} 'OK' if successful
*/
exports.wrap = async (key, fn, {
ttlInSeconds
} = {}) => {
exports.wrap = async (key, fn, { ttlInSeconds } = {}) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds

if (ttl &amp;&amp; ttl === 0) {
Expand Down
10 changes: 5 additions & 5 deletions lib/redisCache.js
Expand Up @@ -8,7 +8,7 @@ const NotInitialisedError = require('./NotInitialisedError')
let options = {}
let store = null

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

/**
* @param {object} options
Expand All @@ -32,7 +32,7 @@ exports.init = ({
redisOptions,
poolOptions,
logger: createLogger(logger),
ttlInSeconds
ttlInSeconds: getNumber(ttlInSeconds)
}

store = new RedisStore(options)
Expand Down Expand Up @@ -77,7 +77,7 @@ exports.getTtlInSeconds = () => options.ttlInSeconds
* Sets the ttlInSeconds
* @returns {number} ttl
*/
exports.setTtlInSeconds = (ttl) => {
exports.setTtlInSeconds = ttl => {
options.ttlInSeconds = getNumber(ttl)
return options.ttlInSeconds
}
Expand Down Expand Up @@ -111,7 +111,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 @@ -145,7 +145,7 @@ exports.deleteAll = (pattern = '*') => this.getStore().deleteAll(pattern)
exports.wrap = async (key, fn, { ttlInSeconds } = {}) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds

if (ttl && ttl === 0) {
if (ttl === 0) {
debug(`Not caching, invalid ttl: ${ttlInSeconds}`)
return fn()
}
Expand Down
49 changes: 23 additions & 26 deletions lib/redisCache.test.js
Expand Up @@ -8,6 +8,7 @@ const {
getPoolOptions,
getRedisOptions,
getset,
getStore,
getTtlInSeconds,
keys,
set,
Expand All @@ -22,17 +23,9 @@ describe('redisCache', () => {
host: process.env.REDIS_HOST || '127.0.0.1',
auth_pass: process.env.REDIS_AUTH
}
const poolOptions = {
min: 2,
max: 4
}
const poolOptions = { min: 2, max: 4 }
const ttlInSeconds = 10
const options = {
name,
redisOptions,
poolOptions,
ttlInSeconds
}
const options = { name, redisOptions, poolOptions, ttlInSeconds }
init(options)

const key = 'chuck-norris'
Expand Down Expand Up @@ -69,6 +62,12 @@ describe('redisCache', () => {
})
})

describe('getStore', () => {
test('returns store', () => {
expect(getStore()).not.toBeNull()
})
})

describe('status', () => {
test('get store stats', () => {
const { name: statusName, size, available, pending } = status()
Expand Down Expand Up @@ -126,6 +125,10 @@ describe('redisCache', () => {

beforeAll(() => deleteAll())

afterEach(() => {
setTtlInSeconds(ttlInSeconds)
})

test("set if key doesn't exist", async () => {
const localKey = genRandomStr()

Expand All @@ -146,6 +149,7 @@ describe('redisCache', () => {

test('do nothing when ttlInSeconds=0', async () => {
const localKey = genRandomStr()
setTtlInSeconds(0)

const result = await wrap(localKey, fn, {
ttlInSeconds: 0
Expand All @@ -154,8 +158,9 @@ describe('redisCache', () => {
expect(result).toBe(newValue)
})

test('do nothing when ttlInSeconds=0', async () => {
test('do nothing when ttlInSeconds < 0', async () => {
const localKey = genRandomStr()
setTtlInSeconds(0)

const result = await wrap(localKey, fn, {
ttlInSeconds: -1
Expand All @@ -166,6 +171,7 @@ describe('redisCache', () => {

test('do nothing when ttlInSeconds is invalid', async () => {
const localKey = genRandomStr()
setTtlInSeconds('NOT_NUMBER')

const result = await wrap(localKey, fn, {
ttlInSeconds: 'NOT_NUMBER'
Expand All @@ -176,14 +182,11 @@ describe('redisCache', () => {
})

describe('keys', () => {
const keyValues = {
key1: 'value1',
'test:key2': 'value2'
}
const keyValues = { key1: 'value1', 'test:key2': 'value2' }

beforeAll(() => deleteAll())
beforeEach(() =>
Promise.all(Object.keys(keyValues).map((k) => set(k, keyValues[k])))
Promise.all(Object.keys(keyValues).map(k => set(k, keyValues[k])))
)

test('return all the keys', async () =>
Expand All @@ -203,14 +206,11 @@ describe('redisCache', () => {
})

describe('del', () => {
const keyValues = {
key1: 'value1',
key2: 'value2'
}
const keyValues = { key1: 'value1', key2: 'value2' }

beforeEach(async () => {
await deleteAll()
await Promise.all(Object.keys(keyValues).map((k) => set(k, keyValues[k])))
await Promise.all(Object.keys(keyValues).map(k => set(k, keyValues[k])))
})

test('delete keys array', async () => {
Expand All @@ -230,13 +230,10 @@ describe('redisCache', () => {
})

describe('deleteAll', () => {
const keyValues = {
key1: 'value1',
key2: 'value2'
}
const keyValues = { key1: 'value1', key2: 'value2' }

beforeEach(() =>
Promise.all(Object.keys(keyValues).map((k) => set(k, keyValues[k])))
Promise.all(Object.keys(keyValues).map(k => set(k, keyValues[k])))
)

test('delete all the keys', async () => {
Expand Down

0 comments on commit d029c72

Please sign in to comment.