forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
95 lines (83 loc) · 2.09 KB
/
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
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
import Redis from 'ioredis';
import blake3 from 'blake3';
import logger from './logger.js';
const redis = (process.env.SPACEX_REDIS) ? new Redis(process.env.SPACEX_REDIS) : new Redis();
let redisAvailable = false;
redis.on('error', () => {
redisAvailable = false;
});
redis.on('end', () => {
redisAvailable = false;
});
redis.on('ready', () => {
redisAvailable = true;
logger.info('Redis connected');
});
/**
* BLAKE3 hash func for redis keys
*
* @param {String} str String to hash
* @returns {String} Hashed result
*/
const hash = (str) => blake3.createHash().update(str).digest('hex');
/**
* Redis cache middleware
*
* @param {Number} ttl Cache TTL in seconds
* @returns {void}
*/
export default (ttl) => async (ctx, next) => {
if (process.env.NODE_ENV !== 'production') {
await next();
return;
}
if (!redisAvailable) {
ctx.response.set('spacex-api-cache-online', 'false');
await next();
return;
}
ctx.response.set('spacex-api-cache-online', 'true');
const { url, method } = ctx.request;
const key = `spacex-cache:${hash(`${method}${url}${JSON.stringify(ctx.request.body)}`)}`;
if (ttl) {
ctx.response.set('Cache-Control', `max-age=${ttl}`);
} else {
ctx.response.set('Cache-Control', 'no-store');
}
// Only allow cache on whitelist methods
if (!['GET', 'POST'].includes(ctx.request.method)) {
await next();
return;
}
let cached;
try {
cached = await redis.get(key);
if (cached) {
ctx.response.status = 200;
ctx.response.set('spacex-api-cache', 'HIT');
ctx.response.type = 'application/json';
ctx.response.body = cached;
cached = true;
}
} catch (e) {
cached = false;
}
if (cached) {
return;
}
await next();
const responseBody = JSON.stringify(ctx.response.body);
ctx.response.set('spacex-api-cache', 'MISS');
// Set cache
try {
if ((ctx?.response?.status !== 200) || !responseBody) {
return;
}
await redis.set(key, responseBody, 'EX', ttl);
} catch (e) {
console.log(`Failed to set cache: ${e.message}`);
}
};
export {
redis,
};