Hi! I'm currently reworking my site, so although this content is still online things may change or look odd. Please get in touch to find out how I can help you!

How to Tell if Query Caching is Enabled in CodeIgniter

CodeIgniter’s query caching functionality is great - once you’ve got your head around having to manually clear the caches when the database gets updated it works pretty much transparently, as a good cache should.

One annoying thing though is that there’s no documented way to tell if caching is enabled or not. Not a big problem most of the time, but occasionally it could be very handy indeed - for example, if you wanted to display some kind of “system status” panel, or maybe display a manual “clear cache” button in case something goes skewiff and you need to clear the cache.

The Answer

Having failed to find anything in the documentation or via a quick search, I decided to do what all good developers would and RTFS. Sure enough, there is a way of telling if query caching is enabled, as shown in this snippet from system/database/DB_driver.php shows:

// Is query caching enabled?  If the query is a "read type" 		
// we will load the caching class and return the previously 		
// cached query if it exists 		
if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) 		
{ 			
	if ($this->_cache_init()) 			
	{ 				
		$this->load_rdriver(); 				
		if (FALSE !== ($cache = $this->CACHE->read($sql))) 				
		{ 					
			return $cache; 				
		} 			
	} 
}

The key part of that snippet is in the very first line of code - if($this->cache_on == TRUE… - this is how CodeIgniter checks internally whether query caching is enabled. $this->cache_on is set to TRUE or FALSE accordingly when you run the $this->db->cache_on() and $this->db->cache_off() methods.

So, if you want to tell whether query caching is enabled, you just have to check the value of $this->db->cache_on.

Be Careful!

All well and good, but using the internal variable like this can be dangerous. $this->db->cache_on looks an awful lot like $this->db->cache_on() but they can have very different results. It would be best to have a getter that would just return the state of $this->db->cache_on, but because you can’t extend the database classes it’s not as simple as it would usually be in CodeIgniter.

I have submitted a ticket to the CodeIgniter Reactor forum - if you agree that a getter would be useful, cast your votes and we’ll see if we can get it included in a future release!

Main image: “Hard Disk” by fDhooghe

Tags: Caching CodeIgniter Databases Optimization PHP

Feedback

Sorry, feedback is now closed on this post, but please feel free to get in touch if you would like to talk about it!

Or find posts by tag →

Friends & Influences

  1. Aching Brain
  2. Andy Budd
  3. Anthony Killeen
  4. Ben Everard
  5. Cameron Moll
  6. Dan Cederholm
  7. Dan Mall
  8. Dave Shea
  9. Elliot Jay Stocks
  10. Jamie Knight
  11. Jamie Rumbelow
  12. Jason Santa Maria
  13. Jeff Croft
  14. Jeffrey Zeldman
  15. Jeremy Keith
  16. Jon Hicks
  17. Khoi Vinh
  18. Mark Boulton
  19. Matt Croucher
  20. Nocturnal Monkey
  21. Sarah Parmenter
  22. Shaun Inman
  23. Simon Collison
  24. Tim Van Damme
  25. Toby Howarth