<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>laneolson.ca &#187; caching</title>
	<atom:link href="http://www.laneolson.ca/tag/caching/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.laneolson.ca</link>
	<description></description>
	<lastBuildDate>Fri, 07 Jan 2011 23:06:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Caching Data Queries in CakePHP</title>
		<link>http://www.laneolson.ca/2009/05/22/caching-data-queries-in-cakephp/</link>
		<comments>http://www.laneolson.ca/2009/05/22/caching-data-queries-in-cakephp/#comments</comments>
		<pubDate>Fri, 22 May 2009 16:44:44 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://laneolson.ca/?p=33</guid>
		<description><![CDATA[In a recent CMS project in CakePHP I was loading in some data from a model to use nearly on every page.  This data is not updated very often, so I figured I would cache the query.  The method of caching data I am going to outline can be done not just for queries&#8230; but for any kind of data really.  Lets get started!
The Cache Function
CakePHP&#8217;s cache function is quite easy to use.  The code snippet below shows how to use it.

1
2
3
4
5
6
7
8
9
10
11
$cache_name = 'query';
$cache_data = Cache::read&#40;$cache_name&#41;;
if &#40;empty&#40;$cache_data&#41;&#41;
&#123;
	$dataModel = $this-&#62;Model-&#62;find&#40;'first'&#41;;
	Cache::write&#40;$cache_name, $dataModel, array&#40;'config' =&#62; 'File', 'duration' =&#62; 7200&#41;&#41;;
&#125;
else
&#123;
	$dataModel = $cache_data;
&#125;

The code above attempts to read in the data from the cache, given the $cache_name.  If the data is not found, the query is executed and then written to the cache.  As you can see in the Cache::write I am using the File engine for caching, but you can use whichever you desire.  I also set the duration to 7200 seconds (2 hours).  If the cache data IS found then the query is skipped and the cache data is assigned to $dataModel.  Pretty nifty eh?
I also believe that when you do a save operation on the Settings model Cake knows to automatically remove that cache data, so you don&#8217;t end up with out of sync info!  If for some reason you want to manually delete the cached data you can use: Cache::delete($key) for a specific key, or Cache::clear() to delete everything in the cache.
]]></description>
			<content:encoded><![CDATA[<p>In a recent CMS project in CakePHP I was loading in some data from a model to use nearly on every page.  This data is not updated very often, so I figured I would cache the query.  The method of caching data I am going to outline can be done not just for queries&#8230; but for any kind of data really.  Lets get started!</p>
<h3>The Cache Function</h3>
<p>CakePHP&#8217;s cache function is quite easy to use.  The code snippet below shows how to use it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cache_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'query'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cache_data</span> <span style="color: #339933;">=</span> Cache<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$dataModel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Cache<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dataModel</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'File'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'duration'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">7200</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$dataModel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache_data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The code above attempts to read in the data from the cache, given the $cache_name.  If the data is not found, the query is executed and then written to the cache.  As you can see in the Cache::write I am using the File engine for caching, but you can use whichever you desire.  I also set the duration to 7200 seconds (2 hours).  If the cache data IS found then the query is skipped and the cache data is assigned to $dataModel.  Pretty nifty eh?</p>
<p>I also believe that when you do a save operation on the Settings model Cake knows to automatically remove that cache data, so you don&#8217;t end up with out of sync info!  If for some reason you want to manually delete the cached data you can use: Cache::delete($key) for a specific key, or Cache::clear() to delete everything in the cache.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2009/05/22/caching-data-queries-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

