- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
- (06/12) View Layer - Getting count of Entities in View in Cakephp 3TAGS:CakephpCake3
- (05/23) Changing URL in beforeRedirect() in Component - Cakephp 3TAGS:CakephpCake3Web Programming
- (01/28) Foundation CSS Framework ResourcesTAGS:FoundationCss
- (01/27) Change View File from Controller in CakePHP 3TAGS:CakephpCake3UsageWeb Programming
- (01/13) Loading Model/Table Anywhere in Cake3TAGS:Cake3Cakephp
- (10/06) CURL PHP 5.5 Issue on Windows (Using Twilio SDK)
Subscribe to my feed
Cache Results from Query in CakePHP
Posted on 03/24/2010 at 11:18 am by Kevin Wentworth
Viewed 10,024 times | 0 comments
I have a HUGE query that I wanted to cache. I love CakePHP's caching functionality out of the box, but one thing is missing- you can't cache the results of a query. Let me clarify. You can cache the reults of a query, but only for that instance of a page load. Using $this->cacheQueries only caches the query to memory, which only lasts for the duration of the page load. If you want to cache query results to the file system, listen to Miles.
Cache Query Results in CakePHP
You'll need to add a function to your app_model.php, setup a Cache configuration and add a folder to your app/tmp/cache directory. After that, you can cache queries easily:
- $this->ModelName->find('all', array('cache'=>'cacheKey'));
One Modification (I couldn't resist)
Instead of checking if Cache.disabled is === false, I just changed it to a simple !Cache.disabled. I did this so the function would work with how core.php ships, which has Configure::write('Cache.disable', true); commented out. To recap:
- if (!Configure::read('Cache.disable') && ...
- //instead of
- if (Configure::read('Cache.disable') === false && ...
The above change is a mere matter of preference.
Get the Query Caching Code
Get the code at Miles' blog: query caching in CakePHP (a brilliant and simple approach!)