How to cache database heavy operations in php when database updates
How to cache database heavy page(almost 12tables it references to) so that i
- Give the user the latest page always
- I have a cached HTML output of the page for the output.
I already have a caching system where the page is cached for 1day and refreshes if its older than 1 day. But that takes 1 day (or what ever the time interval that is kept in the scrip) for changes to get affected. Can i have it done faster? I mean it should update itself when ever the row/col in those tables get updated.
Having a timestamp matching for when the row was updated would be again heavy as there are 12tables 开发者_如何学编程(each page referes to 1 row in them) and its as good as generating it dynamically. Besides the code is already written and it would be very difficult to go through every single operations that may insert/update/delete the tables.
You need to denormalize your database structure... The most unpainful method is creating some "cache" tables where will be only those columns which are being displayed... So you will have about 70% less tables and fields... Less joins, smart indexes...
After that you need to write a cron job, which will synchronize these cache tables with real ones. You can add some flag fields (for ex. is_modified, or date_last_modified, etc...) into your real tables and update these fields using triggers. After that your cron job will se what has gone with your real tables, for ex. during last 2-3 minutes and will sync these table accordingly...
This is a very project specific solution, but I had success with many projects using this cache technique...
精彩评论