<?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>Brain Matters &#187; cache</title>
	<atom:link href="http://blog.agoragames.com/tag/cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agoragames.com</link>
	<description></description>
	<lastBuildDate>Mon, 16 Aug 2010 19:36:30 +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>Write if read returns nil</title>
		<link>http://blog.agoragames.com/2009/05/13/write-if-read-returns-nil/</link>
		<comments>http://blog.agoragames.com/2009/05/13/write-if-read-returns-nil/#comments</comments>
		<pubDate>Wed, 13 May 2009 14:14:00 +0000</pubDate>
		<dc:creator>Ola Mork</dc:creator>
				<category><![CDATA[Bending Rails]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://blog.agoragames.com/?p=246</guid>
		<description><![CDATA[Usually we use standard caching methods on our site (primarily fragment caching to avoid DB queries).
Occasionally we need to do something more fancy. These instances usually come up when we&#8217;re splitting one query into two because rails doesn&#8217;t support :force_index or :adapter_specific_find_options on ActiveRecord::Base.find. We understand this motivation but really hate ActiveRecord::Base.connection#find_by_sql or ActiveRecord::Base.connection#execute. These [...]]]></description>
			<content:encoded><![CDATA[<p>Usually we use standard caching methods on our site (primarily fragment caching to avoid DB queries).</p>
<p>Occasionally we need to do something more fancy. These instances usually come up when we&#8217;re splitting one query into two because rails doesn&#8217;t support :force_index or :adapter_specific_find_options on ActiveRecord::Base.find. We understand this motivation but really hate ActiveRecord::Base.connection#find_by_sql or ActiveRecord::Base.connection#execute. These are not rational hatreds.</p>
<p>So when we get into a situation where we&#8217;re going to be caching manually it&#8217;s usually in the controller and we almost always end up with a pattern of:</p>
<pre class="brush: ruby">
@object = Rails.cache.read(&#039;really/complicated/and/stinky/key&#039;)
if @object.nil?
@object = what_should_my_object_be?
end
</pre>
<p>That&#8217;s fine in a contrived example but we were doing this in about 10 different places and it looked like a good candidate for drying up.</p>
<p>Here&#8217;s the solution we use:</p>
<pre class="brush: ruby">
module ActiveSupport
module Cache
class Store
def read_and_write_if_nil(key, options = {})
object = read(key)
if object.nil?
object = yield
write(key, object, options)
end
object
end
end
end
end
</pre>
<p>And the production example looks like this:</p>
<pre class="brush: ruby">
account_ids = Rails.cache.read_and_write_if_nil(&quot;member_ids_for_clan_#{@clan.id}&quot;, :expires_in =&gt; 5.minutes) do
@clan.members.find(:all, :order =&gt; &#039;groupies DESC&#039;, :select =&gt; &#039;accounts.id&#039;).collect(&amp;amp;amp;:id)
end
</pre>




	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F05%2F13%2Fwrite-if-read-returns-nil%2F&amp;title=Write%20if%20read%20returns%20nil" title="Reddit"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F05%2F13%2Fwrite-if-read-returns-nil%2F&amp;title=Write%20if%20read%20returns%20nil&amp;bodytext=Usually%20we%20use%20standard%20caching%20methods%20on%20our%20site%20%28primarily%20fragment%20caching%20to%20avoid%20DB%20queries%29.%0D%0A%0D%0AOccasionally%20we%20need%20to%20do%20something%20more%20fancy.%20These%20instances%20usually%20come%20up%20when%20we%27re%20splitting%20one%20query%20into%20two%20because%20rails%20doesn%27t%20su" title="Digg"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F05%2F13%2Fwrite-if-read-returns-nil%2F&amp;title=Write%20if%20read%20returns%20nil&amp;notes=Usually%20we%20use%20standard%20caching%20methods%20on%20our%20site%20%28primarily%20fragment%20caching%20to%20avoid%20DB%20queries%29.%0D%0A%0D%0AOccasionally%20we%20need%20to%20do%20something%20more%20fancy.%20These%20instances%20usually%20come%20up%20when%20we%27re%20splitting%20one%20query%20into%20two%20because%20rails%20doesn%27t%20su" title="del.icio.us"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fblog.agoragames.com%2F2009%2F05%2F13%2Fwrite-if-read-returns-nil%2F&amp;t=Write%20if%20read%20returns%20nil" title="Facebook"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.agoragames.com%2F2009%2F05%2F13%2Fwrite-if-read-returns-nil%2F&amp;t=Write%20if%20read%20returns%20nil&amp;s=Usually%20we%20use%20standard%20caching%20methods%20on%20our%20site%20%28primarily%20fragment%20caching%20to%20avoid%20DB%20queries%29.%0D%0A%0D%0AOccasionally%20we%20need%20to%20do%20something%20more%20fancy.%20These%20instances%20usually%20come%20up%20when%20we%27re%20splitting%20one%20query%20into%20two%20because%20rails%20doesn%27t%20su" title="Tumblr"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Write%20if%20read%20returns%20nil%20-%20http%3A%2F%2Fblog.agoragames.com%2F2009%2F05%2F13%2Fwrite-if-read-returns-nil%2F" title="Twitter"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://blog.agoragames.com/2009/05/13/write-if-read-returns-nil/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->