Skip to content

Commit b889e4f

Browse files
committed
add redis cache to bin
1 parent 39fb618 commit b889e4f

File tree

3 files changed

+132
-6
lines changed

3 files changed

+132
-6
lines changed

bin/update-server.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,45 @@
55
process.title = 'update-server'
66

77
const Updates = require('..')
8+
const redis = require('redis')
9+
const { promisify } = require('util')
10+
const ms = require('ms')
11+
const assert = require('assert')
12+
13+
//
14+
// Args
15+
//
16+
817
const { TOKEN: token } = process.env
18+
assert(token, 'TOKEN required')
19+
20+
//
21+
// Cache
22+
//
23+
24+
const client = redis.createClient()
25+
const get = promisify(client.get).bind(client)
26+
const set = promisify(client.set).bind(client)
27+
28+
const cache = {
29+
async get (key) {
30+
const json = await get(key)
31+
return json && JSON.parse(json)
32+
},
33+
async set (key, value) {
34+
const multi = client.multi()
35+
multi.set(key, JSON.stringify(value))
36+
multi.expire(key, ms('15m'))
37+
const exec = promisify(multi.exec).bind(multi)
38+
await exec()
39+
}
40+
}
41+
42+
//
43+
// Go!
44+
//
945

10-
const updates = new Updates({ token })
46+
const updates = new Updates({ token, cache })
1147
const server = updates.listen(3000, () => {
1248
console.log(`http://localhost:${server.address().port}`)
1349
})

package-lock.json

Lines changed: 92 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"test": "prettier-standard '**/*.js' && standard && NODE_ENV=test nyc tap test/*.js"
55
},
66
"dependencies": {
7-
"node-fetch": "^2.1.1"
7+
"ms": "^2.1.1",
8+
"node-fetch": "^2.1.1",
9+
"redis": "^2.8.0"
810
},
911
"devDependencies": {
1012
"nodemon": "^1.17.1",

0 commit comments

Comments
 (0)