/
redisCache.js
167 lines (144 loc) · 4.64 KB
/
redisCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const debug = require('debug')('nodeCacheRedis')
const RedisStore = require('./redisStore')
const createLogger = require('./createLogger')
const genRandomStr = require('./genRandomStr')
const NotInitialisedError = require('./NotInitialisedError')
let options = {}
let store = null
const getNumber = num => (!Number.isNaN(num) && num >= 0 ? num : 0)
/**
* @param {object} options
* @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
* @param {integer} options.ttlInSeconds - Number of seconds to store by default
*/
exports.init = ({
name,
redisOptions,
poolOptions = {},
logger,
ttlInSeconds
} = {}) => {
if (store) return
options = {
name: name || `redisCache-${genRandomStr()}`,
redisOptions,
poolOptions,
logger: createLogger(logger),
ttlInSeconds: getNumber(ttlInSeconds)
}
store = new RedisStore(options)
}
exports.getStore = () => {
if (!store) throw NotInitialisedError('RedisCache not initialised')
return store
}
/**
* Returns name of this pool
* @returns {string} Name of the pool
*/
exports.getName = () => this.getStore().getName()
/**
* Returns redisOptions of this pool
* @returns {object} redis options given
*/
exports.getRedisOptions = () => this.getStore().getRedisOptions()
/**
* Returns poolOptions of this pool
* @returns {object} pool options given
*/
exports.getPoolOptions = () => this.getStore().getPoolOptions()
/**
* Returns pool status and stats
* @returns {object} cache and its store status and stats
*/
exports.status = () => this.getStore().status()
/**
* Return the ttlInSeconds
* @returns {number?} ttlInSeconds
*/
exports.getTtlInSeconds = () => options.ttlInSeconds
/**
* Sets the ttlInSeconds
* @returns {number} ttl
*/
exports.setTtlInSeconds = ttl => {
options.ttlInSeconds = getNumber(ttl)
return options.ttlInSeconds
}
/**
* 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
*/
exports.set = async (key, value, ttlInSeconds) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
return ttl === 0 ? value : this.getStore().set(key, value, ttl)
}
/**
* 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
*/
exports.getset = async (key, value, ttlInSeconds) => {
const ttl = getNumber(ttlInSeconds) || options.ttlInSeconds
return ttl === 0 ? value : this.getStore().getset(key, value, ttl)
}
/**
* Returns value or null when the key is missing
* @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)
/**
* Returns all keys matching pattern
* @param {string} pattern - glob-style patterns/default '*'
* @returns {array} all keys matching pattern
*/
exports.keys = (pattern = '*') => this.getStore().keys(pattern)
/**
* Delete keys
* @param {array} keys - keys for the value stored
* @returns {number} The number of keys that were removed.
*/
exports.del = (keys = []) => this.getStore().del(keys)
/**
* Deletes all keys matching pattern
* @param {string} pattern - glob-style patterns/default '*'
* @returns {number} The number of keys that were removed.
*/
exports.deleteAll = (pattern = '*') => this.getStore().deleteAll(pattern)
/**
* Wraps promise to set its value if not exists.
* @param {string} key - key for the value stored
* @param {function} fn - function to call if not cache found
* @param {object} opts - options for wrap
* @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
if (ttl === 0) {
debug(`Not caching, invalid ttl: ${ttlInSeconds}`)
return fn()
}
const cachedValue = await this.getStore().get(key)
if (!cachedValue) {
debug('MISS', {
key
})
const value = await Promise.resolve(fn())
await this.set(key, value, ttlInSeconds)
return value
}
debug('HIT', {
key
})
return cachedValue
}