-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
87 lines (58 loc) · 2.43 KB
/
main.js
File metadata and controls
87 lines (58 loc) · 2.43 KB
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
const express = require('express')
const database = require('./database')
const cache = require('./cache')
const ProgramModel = require('./programs.model')
const app = express()
const PORT = 4000
// define our routes
database.connect()
cache.connect()
app.get('/programs', async (req, res) => {
const key = `api:get:programs`
// check cache
const cachedData = await cache.redis.get(key)
// if in cache return data
if (cachedData) {
console.log('returning data from cache')
return res.json({ message: 'success retrieved programs', programs: JSON.parse(cachedData) })
}
console.log('fetching data from db')
// if not in cache query db and return data, set data in cache
const programs = await ProgramModel.find()
// set data in cache
await cache.redis.set(key, JSON.stringify(programs), { EX: 24 * 60 * 60 * 7})
return res.json({ message: 'success retrieved programs', programs })
})
app.get('/programs/:department_id', async (req, res) => {
const { department_id } = req.params
console.log(department_id)
const db_query = { department_id: Number(department_id) }
const key = `db:programs:${JSON.stringify(db_query)}`
// check cache
const cachedData = await cache.redis.get(key)
// if in cache return data
if (cachedData) {
console.log('returning data from cache')
return res.json({ message: 'success retrieved programs', programs: JSON.parse(cachedData) })
}
console.log('fetching data from db')
// if not in cache query db and return data, set data in cache
const programs = await ProgramModel.find(db_query)
// set data in cache
await cache.redis.set(key, JSON.stringify(programs), { EX: 24 * 60 * 60 * 7})
return res.json({ message: 'success retrieved programs', programs })
})
// on update or delete of data, delete cache where related query exists
app.patch('/programs/:department_id', async (req, res) => {
const { department_id } = req.params
const db_query = { department_id: Number(department_id) }
const key = `db:programs:${JSON.stringify(db_query)}`
// if not in cache query db and return data, set data in cache
const programs = await ProgramModel.findOneAndUpdate(db_query)
// delete key from redis
await cache.redis.del(key)
return res.json({ message: 'success retrieved programs', programs })
})
app.listen(PORT, () => {
console.log(`App is listening on port: ${PORT}`)
})