DrJohnZoidberg
Honorary Master
I'm building a custom website for our business which displays all our reporting in a nice graphical way using graphs and tables.
Everything is done with PHP and Javascript/jQuery and using MySQL for the database.
The thing is the site is being hosted locally at our office but I have also made it available to the outside and a few users will be accessing it internationally. Our office internet is pretty crap (we only have 4Mbit ADSL, so upload speed is slow) and we already have a bunch of other web services competing for the bandwidth.
I've already written a client-side caching function using javascript which stores data requests in the browsers session storage. The only issue here obviously is that once the session is killed the data must be retrieved again, MySQL queries need to be called, etc. Also it is only stored for that users browser and anyone else accessing it will obviously have to generate their own cache each time.
So basically I'm trying to decide whether I should leave it as is or use server-side caching. On the one side I want to reduce the amount of calls to the database but also with limited bandwidth I would like to limit the data requests from the server.
Which option would you chose in this scenario? If you think I should go server-side do you have any tips/resources where I might start with this?
Note that I'm not using any framework, although I have looked in to using something like Phalcon or Laravel but I don't have time to rewrite the entire project right now.
Here is the code for my client-side caching, maybe it's useful to someone else and maybe you could help me optimise it too and point out any issues
Everytime the page needs to display data I first use getCache() to see if the data is cached and if it's not then I retrieve the data from the server and use setCache() to store it.
Everything is done with PHP and Javascript/jQuery and using MySQL for the database.
The thing is the site is being hosted locally at our office but I have also made it available to the outside and a few users will be accessing it internationally. Our office internet is pretty crap (we only have 4Mbit ADSL, so upload speed is slow) and we already have a bunch of other web services competing for the bandwidth.
I've already written a client-side caching function using javascript which stores data requests in the browsers session storage. The only issue here obviously is that once the session is killed the data must be retrieved again, MySQL queries need to be called, etc. Also it is only stored for that users browser and anyone else accessing it will obviously have to generate their own cache each time.
So basically I'm trying to decide whether I should leave it as is or use server-side caching. On the one side I want to reduce the amount of calls to the database but also with limited bandwidth I would like to limit the data requests from the server.
Which option would you chose in this scenario? If you think I should go server-side do you have any tips/resources where I might start with this?
Note that I'm not using any framework, although I have looked in to using something like Phalcon or Laravel but I don't have time to rewrite the entire project right now.
Here is the code for my client-side caching, maybe it's useful to someone else and maybe you could help me optimise it too and point out any issues
Everytime the page needs to display data I first use getCache() to see if the data is cached and if it's not then I retrieve the data from the server and use setCache() to store it.
Code:
if(sessionStorage === null) console.log("Storage not supported by the browser");
//cached object
var temp = sessionStorage.getItem('cacheObj');
var cacheObj = $.parseJSON(temp);
if (cacheObj === null) {
var cacheObj = new Object();
}
function setCache(postId, postData) {
if (cacheObj.length > 0) {
var objectExists = false;
var milliseconds = new Date().getTime();
//check if we already have this data stored and is current
for (var i = 0; i < cacheObj.length; i++) {
if (cacheObj[i].postId === postId) {
objectExists = true;
}
}
// add the data to the object if it's not there already
if (!objectExists) {
cacheObj.push( { postId: postId, data: postData, timestamp: milliseconds } );
sessionStorage.setItem('cacheObj', JSON.stringify(cacheObj));
}
} else {
cacheObj = [ { postId: postId, data: postData, timestamp: milliseconds } ];
sessionStorage.setItem('cacheObj', JSON.stringify(cacheObj));
}
};
function getCache(postId) {
var milliseconds = new Date().getTime();
var validityMilliseconds = 60000*60*1; // 1 hour
//var validityMilliseconds = 30000; // 30 seconds
if (cacheObj.length > 0) {
for (var i = 0; i < cacheObj.length; i++) {
if (cacheObj[i].postId === postId) {
if (milliseconds < (cacheObj[i].timestamp + validityMilliseconds)) {
return cacheObj[i].data;
} else {
console.log('Object expired, destroying.');
cacheObj.splice(i, 1);
}
}
}
}
return false;
};