Im having some trouble how I could realize it to have a dynamic website where the data is fetched via ajax to the user.
At the end of the ajax is a simple php script which opens a MySQL connection to my db and fetching some results (which looks to all users the same, there aren't any user specific results) to display them later for example in a table.
This is what I come so far. It is working but when there thousands of users who open the website ... this wouldn't be good and I don't think my server would update it fast enough.
So my intention is to update the wanted results only once every x seconds serversided with a cronjob maybe, I would save a lot resources.
The problem is that I don't know how I could realize this part, could I please get some help? Or should I forget the ajax stuff and start using node.js?
I've got a pretty good root server which is my webspace aswell so it would be possible to install some extra applications.
A really basic Node.js solution using memory-cache
and node-cron
:
var cache = require('memory-cache');
var CronJob = require('cron').CronJob;
var job;
var jobStarted = false;
var rateLimitExceeded = false;
var job = new CronJob('5 * * * * *', function() {
// Runs every 5 seconds
doALongDataBaseOperation(function(err, data) {
cache.put('cacheId', data);
});
});
app.get('/path/that/needs/caching', function(req, res) {
cache.get('cacheId', function(e, data) {
if (data) {
res.end(data);
} else {
// contingency in case the cache doesn't exist
doALongDataBaseOperation(function(err, data) {
res.end(data);
cache.put('cacheId', data);
});
}
});
});
You could cache the data using Memcache (or something similar). Your cronjob could run and refresh the cache as often as you would like.
Then your AJAX can call a simple PHP script to retrieve the data from your cache.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.