Recent Posts
- (10/14) Forcing A Single Join in CakePHP PaginationTAGS:CakephpCakephp 13DatabaseErrorsHabtmMysqlMssqlWeb ProgrammingUsage
- (08/11) Inserting NOW() into MySQL Using CakePHPTAGS:CakephpDatabaseMysqlMssqlWeb ProgrammingUsage
- (08/07) Best PaginateCount for CakePHP - with Group By SupportTAGS:CakephpBehaviorsWeb ProgrammingUsageDatabaseExampleMysqlHabtmHack
- (04/03) CakePHP Error Messages Not Showing on FormTAGS:CakephpCakephp 13Web ProgrammingErrorsUsage
- (02/06) Sorting Paginated Results Using a Related Model Field in CakePHPTAGS:CakephpWeb ProgrammingUsageExample
- (11/02) Changing CakePHP's Model useTable on the FlyTAGS:CakephpWeb ProgrammingMysqlDatabaseExampleTutorial
- (10/18) The Funniest Error Message Ever - Thank You EclipseTAGS:EclipseWeb ProgrammingErrorsHumorWindowsUsageSoftwarePhpCakephp
- (09/27) Upgrading CakePHP Application to 1.3 (from 1.2)TAGS:CakephpUpgrade
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 2,292 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
