-
Notifications
You must be signed in to change notification settings - Fork 4
/
ranking.js
78 lines (64 loc) · 2.13 KB
/
ranking.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
const express = require('express')
const route = express.Router()
const encRedisClient = require('./cache/setup')
const init = connection => {
let classification = null
route.get('/', async (req, res) => {
await encRedisClient.getAsync('invalidatedCache', async (err, reply) => {
if (err) {
console.log(err.message)
}
if (reply) {
classification = null
}
})
if (!classification) {
const groupsRanking = await encRedisClient.getAsync('groupsRanking', async (err, reply) => {
if (err) {
console.log(err.message)
} else {
if (reply.length > 0) {
return reply
} else {
const [groups] = await connection.execute('SELECT groups.id, groups.name, SUM(guessings.score) as score FROM `groups` LEFT JOIN guessings ON guessings.group_id = groups.id GROUP BY groups.id ORDER BY score DESC')
await encRedisClient.setexAsync('groupsRanking', 84600, groups, (err) => {
if (err) {
console.log(err.message)
}
})
return groups
}
}
})
const usersRanking = await encRedisClient.getAsync('usersRanking', async (err, reply) => {
if (err) {
console.log(err.message)
} else {
if (reply.length > 0) {
return reply
} else {
const [users] = await connection.execute('SELECT users.id, users.name, SUM(guessings.score) as score FROM `users` LEFT JOIN guessings on guessings.user_id = users.id GROUP BY users.id ORDER BY score DESC')
await encRedisClient.setexAsync('usersRanking', 84600, users, (err) => {
if (err) {
console.log(err.message)
}
})
return users
}
}
})
await encRedisClient.setAsync('invalidatedCache', false, (err) => {
if (err) {
console.log(err.message)
}
})
classification = {
groupsRanking,
usersRanking
}
}
res.render('ranking', classification)
})
return route
}
module.exports = init