<?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>Motion Standing Still &#187; Features</title>
	<atom:link href="http://www.motionstandingstill.com/category/features/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.motionstandingstill.com</link>
	<description>high performance ruby on rails</description>
	<lastBuildDate>Sun, 11 Jul 2010 03:46:07 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Nginx maintenance page configuration</title>
		<link>http://www.motionstandingstill.com/nginx-maintenance-page-configuration/2010-07-11/</link>
		<comments>http://www.motionstandingstill.com/nginx-maintenance-page-configuration/2010-07-11/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 03:45:29 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[maintenance-page]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=271</guid>
		<description><![CDATA[The process of moving clients from Apache to Nginx is nice and simple except for the 'down for maintenance' page.  This is my solution to that problem.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>The process of moving clients from Apache to Nginx is nice and simple except for the &#8216;down for maintenance&#8217; page.  This is my solution to that problem.</p>
<p>Google for how to do it and you&#8217;ll quickly encounter basically this solution where you symlink in the system folder to trigger it:</p>
<pre>  if ( -f $document_root/system/maintenance.html ) {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
   }</pre>
<p>Great, except that maintenance pages ofter have images in them too, and this rewrites those image requests to returning the maintenance page html.  You&#8217;d think you could do a multi-conditions if statement and have something like this which only rewrites when the request isn&#8217;t for stuff under the system folder:</p>
<pre>  if ( -f $document_root/system/maintenance.html &amp;&amp; !$uri ~ ^/system/ ) {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
   }</pre>
<p>That would be sensible, but nope doesn&#8217;t work. So a bit more googling throws up <a href="http://www.ruby-forum.com/topic/159703#703791" target="_blank">this ruby-forum post</a> which initially solved the problem nicely for me using a local variable in the config:</p>
<pre>  if ( -f $document_root/system/maintenance.html ) {
    set $maintenance 1;
  }

  if ( $uri ~ ^/system/ ) {
    set $maintenance 0;
  }

  if ( $maintenance ) {
    rewrite  ^(.*)$  /system/maintenance.html last;
  }</pre>
<p>Even though I was left with the sense of having just used a kludge at the time, I happily moved onto the next yak to shave.  That was until sometime later when I moved <a href="http://www.ponoko.com" target="_blank">Ponoko</a> to Nginx.  In the intervening time local variables in Nginx as I knew them had stopped working in that once it was set you couldn&#8217;t change it to something else.  Back to square one.</p>
<p>A bit of thinking and I hit upon a much more elegant solution &#8211; host the maintenance page assets on a sub-domain which would be controlled by a different Nginx server config and not have any rewrite problems:</p>
<pre>  server {

    listen 80;
    server_name maintenance.somedomain.com;

    # doc root
    root ;

    location / {

      if (-f $request_filename) {
        break;
      }

    }

  }</pre>
<p>I then edited our maintenance page to use the new subdomain for it&#8217;s images etc&#8230; and changed the main server conf back to using the original suggestion:</p>
<pre>  if ( -f $document_root/system/maintenance.html ) {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
   }</pre>
<pre>Problem solved, and it stopped feeling like a kludge.</pre>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/nginx-maintenance-page-configuration/2010-07-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Help!  I need an advisor</title>
		<link>http://www.motionstandingstill.com/help-i-need-an-advisor/2010-03-04/</link>
		<comments>http://www.motionstandingstill.com/help-i-need-an-advisor/2010-03-04/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 21:16:28 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=260</guid>
		<description><![CDATA[Last year I started a personal project to up-skill in some aspects of web development I was lacking in, like Javascript.  I had an idea for a game I'd been bouncing around for several years so I created that.  One thing led to another and now it's become quite a serious project...


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify; ">
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden; text-align: justify;">Last year I started a personal project to up-skill in some aspects of web development I was lacking in, like Javascript.  I had an idea for a game I&#8217;d been bouncing around for several years so I created that.  One thing led to another and now it&#8217;s become quite a serious project.</div>
<p style="text-align: justify;">Last year I started a personal project to up-skill in some aspects of web development I was lacking in, like Javascript.  I had an idea for a game I&#8217;d been bouncing around for several years so I created that.  One thing led to another and now it&#8217;s become quite a serious project.</p>
<p>The main page for <a href="http://www.esdao.com" target="_blank">ESDAO</a> gives an overview of what it is about, you can use this <a href="http://www.esdao.com/users/new?invite=29f78c3067d2012c4124404034a16a51" target="_blank">invite link</a> to take the game for a spin.</p>
<p>There is a small base of committed testers enjoying the game &#8211; but it&#8217;s not really growing much and I&#8217;m kinda stumped about the best way to fix that.  I have some ideas but I have little experience in this type of thing and would love to get advice from someone with a successful background doing just that.</p>
<p style="text-align: justify;">The game itself is close to completion and my next major tasks are getting more people playing it and sorting out the look and feel.  I have a designer lined up, but have yet to commission a site design while I work out what to do next.</p>
<p style="text-align: justify;">So, I&#8217;m after an advisor who I can meet on a regular basis.  Someone who I can pay for good objective advice.  I&#8217;m based in Wellington and would obviously prefer someone local.</p>
<p style="text-align: justify;">If you&#8217;d like to know more about me I have a <a href="http://www.motionstandingstill.com/available-for-hire/" target="_blank">page for that</a>.</p>
<p style="text-align: justify;">I&#8217;m best contacted via email &#8211; nahum.wild@gmail.com</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/help-i-need-an-advisor/2010-03-04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spandex Memcache Store is now in Rails 2.3</title>
		<link>http://www.motionstandingstill.com/spandex-memcache-store-is-now-in-rails-23/2009-02-04/</link>
		<comments>http://www.motionstandingstill.com/spandex-memcache-store-is-now-in-rails-23/2009-02-04/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 00:36:06 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Memcache]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[spandex mem cache store]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=228</guid>
		<description><![CDATA[Just a quick note to say that the local cache functionality of my Spandex Memcache Store Plugin has been included in Ruby on Rails 2.3 RC1.  After I released the plugin I was asked to submit a patch to Rails Edge adding it&#8217;s functionality &#8211; which I did over my Christmas break.  My changes have [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Just a quick note to say that the local cache functionality of my <a href="http://github.com/terrcin/spandex_mem_cache_store/tree/master" target="_blank">Spandex Memcache Store Plugin</a> has been included in <a href="http://weblog.rubyonrails.org/2009/2/1/rails-2-3-0-rc1-templates-engines-rack-metal-much-more" target="_blank">Ruby on Rails 2.3 RC1</a>.  After I released the plugin I was asked to submit a patch to Rails Edge adding it&#8217;s functionality &#8211; which I did over my Christmas break.  My changes have subsequently been re-factored a bit, decoupling the local cache functionality from the MemCacheStore class and moving it to being a wrapper around whatever caching store is used.  Hurray for open source!</p>
<p>The plugin is still useful as I didn&#8217;t submit it&#8217;s session caching changes to Rails &#8211; essentially using Rails.cache for session storage rather than establishing a second connection to MemCache.  This also benefits from the local cache changes.</p>
<p>Where next?  In the next week or so I plan on updating the plugin by removing the local cache functionality to prevent any conflicts with Rails 2.3 when it&#8217;s released.</p>
<p>After that, I&#8217;m planning on working through the list of changes detailed under <em>Future Plans</em> in the <a href="http://github.com/terrcin/spandex_mem_cache_store/blob/402a216f1a982d2eaacb2169802a9a41708366fb/README" target="_blank">plugins readme</a>, also listed below.</p>
<ul>
<li>TESTS! (I wrote a bunch for the patch, just need to do that for the remaining session changes)</li>
<li>Make this a Gem</li>
<li>Conditional caching</li>
<li>Multi-Get</li>
<li>Dynamic key helpers</li>
<li>Make the code more awesome</li>
</ul>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/spandex-memcache-store-is-now-in-rails-23/2009-02-04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up Postgres + Postgis on OSX</title>
		<link>http://www.motionstandingstill.com/setting-up-postgres-postgis-on-os/2008-12-18/</link>
		<comments>http://www.motionstandingstill.com/setting-up-postgres-postgis-on-os/2008-12-18/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 23:22:49 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Features]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[macports]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[postgis]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=217</guid>
		<description><![CDATA[I've just installed Postgresql for the first time on my MacBook - it was a pain and wasn't as simple as it should have been.  This is what I did to get it working.


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Even though I&#8217;m stupidly busy with my current work I&#8217;ve started getting ready for my next gig.  As this has ment installing Postgresql on my Macbook for the first time I thought I&#8217;d quickly share the solution to the pain I went through in getting it to work.</p>
<p style="text-align: justify;">I received two suggestions for the &#8216;best install method&#8217;, firstly MacPorts and then later pre-compiled dmg&#8217;s from <a href="http://www.kyngchaos.com/wiki/software:postgres" target="_blank" class="broken_link">Kyng Chaos</a>.  I already use ports extensively making that choice easy for me &#8211; I like being able to  uninstall things if they don&#8217;t work to well and was concerned the dmg option wouldn&#8217;t allow me to.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> postgresql83 postgresql83-doc postgresql83-server postgis</pre></div></div>

<p style="text-align: justify;">I then added the following to my path in ~/.zshrc so I could actually run things</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>postgresql83<span style="color: #000000; font-weight: bold;">/</span>bin</pre></div></div>

<p style="text-align: justify;">and started the server.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> launchctl load <span style="color: #660033;">-w</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>LaunchDaemons<span style="color: #000000; font-weight: bold;">/</span>org.macports.postgresql83-server.plist</pre></div></div>

<p style="text-align: justify;">All seemed to be fine and dandy as I could connect with <a href="http://www.pgadmin.org/" target="_blank">pgAdmin</a>.  I quickly discovered that I needed to create a user with the same login as my osx login when I tried creating a test db.</p>
<p style="text-align: justify;">Next I tried restoring my client&#8217;s db dump and it barfed big time.  As I&#8217;ll be working on some mapping software I&#8217;d installed the PostGIS package which seems to have failed to link up with Postgres properly.   I was getting the follow missing library error:</p>
<blockquote style="text-align: justify;"><p>could not load library /usr/lib/postgresl/liblwgeom.so.1.3</p></blockquote>
<p style="text-align: justify;">Obviously it&#8217;s looking in the wrong place for a start, so I setup a LIBDIR export in my profile to get over that problem and restarted the server.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LIBDIR</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>postgresql83</pre></div></div>

<p style="text-align: justify;">That still didn&#8217;t solve the problem though as I don&#8217;t have a file by that name on my system, I do however have liblwgeom.1.3.so which looked to be the same thing.  Pretty annoying, but easily solved with a symlink.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>postgresql83
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-snf</span> liblwgeom.1.3.so liblwgeom.so.1.3</pre></div></div>

<p style="text-align: justify;">Then the backup restored like a chalm.</p>
<p style="text-align: justify;">To get Rails talking to Postgresql I needed to install the postgres adapter gem appropriately called &#8216;postgres&#8217;.  I didn&#8217;t expect that to work straight off though as I&#8217;d installed postgresql with ports and the gem by default would be looking in the wrong place for it.  I was correct, but I also received an additional error complaining about the architechture not being specified &#8211; I needed to tell it I wanted i386.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">env</span> <span style="color: #007800;">ARCHFLAGS</span>=<span style="color: #ff0000;">&quot;-arch i386&quot;</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> postgres <span style="color: #660033;">--</span> <span style="color: #660033;">--with-pgsql-include-dir</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>postgresql83l <span style="color: #660033;">--with-pgsql-lib-dir</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>postgresql83<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p style="text-align: justify;">I gropped around for a bit trying to figure out the correct way of letting it know and eventually found the above solution on <a href="http://codesnippets.joyent.com/posts/show/1441" target="_blank">code snippets</a>.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/setting-up-postgres-postgis-on-os/2008-12-18/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Starting simple with Rails caching</title>
		<link>http://www.motionstandingstill.com/starting-simple-with-rails-caching/2008-11-27/</link>
		<comments>http://www.motionstandingstill.com/starting-simple-with-rails-caching/2008-11-27/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 22:16:41 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Memcache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[expire]]></category>
		<category><![CDATA[keys]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[scaling]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[stale data]]></category>
		<category><![CDATA[sweeper]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=195</guid>
		<description><![CDATA[So, you’ve launched your website and increasingly people are actually using it! Congratulations. Money is probably tight and your server(s) are starting to feel the strain but you can’t buy more - people get turned off by slow responding sites and traffic is $$. What do do?


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">So, you&#8217;ve launched your website and increasingly people are actually using it!  Congratulations. Money is probably tight and your server(s) are starting to feel the strain but you can&#8217;t buy more &#8211; people get turned off by slow responding sites and traffic is $$.  What do do?</p>
<p style="text-align: justify;">You&#8217;ve noticed that your app is doing a lot of repetitive work &#8211; calculating and showing the same data over and over again.  This doesn&#8217;t fit with your adherence to DRY principles at all.  If you cached repetitive responses your servers could give you way more bang for you buck &#8211; and it would only cost your time.</p>
<p style="text-align: justify;">You are about to embark on an exciting journey fraught with hidden dangers, so let me prepare you &#8211; cheesy I know but hey.</p>
<p style="text-align: justify;">First, you have actually done some code optimisation haven&#8217;t you?  Stopped using the default mysql config?  Checked your slow query log?  Used pl_analyise to parse your production logs?  Fixed the popular, but slow actions?  Done some eager loading so you get everything in a couple of hits from mysql rather than many many many little hits as your views render?  When you dream you see log tailing in the background?  Minified your javascript and css?  Static assets have expire headers?  Don&#8217;t have too many Mongrels running?  Sessions are stored in MemCache? Go do all that if you haven&#8217;t and only come back here if this are still too slow.  Oh and try to get your yslow score above 75.</p>
<p style="text-align: justify;">Note: I&#8217;m assuming that you have read a bit about caching in Rails already and have had a play, reading fragments from actions etc&#8230;  If not try googling as there are a number of great articles on it, the Agile book is probably a good start too.</p>
<p style="text-align: justify;"><strong>Start at the end</strong></p>
<p style="text-align: justify;">Instead of just saying &#8216;do this awesome thing&#8217; I&#8217;m gonna explain how I&#8217;ve gotten to saying that, as you shouldn&#8217;t be making blind decisions about these things.  Most of the time when I implement caching for clients I completely skip page caching and action caching and go straight to line item caching, ie caching of small fragments of html.  Sounds completely counter-intuitive doesn&#8217;t it, but it&#8217;s not.</p>
<p style="text-align: justify;">First off I need to define a typical client&#8217;s website so you see where I&#8217;m coming from.  It&#8217;s a user driven web 2.0 website &#8211; so most of the data is user generated and sometimes social networking elements are included, where a user&#8217;s data can be restricted to just their friends instead of being publicly available.  Basically the site lists stuff that people put on it.</p>
<p style="text-align: justify;">Why not page or action caching?  As a starting point it&#8217;s too hard to manage due to the continually changing nature of the data being presented.  This changing nature means your cache might not actually get used that much.  Additionally when you are logged in your name is typically on the page with other unique to you info &#8211; you have x messages etc&#8230;  The latter reason completely rules out caching a whole page for anyone that&#8217;s logged in.  Don&#8217;t restrict caching based speed increases to joe public as logged in people, ie people that give a damn about your site, shouldn&#8217;t be second class citizens.</p>
<p style="text-align: justify;"><strong>Don&#8217;t start with caching lists</strong></p>
<p style="text-align: justify;">Developers still want to jump straight in and cache lists of items (and pages too) rather than the individual line items &#8211; basically as much of the page as they can in a single hit &#8211; thinking they&#8217;ll get great bang for their buck.  When you deploy with that strategy you&#8217;re in for a world of hurt.  This is because they&#8217;ve not realised they need to take the following into account.</p>
<p style="text-align: justify;">Each time one of the line items changes you have to expire the cache for the whole listing.  If you have pagination then you&#8217;ll likely have to expire ALL cached pages unless you can 100% reliably work out which page the item is on.  So yes you have a separate cache entry for each page of the listing.  If someone adds or edits a line item you have to expire the whole lot &#8211; and thats what people should be doing all the time on your site right?.  Additionally, if you allow sorting or a filtering mechanism you have to uniquely cache by page and the additional dimension(s), thus further reducing the frequency the cache will be used.</p>
<p style="text-align: justify;"><strong>What&#8217;s the goal?</strong></p>
<p style="text-align: justify;">What we are trying to achieve with caching is to DRY up the execution of requests, but in a way that doesn&#8217;t add complexity.  The idea is to let your servers focus on the unique stuff like processing credit cards and telling Jill&#8217;s friends that based on her network and comment history she really doesn&#8217;t like doing things with Sarah unless Joe is there too.</p>
<p style="text-align: justify;">Keep this in mind.  With seeking something to cache, you are basically looking for something that has a high ratio of renders/requests of the exact same html in between changes of that html.  So the more content in the cached fragment, the more frequently you&#8217;ll need to expire it.  Putting that another way, the fewer data sources, instances of models and display dimensions like pagination, sorting and filtering used to make up the cached html the better.  The simplest you can go is one data source, ie a line item.  Remember, keep it simple.</p>
<p style="text-align: justify;">Put in business speak, it&#8217;s a cost / benefit decision.</p>
<p style="text-align: justify;"><strong>It&#8217;s all about the keys</strong></p>
<p style="text-align: justify;">When you store fragments in your cache you name them with a unique string based key so you can access them later -  exactly like a hash.  So yes, they are important.  Typically they looks like a url, sort of based on the actual url or controller/action and the dimensions involved to make it unique to the html being cached.  So for the above listing situation the key should look something like this:</p>
<p style="text-align: justify;">&#8216;/products/list/sort=price/status=in-stock/page=1&#8242;</p>
<p style="text-align: justify;">This actually presents a completely separate reason as to why you shouldn&#8217;t do list caching when you start out on this path.  The problem with this key is that to expire it you have to count the number of products to determine how many pages there are and then manually go and expire all possible keys that could have been generated, one at a time &#8211; this is because expiring via regex is not supported.  If additional dimensions are involved the problem will compound even further.</p>
<p style="text-align: justify;">Yes, that&#8217;s real ugly and will become completely unmanageable.  There are some clever tricks to help out there such as self expiring keys, but that&#8217;s a different topic which I&#8217;ll get to at a later date.</p>
<p style="text-align: justify;"><strong>When to expire</strong></p>
<p style="text-align: justify;">Wait there&#8217;s more!  You have to monitor data changes so you can expire those keys when they become stale as it doesn&#8217;t happen magically.  Normally you&#8217;d setup a sweeper (observer) and just expire things when &#8216;after_save&#8217; is triggered.  Not a problem.  If you have data from multiple models involved in your cached html fragments then you need to monitor all the models involved.  You can go as far as only expiring particular keys based on the attributes that are changing etc&#8230; now much easier in Rails 2.1 with Dirty Objects.</p>
<p style="text-align: justify;">This is all adding yet another layer of complexity.  Ugh.  It should all be documented so that other developers know about it especially new developers &#8211; otherwise further work could mess it up and you might start showing stale data to users.  In my book that&#8217;s pretty much on par with errors occurring as you could be showing the wrong price or even deleted data to users.  Unless you are careful about it, it can all become some really hard to manage mess which really isn&#8217;t going to give much benefit to your users.</p>
<p style="text-align: justify;"><strong>Line item caching</strong></p>
<p style="text-align: justify;">As you are not having to worry about other dimensions such as pagination, sort order and filtering, other line items or even if the user is logged in or not, the line item&#8217;s cache key should be something like this:</p>
<p style="text-align: justify;">&#8216;/products/#{product.id}/line_item&#8217;</p>
<p style="text-align: justify;">To make sure it expires correctly all you have to do is setup a sweeper watching the line item and simply delete it&#8217;s key when it changes &#8211; use Rails.cache.delete for this.  If the line item is rendered using data from through relationships, you then have to monitor them too.  I&#8217;ve seen people putting a &#8216;last_active_at&#8217; field on a line item and when anything related to it changes they update that time field.  That works nicely and things are kept simple.</p>
<p style="text-align: justify;"><strong>Finally</strong></p>
<p style="text-align: justify;">That should set you off in the right direction, remembering though this is a starting point for your first caching strategy.  Later on you&#8217;ll actually start doing multiple layers of caching and covering more complex html, such as lists, if your site continues to grow.</p>
<p style="text-align: justify;">Now hopefully I&#8217;ve convinced you to start out with caching the html for a single line item &#8211; or something just as simple.  If not post a comment and tell me why not.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/starting-simple-with-rails-caching/2008-11-27/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I don&#8217;t use Foreign Key Constraints with Rails</title>
		<link>http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/</link>
		<comments>http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 23:45:58 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[convention over configuration]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[dry]]></category>
		<category><![CDATA[foreign key constraints]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[pragmatism]]></category>
		<category><![CDATA[premature optmisation]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=152</guid>
		<description><![CDATA[A couple of months back we had Nigel talking at WellRailed about a Foreign Key Constraint plugin he uses with Rails and then just this week the topic was brought up again for me with a client.  I&#8217;ve found it&#8217;s something that pops up every now and again.  Given this is such a polarizing issue [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">A couple of months back we had <a href="http://workingwithrails.com/person/9576-nigel-ramsay" target="_blank">Nigel</a> talking at <a href="http://groups.google.com/group/WellRailed" target="_blank">WellRailed</a> about a Foreign Key Constraint plugin he uses with Rails and then just this week the topic was brought up again for me with a client.  I&#8217;ve found it&#8217;s something that pops up every now and again.  Given this is such a polarizing issue I thought I&#8217;d order my thoughts into a post explaining why I don&#8217;t use foreign key constraints, in particular cascading deletes.</p>
<p style="text-align: justify;">For me the reasoning is distilled down to my programming style.</p>
<p style="text-align: justify;"><strong>Pragmatism</strong></p>
<p style="text-align: justify;">When it comes down to it, what vital benefit does it give you over :dependent =&gt; :destroy or model validation?  Will this give you increased quality faster, quicker and cheaper?</p>
<p style="text-align: justify;"><strong>Premature Optmisation</strong></p>
<p style="text-align: justify;">Basically don&#8217;t waste time optimising something that isn&#8217;t slow or is hardly used.  Rails is so slow that some fractional speedup on the DB isn&#8217;t really going to be noticeable by the end user.  Instead examine your site with <a href="http://www.newrelic.com/" target="_blank">NewRelic</a> or <a href="http://www.fiveruns.com/" target="_blank" class="broken_link">FiveRuns</a> and fix your bottleneck code &#8211; way more bang for your buck.</p>
<p style="text-align: justify;"><strong>Data is Valuable</strong></p>
<p style="text-align: justify;">So don&#8217;t go around deleting it!  If a user wants to delete some data you should want to know why.  Looking at the data can aid in determining why without having to bug the user about it.  Yes there are occasions when this does need to happen, but they should be the exception to the rule and thus explicitly coded for.</p>
<p style="text-align: justify;">Just mark a record as deleted and filter it out automatically, that&#8217;s easy enough to do with rails &#8211; way easier than any other language I&#8217;ve used before.  Better yet, search <a href="http://www.github.com" target="_blank">github</a> and use someone else&#8217;s wheel.</p>
<p style="text-align: justify;"><strong>Convention Over Configuration</strong></p>
<p style="text-align: justify;">Basically put, you do things the way that everyone else has done it.  Walk the well walked path so to speak&#8217; and you won&#8217;t experience any bizzaro problems that someone else hasn&#8217;t already solved before you.  As you&#8217;ve created your app in a predictable way, someone else can easily come in and contribute to your project, or visa-versa and it makes the whole Rails community more accessible to you.</p>
<p style="text-align: justify;">So in following the conventions and as you are not encouraged to use foreign key constraints, Rails isn&#8217;t expecting data to be deleted out from underneath it.</p>
<p style="text-align: justify;"><strong>Don&#8217;t Repeat Yourself &amp;&amp; Model View Controller</strong></p>
<p style="text-align: justify;"><a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a> is another of the core philosophies behind Rails &#8211; partials are a real good example of this especially when you start to think of them as essentially methods.  On top of the obvious &#8217;speed to deliver&#8217; benefits this concept provides, it also causes you to distill your code down to being more readable and accessible.  This ties in nicely with <a href="http://en.wikipedia.org/wiki/Model-view-controller" target="_blank">MVC</a> principles as you then tend to group like logic together which is what MVC is all about.</p>
<p style="text-align: justify;">Foreign key constraints are business logic as they describe and implement a behavior &#8211; &#8220;when you delete this record, also delete these other records over here&#8221;.  So why would you go and put some &#8216;important to know about&#8217; business logic in an isolated and often hard to reach place?</p>
<p style="text-align: justify;"><strong>Documentation</strong></p>
<p style="text-align: justify;">The one great thing I learned from <a href="http://www.stevemcconnell.com/" target="_blank">Steve McConnell&#8217;s</a> <a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1224199994&amp;sr=1-1" target="_blank">Code Complete</a> was how to write comments, they should describe the intention of the code, rather then repeat the code.  This nicely ties in with the DRY principle.</p>
<p style="text-align: justify;">I&#8217;m also a great believer in self documenting code, Rails and Ruby help heaps with this, and generally write comments to help simplify complex code.  Instead of my code being littered with redundant comments cluttering everything up, it just contains important ones as they are more likely to be read and updated when the code is next worked on.</p>
<p style="text-align: justify;">If you use foreign key constraints in your DB you&#8217;ll likely end up documenting them (ie repeating them) somewhere more accessible, in the models being an obvious place.  That wouldn&#8217;t be because the business logic lives there would it?.  Have a think about that.</p>
<p style="text-align: justify;"><strong>The Rails Way</strong></p>
<p style="text-align: justify;">Looking at the Convention over configuration, Don&#8217;t Repeat Yourself and Model View Controller concepts above &#8211; as much as Rails is a framework it&#8217;s also about a way of doing things.</p>
<p style="text-align: justify;">Over the years I&#8217;ve seen a notable number of people, new to rails and these concepts, trying to make rails work their way &#8211; often with less than desirable results.  If you embrace Rails and it&#8217;s &#8216;way&#8217; then it works beautifully and is a pure joy to use, rather than a frustrating experience.</p>
<p style="text-align: justify;"><strong>For me</strong></p>
<p style="text-align: justify;">Coding is an art form and as such a creative process.  If something doesn&#8217;t look, or even feel right then it&#8217;s wrong.  In the end foreign key constraints on a web app just rub me up the wrong way.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Be careful how you count in Rails</title>
		<link>http://www.motionstandingstill.com/be-careful-how-you-count/2008-09-12/</link>
		<comments>http://www.motionstandingstill.com/be-careful-how-you-count/2008-09-12/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 00:05:23 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[eager loading]]></category>
		<category><![CDATA[named_scope]]></category>
		<category><![CDATA[relationships]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=132</guid>
		<description><![CDATA[I&#8217;ve just encountered this problem yet again effecting code I&#8217;m working though, so I thought I&#8217;d quickly blog about it.
Put basically if you call count on a relationship it will always make a db call to see how many there are even if the relationship has been populated.  If you use size or any? they [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I&#8217;ve just encountered this problem yet again effecting code I&#8217;m working though, so I thought I&#8217;d quickly blog about it.</p>
<p style="text-align: justify;">Put basically if you call <em>count</em> on a relationship it will always make a db call to see how many there are even if the relationship has been populated.  If you use <em>size</em> or <em>any?</em> they will too, unless the relationship is populated, in which case they treat it like an array and leave the DB the heck alone.</p>
<p style="text-align: justify;">For example, when displaying a bunch of users you want nice html when showing if they own any books , so you&#8217;ll probably end up checking user.books.count multiple times per user shown.  This will ask the db for a book count every time you call <em>count</em> and it will do it for all users shown.  Not fun.</p>
<p style="text-align: justify;">Unfortunately, simply using <em>size</em> or <em>any?</em> instead will make no difference unless the relationship is already populated or coincidentally 0 is returned as, for some odd reason, this result is cached by Rails.</p>
<p style="text-align: justify;">Additionally you should eager load if you&#8217;re accessing data within the relationship when rendering the user&#8217;s line  &#8211; you were doing this already ay?  If a &#8216;count&#8217; of books is simply being used rather than book related data then still use <em>size</em> and <em>any?</em> as this may change later down the line and it&#8217;s no extra effort.</p>
<p style="text-align: justify;">Now to throw a spanner in the works, named_scope is different, in fact it behaves the exact opposite. way to  <em>size</em> and <em>any?</em>.  If you call them on a named scope they grab the whole result set (ie all the models), tell you what you want to know and then discard the result set &#8211; EVERY TIME.</p>
<p style="text-align: justify;">Named scopes are not like relationships as they don&#8217;t cache their results, so successive calls to a named scope will hit up the DB each time, ignoring any previous calls made to it.  Ironically, calling <em>count</em> on a named scope makes a &#8216;count(*)&#8217; call to the DB so it&#8217;s much lighter weight than <em>size</em> or <em>any?</em>.</p>
<p style="text-align: justify;">I almost thinking that this behavior is inconsistent and could be changed so that calling <em>count</em>, <em>size</em> and <em>any?</em> on an unpopulated relationship would cache the result for that request as it could actually change during execution potentially causing errors or show something weirdly.</p>
<p style="text-align: justify;">This problem can also occur with caching as cached fragments are usually checked for in actions and then in the view.  The fragment could be expired by a different request in between these two checks causing the view to barf as the action won&#8217;t have set up any data thinking the view won&#8217;t need it.  I&#8217;ve had this unlikely situation happen multiple times and ended up building an in-request cache to combat it.</p>
<p style="text-align: justify;">I feel a plugin, or even attempted patch to Rails welling up from within&#8230;</p>
<p style="text-align: justify;">UPDATE: I forgot all about using <em>length</em>, which muddies the waters further!  <em>Length</em> populates the relationship and then returns it&#8217;s <em>size</em>.  Meaning subsequent calls don&#8217;t talk to the DB or if the relationship was already populated there isn&#8217;t any DB activity either &#8211; just like <em>size</em> and <em>any?</em>.  The big thing to note here though is that it attempts to populate the relationship first which you many not want happening.  Remember eager loading?</p>
<p style="text-align: justify;">With named scopes it behaves the same, but since named_scopes behave differently to relationships there is no caching or populating happening &#8211; so each call involves the database returning all the records and Rails turning them into models.</p>
<p style="text-align: justify;">I&#8217;m still going to be using <em>size</em> and <em>any?</em> as they&#8217;ll do &#8216;count(*)&#8217; calls to the db if the relationship isn&#8217;t populated, otherwise they&#8217;ll just return it&#8217;s array size &#8211; or a boolean in the case of <em>any?</em>.  Meaning a git a bit of code future proof happening &#8211; ie less &#8216;why is that suddenly real slow?&#8217; type situations.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/be-careful-how-you-count/2008-09-12/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.873 seconds -->
