Code Igniter User Guide Version 1.5.0


Database Caching Class

The Database Caching Class permits you to cache your queries as text files for reduced database load.

Important:  This class is initialized automatically by the database driver when caching is enabled. Do NOT load this class manually.

Also note:  Not all query result functions are available when you use caching. Please read this page carefully.

Enabling Caching

Caching is enabled in three steps:

How Does Caching Work?

Code Igniter's query caching system happens dynamically when your pages are viewed. When caching is enabled, the first time a webpage is loaded, the query result object will be serialized and stored in a text file on your server. The next time the page is loaded the cache file will be used instead of accessing your database. Your database usage can effectively be reduced to zero for any pages that have been cached.

Only read-type (SELECT) queries can be cached, since these are the only type of queries that produce a result. Write-type (INSERT, UPDATE, etc.) queries, since they don't generate a result, will not be cached by the system.

Cache files do NOT expire. Any queries that have been cached will remain cached until you delete them. The caching system does have an "auto-delete"feature, as described below. It also lets you manually clear caches associated with individulal pages, or you can delete the entire collection of cache files.

Will Caching Improve Your Site's Performance?

Maybe. Whether you see a performance gain as a result of caching depends on many factors. For example, if you have a highly optimized database under very little load, you probably won't see a performance boost. If your database is under heavy use you probably will see an improved response, assuming your filesystem is not overly taxed. Remember that caching simply changes how your information is retrieved, shifting it from being a database operation to a filesystem one.

In some clustered server environments caching may be detrimental since filesystem operations are so intense. On single servers (particularly in shared enironments) caching will probably be beneficial. Unfortunately there is no single answer to the question of whether you should cache your database. It really depends on your situation.

How are Cache Files Stored?

Code Igniter places the result of EACH query into its own cache file. Sets of cache files are further organized into sub-folders corrsponding to your controller functions.

For example, let's say you have a controller called blog with a function called comments that contains three queries. The caching system will create a cache folder called blog_comments, into which it will write three cache files.

If your have dynamic queries that change based on inormation in your URI (when using pagination, for example), each instance of the query will produce its own cache file. It's possible, therefore, to end up with many times more cache files than you have queries.

Managing your Cache Files

Since cache files do not expire, you'll need to build deletion routines into your application. For example, let's say you have a blog that allows user commenting. Whenever a new comment is submitted you'll want to delete the cache files associated with the controller function that serves up your comments. You'll find two delete functions described below that help you clear data.

Function Reference