-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheController.js
More file actions
40 lines (38 loc) · 987 Bytes
/
cacheController.js
File metadata and controls
40 lines (38 loc) · 987 Bytes
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
// Import redis
let Redis = require("redis");
// Setup redis
const redisClient = Redis.createClient({
legacyMode: true,
});
(async () => {
await redisClient.connect();
})();
redisClient.on("connect", () => console.log("Redis Client Connected"));
redisClient.on("error", (err) =>
console.log("Redis Client Connection Error", err)
);
// Import photo model
const Photo = require("./photoModel");
exports.get = async function (req, res) {
redisClient.get("photos", (error, photos) => {
if (error) {
res.status(500).send(err);
return;
}
if (photos) {
console.log("Get from cache");
res.status(200).send(JSON.parse(photos));
return;
} else {
Photo.get(function (err, photosDb) {
if (err) {
res.status(500).send(err);
return;
}
redisClient.setEx("photos", 3600, JSON.stringify(photosDb));
console.log("Set cache");
res.status(200).send(photosDb);
});
}
});
};