views:

30

answers:

3

i have a module called online events in my website, this module will run for every 8 sec and fetches a recent activities in my websites. So, if more than 100 users are using my website at a time, it consumes more memory and more cpu usuage. what should i do to reduce cpu usage and memory, provided it should not affect a online events module. will it be would to do something with files here? pls help me.

A: 

If I understand it correctly the module needs to run every 8 seconds?

Why not store the time when the module ran last and then with every access from those thousands of users you only check if the time since the module last ran is bigger than 8 seconds? If it is you ran the module and store thwe new time, if it isn't you do nothing and wait for another user to access the site, to generate new online events for him?

mistrfu
A: 

At first, make the interval higher. Then, cache the module somehow on server side. E.g. make a static html file that gets loaded through a php file, and that will only get updated if something has changed in the db, so you only need to query the DB and render the data from it if really something happened. A simple approach to ensure cache update would be to remove the static file everytime something changes in the DB. This only makes sense if there are not too many DB changes.

joni
In principle this is the right approach - but PHP does not have sophisticated file locking semantics - store the cached content in a database to ensure updates are atomic (or use different filenames for different versions and store the current value int the database)
symcbean