about:benjie

Random learnings and other thoughts from an unashamed geek

CakePHP Tips: Disable Database Caching

| Comments

Cake caches database queries. This is good, generally, it speeds things up a lot. But it is not always good. For example, if I want to query the database once a second to look for updates (e.g. for my chat application) the you don’t want the cache. Here’s how to clear it:

1) Create /app/app_model.php, and add in it the following

1
2
3
4
5
6
7
<?php
class AppModel extends Model{
 function \_clearDBCache() {
  $db =& ConnectionManager::getDataSource($this-\>useDbConfig);
  $db-\>\_queryCache = array();
  }
}

Note: you don’t always need a close “?>”

2) Call $modelNameHere->_clearDBCache() whereever you need the DB cache cleared.

That’s it! Have fun!

Whilst I am on the subject of CakePHP tips, why not look into using expects() to make your CakePHP queries more efficient - only request the data you need. Much better than using unbindModel and bindModel all over the place!

Comments