Skip to content

Commit 341c2e3

Browse files
author
ml9951
committed
Periodically refresh the server cache (every 20 min.)
1 parent c9840cd commit 341c2e3

File tree

1 file changed

+103
-79
lines changed

1 file changed

+103
-79
lines changed

backend/routes.js

+103-79
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,85 @@ const REDIRECT_URL = 'https://www.thebeerfeed.com/AuthRedirect';
99

1010
var feedCache = {};
1111

12+
function getData(feed, lastID){
13+
var cache = feedCache[feed];
14+
return Checkin.aggregate([
15+
{
16+
$match : {
17+
$and : [
18+
{ checkin_username : feed },
19+
{ checkin_id : { $gt : lastID } },
20+
{ venue_category : { $ne : 'Travel & Transport'} },
21+
{ venue_category : { $ne : 'Outdoors & Recreation' } },
22+
{ venue_url : { $ne : null } }
23+
]
24+
}
25+
},
26+
{
27+
$group : {
28+
_id : { venue_id : "$venue_id", bid : "$bid" },
29+
checkin_id : { $max : '$checkin_id'},
30+
name : { $first : '$beer_name' },
31+
bid : { $first : '$bid' },
32+
lat : { $first : '$lat' },
33+
lon : { $first : '$lon' },
34+
venue : { $first : '$venue' },
35+
brewery : { $first : '$brewery_name' },
36+
venue_id : { $first : '$venue_id' },
37+
created : { $max : '$checkin_created' },
38+
checkin_count : { $sum : 1 }
39+
}
40+
},
41+
{
42+
$lookup : {
43+
from : 'beers',
44+
localField : 'bid',
45+
foreignField : 'bid',
46+
as : 'beer'
47+
}
48+
},
49+
{ $unwind : '$beer' },
50+
{
51+
$project : {
52+
"beer._id" : 0,
53+
"beer.brewery_id" : 0,
54+
"beer.last_updated" : 0,
55+
"beer.twitter" : 0,
56+
"beer.brewery_url" : 0,
57+
"beer.bid" : 0,
58+
"beer.brewery" : 0,
59+
"beer.facebook" : 0,
60+
"beer.brewery_slug" : 0,
61+
}
62+
},
63+
{ $match : { "beer.rating" : { $gte : 4.0 } } },
64+
{ $sort : { checkin_id : -1 } }
65+
])
66+
.then(result => {
67+
const twoDays = new Date(new Date() - 1000 * 60 * 60 * 24 * 2);
68+
cache = (cache || []).filter(c => (new Date(c.created)) > twoDays);
69+
70+
const allCheckins = result.concat(cache || []);
71+
feedCache[feed] = allCheckins;
72+
return allCheckins;
73+
})
74+
}
75+
76+
const usersPromise = User.find({general_purpose : false})
77+
78+
function refresh(){
79+
console.log('Refreshing cache')
80+
usersPromise.then(users => {
81+
users.forEach(user => {
82+
const cache = feedCache[user.id];
83+
const min_id = (cache && cache.length > 0) ? cache[0].checkin_id : 0;
84+
getData(user.id, min_id)
85+
})
86+
})
87+
}
88+
89+
setInterval(1000 * 60 * 20, refresh)
90+
1291
module.exports = function(untappd){
1392
/**
1493
* Get the feed for a particular location
@@ -19,88 +98,33 @@ module.exports = function(untappd){
1998
router.get('/Feed/:user/:last_id?', (req, res) => {
2099
var cache = feedCache[req.params.user];
21100
const min_id = (cache && cache.length > 0) ? cache[0].checkin_id : 0;
22-
Checkin.aggregate([
23-
{
24-
$match : {
25-
$and : [
26-
{ checkin_username : req.params.user },
27-
{ checkin_id : { $gt : min_id } },
28-
{ venue_category : { $ne : 'Travel & Transport'} },
29-
{ venue_category : { $ne : 'Outdoors & Recreation' } },
30-
{ venue_url : { $ne : null } }
31-
]
32-
}
33-
},
34-
{
35-
$group : {
36-
_id : { venue_id : "$venue_id", bid : "$bid" },
37-
checkin_id : { $max : '$checkin_id'},
38-
name : { $first : '$beer_name' },
39-
bid : { $first : '$bid' },
40-
lat : { $first : '$lat' },
41-
lon : { $first : '$lon' },
42-
venue : { $first : '$venue' },
43-
brewery : { $first : '$brewery_name' },
44-
venue_id : { $first : '$venue_id' },
45-
created : { $max : '$checkin_created' },
46-
checkin_count : { $sum : 1 }
47-
}
48-
},
49-
{
50-
$lookup : {
51-
from : 'beers',
52-
localField : 'bid',
53-
foreignField : 'bid',
54-
as : 'beer'
55-
}
56-
},
57-
{ $unwind : '$beer' },
58-
{
59-
$project : {
60-
"beer._id" : 0,
61-
"beer.brewery_id" : 0,
62-
"beer.last_updated" : 0,
63-
"beer.twitter" : 0,
64-
"beer.brewery_url" : 0,
65-
"beer.bid" : 0,
66-
"beer.brewery" : 0,
67-
"beer.facebook" : 0,
68-
"beer.brewery_slug" : 0,
69-
}
70-
},
71-
{ $match : { "beer.rating" : { $gte : 4.0 } } },
72-
{ $sort : { checkin_id : -1 } }
73-
])
74-
.then(result => {
75-
const twoDays = new Date(new Date() - 1000 * 60 * 60 * 24 * 2);
76-
cache = (cache || []).filter(c => (new Date(c.created)) > twoDays);
77-
78-
const allCheckins = result.concat(cache || []);
79-
feedCache[req.params.user] = allCheckins;
80-
if(allCheckins.length == 0){
81-
res.json({
82-
lastID : req.params.last_id || 0,
83-
checkins : []
84-
})
85-
}else{
86-
if(req.params.last_id){
87-
const last_id = +req.params.last_id;
101+
102+
getData(req.params.user, min_id)
103+
.then(allCheckins => {
104+
if(allCheckins.length == 0){
88105
res.json({
89-
lastID : allCheckins[0].checkin_id,
90-
checkins : allCheckins.filter(c => c.checkin_id > last_id)
91-
});
92-
}else{
93-
res.send({
94-
lastID : allCheckins[0].checkin_id,
95-
checkins : allCheckins
106+
lastID : req.params.last_id || 0,
107+
checkins : []
96108
})
109+
}else{
110+
if(req.params.last_id){
111+
const last_id = +req.params.last_id;
112+
res.json({
113+
lastID : allCheckins[0].checkin_id,
114+
checkins : allCheckins.filter(c => c.checkin_id > last_id)
115+
});
116+
}else{
117+
res.send({
118+
lastID : allCheckins[0].checkin_id,
119+
checkins : allCheckins
120+
})
121+
}
97122
}
98-
}
99-
})
100-
.catch(err => {
101-
console.log(err);
102-
res.status(500).send(err)
103-
})
123+
})
124+
.catch(err => {
125+
console.log(err);
126+
res.status(500).send(err)
127+
})
104128
});
105129

106130
/**

0 commit comments

Comments
 (0)