codeigniter-2page-cachingdatabase-caching

Codeigniter Web Page Caching and Database Caching both?


I am still new into Codeigniter framework. Today I read about Database Caching http://codeigniter.com/user_guide/database/caching.html and Web Page Caching http://codeigniter.com/user_guide/general/caching.html.

I am a bit confused if database caching makes any big sense once page view is already in cache. So if the page is in cache, it won't go to database anyway.

The only point I see in the following scenario: If I load 30 results from db, then use php to shuffle results and pull from array 10 results. Next time when page cache is deleted, I will still have 30 results from db in cache, but this time there will be different results after shuffle those 30 results.

Am I missing something, is there any other scenario when having database cache would bring any benefit when using also page caching?


Solution

  • Database caching can benefit you also when using page caching. If your page is generated by several database queries, where some data is constant while the other changes frequently.

    In this case you will want to set the page caching to a short time period and retrieve the new data from the database each time while using the same constant data without querying the database.

    Example: lets say your frequent data needs to be refreshed every 5 minutes while the constant data changes every 24 hours. In this case you will set the page caching to 5 minutes. Over a period of 24 hours you have queried the database 288 times for the frequent data but have queried for the constant data only once. It totals to 289 queries instead of 576 if you haven't used database caching.