-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
109 lines (85 loc) · 2.31 KB
/
server.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const express = require('express');
const findPrime = require('./utils/findPrime');
const memcache = require('./services/memcache');
const cacheView = require('./middleware/cacheView');
const session = require('express-session');
const MemcacheStore = require('connect-memjs')(session);
const app = express();
app.set('view engine', 'ejs');
app.use(
session({
secret: 'your-session-secret',
resave: false,
saveUninitialized: true,
store: new MemcacheStore({
servers: [process.env.MEMCACHIER_SERVERS],
prefix: 'session_',
}),
})
);
/**
* Session sanity check middleware
*/
app.use(function (req, res, next) {
console.log('Session ID:', req.session.id);
// Get the item from the cache
memcache.get(`session_${req.session.id}`, (err, val) => {
if (err) console.log(err);
if (val !== null) {
console.log('Session from cache:', val.toString());
}
});
next();
});
/**
* Key is `n`
* Value is the number of 'likes' for `n`
*/
const likesMap = {};
app.get('/', cacheView, (req, res) => {
const n = req.query.n;
if (!n) {
res.render('index');
return;
}
let prime;
const key = 'prime_' + n;
memcache.get(key, (err, val) => {
if (err) console.log(err);
if (val !== null) {
// Use the value from the cache
// Convert Buffer string before converting to number
prime = parseInt(val.toString());
} else {
// No cached value available, find it
prime = findPrime(n);
memcache.set(key, prime.toString(), { expires: 0 }, (err) => {
if (err) console.log(err);
});
}
// Initialize likes for this number when necessary
if (!likesMap[n]) likesMap[n] = 0;
const locals = { n, prime, likes: likesMap[n] };
res.render('index', locals);
});
});
app.get('/like', (req, res) => {
const n = req.query.n;
if (!n) {
res.redirect('/');
return;
}
likesMap[n]++;
// The URL of the page being 'liked'
const url = `/?n=${n}`;
// The view for this URL has changed, so the cached version is no longer valid, delete it from the cache.
const key = `view_${url}`;
memcache.delete(key, (err) => {
if (err) console.log(err);
});
res.redirect(url);
});
const port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(`Example app is listening on port ${port}.`)
);