Skip to content

Commit

Permalink
BE: Request cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav Bhattarai committed Jan 16, 2021
1 parent 40fa491 commit 7809fb9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/Middleware/redis-request-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import redis from 'redis';
const cache = redis.createClient();

const RequestCache = (req, res, next)=>{
cache.get(`requests/${req.params.Username}`, (err, reply)=>{
cache.get(`friend-req/${req.params.Username}`, (err, reply)=>{
if(!err){
if(reply){
const data = JSON.parse(reply);
Expand Down
20 changes: 15 additions & 5 deletions server/Routes/friend-request.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import express from 'express';
import redis from 'redis';
import RequestCache from '../Middleware/redis-request-cache.js';
import RegisterModel from '../Models/register-model.js';

const router = express.Router();
const cache = redis.createClient();

router.get('/:username', (req, res)=>{
const Username = req.params.username;
router.get('/:Username', RequestCache, (req, res)=>{
const Username = req.params.Username;
RegisterModel.find().where("Username").equals(Username).then((response)=>{
if(response.length === 1){
return res.json({data: response[0].Requests});
cache.set(`friend-req/${Username}`, JSON.stringify(response[0].Requests), 'EX', 60*60, ()=>{
return res.json({data: response[0].Requests});
})

}else{
return res.json({no_requests: true})
cache.set(`friend-req/${Username}`, JSON.stringify(response[0].Requests), 'EX', 60*60, ()=>{
return res.json({no_requests: true});
})
}
})
})
Expand All @@ -28,7 +36,9 @@ router.put('/', (req, res)=>{
receiver_data.push({sender: YourName, ProfilePicture: YourProfile});
receiver.Requests = receiver_data;
receiver.save().then(()=>{
return res.json({ request_sent: true });
cache.set(`friend-req/${FriendName}`, JSON.stringify(receiver_data), 'EX', 60*60, ()=>{
return res.json({ request_sent: true });
})
})
}else{
return res.json({ request_redundant: true });
Expand Down

0 comments on commit 7809fb9

Please sign in to comment.