Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
Cache Results from Query in CakePHP
Posted on 03/24/2010 at 11:18 am by Kevin Wentworth
Viewed 15,219 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:
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!)
Cheers,
-Kevin Wentworth
Tags for Cache Results from Query in CakePHP
Cakephp | Database | Web Programming | Usage | Example
Comments for this Posting
No comments. Be the first to post a reply.
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!