/
example-cache.js
47 lines (40 loc) · 1.19 KB
/
example-cache.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
const express = require('express')
const graphqlHTTP = require('express-graphql')
const { GraphQLSchema } = require('graphql')
const { Query } = require('./types')
const { wrapper, metricsMiddleware, express: { chartMiddleware } } = require('../index')
const crypto = require('crypto')
const TTL = 5000
const app = express()
const schema = wrapper(new GraphQLSchema({ query: Query }), [
['*.*', metricsMiddleware],
['Album.photos', cache(TTL)]
])
app.use(chartMiddleware({ barWidth: 50 }))
app.use('/', graphqlHTTP((req, res) => ({
context: res.locals,
schema,
graphiql: true
})))
app.listen(4000)
/*
functions below created with sole purpose of the demo, do not use them in actual applications
*/
function makeHash (string) {
const hash = crypto.createHash('sha256')
hash.update(string)
return hash.digest('hex')
}
function cache (ttl) {
const dictionary = new Map()
return async (next, [, args]) => {
const hash = makeHash(JSON.stringify(args))
const ts = Date.now()
if (dictionary.has(hash) && ts - dictionary.get(hash).ts < ttl) {
return dictionary.get(hash).value
}
const value = await next()
dictionary.set(hash, { ts, value })
return value
}
}