-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (122 loc) · 3.31 KB
/
index.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
const MemoryCache = require('memory-cache')
const Redis = require('ioredis')
/** Factory method
* @param {String} topic Unique identifier for this cache
* @param {Redis.RedisOptions|Redis.Redis} [redisOrOptions] Redis client instance or options
* @returns {Promise.<Cache>}
*/
exports.Create = async function factory(topic, redisOrOptions) {
const cache = new Cache(topic, redisOrOptions)
await cache.subscribe()
return cache
}
exports.Cache = Cache
/** Constructor
* @param {String} topic Unique identifier for this cache
* @param {Redis.RedisOptions|Redis.Redis} [redisOrOptions] Redis client instance or options
*/
function Cache(topic, redisOrOptions) {
if (typeof topic !== 'string') {
throw Error('Expected 1st argument "topic" of type string')
}
this.topic = topic
this.cache = new MemoryCache.Cache()
if (redisOrOptions instanceof Redis) {
this.redisPub = redisOrOptions
this.redisSub = redisOrOptions.duplicate()
} else {
this.redisOptions = redisOrOptions
this.redisSub = new Redis(redisOrOptions)
}
this.redisSub.on('message', (channel, key) => {
if (this.cb) {
this.cb(channel, key)
} else {
this.cache.del(key)
}
})
}
/**
* @returns {Promise}
*/
Cache.prototype.subscribe = function () {
return this.redisSub.subscribe(this.topic)
}
/**
* @returns {Promise}
*/
Cache.prototype.unsubscribe = function () {
return this.redisSub.disconnect()
}
/**
* @param {string} key The key to get
* @returns {*?} the value or `null`
*/
Cache.prototype.get = function (key) {
return this.cache.get(key)
}
/**
* @param {string} key The key to get
* @returns {Promise.<*?>} Evaluates to the value or `null`
*/
Cache.prototype.getRedis = function (key) {
const value = this.cache.get(key)
if (value === null) {
return this._redis().get(key)
.then(value => JSON.parse(value))
} else {
return value
}
}
/**
* @param {string} key The key to put
* @param {*?} value The value to store
* @param {number} [timeout] The timeout value (in ms.)
* @returns {*?} the value being stored
*/
Cache.prototype.put = function (key, value, timeout) {
return this.cache.put(key, value, timeout)
}
/**
* @param {string} key The key to put
* @param {*?} value The value to store
* @param {number} [timeout] The timeout value (in ms.)
* @returns {Promise.<*?>} Evaluates to the value being stored
*/
Cache.prototype.putRedis = function (key, value, timeout) {
const extra = timeout ? ['PX', timeout] : []
this._redis().set(key, JSON.stringify(value), ...extra)
.catch(err => {
/* istanbul ignore next */
console.error(err.stack)
})
return this.cache.put(key, value, timeout)
}
/**
* @param {string} key The key to invalidate
* @returns {Promise}
*/
Cache.prototype.invalidate = function (key) {
return this._redis().publish(this.topic, key)
}
/**
* @param {string} eventName Only 'invalidate' is accepted
* @param {function|null} cb callback function or null
*/
Cache.prototype.on = function (eventName, cb) {
if (eventName !== 'invalidate') {
throw Error('Event name must be "invalidate"')
}
this.cb = cb
}
/**
* @private
* @returns {Redis.Redis}
*/
Cache.prototype._redis = function () {
/* istanbul ignore next */
if (this.redisPub == null) {
this.redisPub = new Redis(this.redisOptions)
}
return this.redisPub
}