[Catalyst-commits] r13707 - trunk/examples/CatalystAdvent/root/2010/pen

skaufman at dev.catalyst.perl.org skaufman at dev.catalyst.perl.org
Mon Nov 22 16:11:37 GMT 2010


Author: skaufman
Date: 2010-11-22 16:11:37 +0000 (Mon, 22 Nov 2010)
New Revision: 13707

Added:
   trunk/examples/CatalystAdvent/root/2010/pen/memcached-dbix-class.html
Log:
First draft of "Painless Memcached Configuration With Catalyst & DBIx::Class, originally published here: http://blogs.perl.org/users/samuel_kaufman/2010/11/painless-memcached-configuration-with-catalyst-dbixclass.html


Added: trunk/examples/CatalystAdvent/root/2010/pen/memcached-dbix-class.html
===================================================================
--- trunk/examples/CatalystAdvent/root/2010/pen/memcached-dbix-class.html	                        (rev 0)
+++ trunk/examples/CatalystAdvent/root/2010/pen/memcached-dbix-class.html	2010-11-22 16:11:37 UTC (rev 13707)
@@ -0,0 +1,103 @@
+This tutorial will show you how to:
+<ul>
+    <li>Easily add resultset level caching to your <a href="http://search.cpan.org/perldoc?Catalyst%3A%3AModel%3A%3ADBIC%3A%3ASchema">Catalyst::Model::DBIC::Schema</a> calls.</li>
+    <li>Move your <a href="http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3ASession">Catalyst::Plugin::Session</a> into memcached.</li>
+    <li>Have a convenient $ctx-&gt;cache method available for anything else you might want to cache.</li>
+</ul>
+<strong>Dependencies:</strong><br/>
+Memcached:
+<ul>
+<li><a href="http://search.cpan.org/perldoc?Cache%3A%3AMemcached">Cache::Memcached</a></li>
+<li><a href="http://search.cpan.org/perldoc?Cache%3A%3AMemcached%3A%3AGetParserXS">Cache::Memcached::GetParserXS</a></li>
+</ul>
+Catalyst Plugins:
+<ul>
+<li><a href="http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3AConfigLoader">Catalyst::Plugin::ConfigLoader</a></li>
+<li><a href="http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3ASession">Catalyst::Plugin::Session</a></li>
+<li><a href="http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3ACache">Catalyst::Plugin::Cache</a></li>
+<li><a href="http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3ASession%3A%3AStore%3A%3ACache">Catalyst::Plugin::Session::Store::Cache</a></li>
+</ul>
+DBIx::Class:
+<ul>
+<li><a href="http://search.cpan.org/perldoc?DBIx%3A%3AClass%3A%3ACursor%3A%3ACached">DBIx::Class::Cursor::Cached</a></li>
+</ul>
+
+<p>So dump all those in your Makefile.PL and you're halfway there.</p>
+<p>First we edit your Catalyst app's base package. Open up your version of MyApp.pm and add:</p>
+<p>
+<pre>
+
+    use Cache::Memcached::GetParserXS;
+    use Cache::Memcached;
+
+</pre>
+</p>
+<p>This will tell Cache::Memcached to use the XS Parser.</p>
+<p>
+Now, in the section where you load your plugins, add the new ones in:
+<pre>
+
+    use Catalyst qw/
+        ConfigLoader
+        Session
+        Cache
+        Session::Store::Cache
+    /;
+
+</pre>
+</p>
+<br/>
+</p>
+
+<p>Now, configure Catalyst::Plugin::Cache. Here's an example for etc/myapp_local.pl:</p>
+<br/>
+<pre>
+
+    #!/usr/bin/env perl
+    use strict;
+    use warnings;
+    return {
+        'Plugin::Cache' =&gt; {
+            backend =&gt; {
+                namespace           =&gt;  'myapp:',
+                class               =&gt;  'Cache::Memcached',
+                servers             =&gt; [ 'dev:11211' ]
+            }
+        }
+    };
+
+</pre>
+<br/>
+<p>Note, I didn't use a .pl config just for kicks. Notice how the 'servers' param takes an ArrayRef value. I tried and failed in expressing that in our Apache Conf style config, before realizing that ConfigLoader is just as happy to grab your .pl config alongside your regular config and merge them for you. Sometimes a cop-out is better than a hack.</p> 
+<br/>
+<p>Now we configure our model. In our apache style conf it would look like this:</p>
+<pre>
+
+    &lt;Model::MyAppDB&gt;
+        schema_class    MyApp::Schema
+        &lt;connect_info&gt;
+            (your connect_info)
+            cursor_class    DBIx::Class::Cursor::Cached
+            traits          Caching
+        &lt;/connect_info&gt;
+    &lt;/Model::MyAppDB&gt;
+
+</pre>
+
+<p>Pat yourself on the back, you should be done (unless something went horribly wrong).</p>
+<p>
+<pre>
+
+    my @sweet_loot = $ctx-&gt;model("MyAppDB::Loot")-&gt;search({ sweet =&gt; 1 },{ cache_for =&gt; 300 })-&gt;all;
+
+</pre>
+<p>That $rs is now cached for 300 seconds. Look at <a href="http://search.cpan.org/perldoc?DBIx%3A%3AClass%3A%3ACursor%3A%3ACached">DBIx::Class::Cursor::Cached</a> for further explanation.
+
+<pre>
+
+    my $cache = $ctx-&gt;cache;
+    $cache-&gt;set('turtles',{ ilike =&gt; 'turtles' },600);
+    my $do_i_like_turtles = $cache-&gt;get('turtles');
+
+</pre>
+<p>That's cached for 600 seconds. See <a href="http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3ACache">Catalyst::Plugin::Cache</a> for the docs.</p>


Property changes on: trunk/examples/CatalystAdvent/root/2010/pen/memcached-dbix-class.html
___________________________________________________________________
Added: svn:mergeinfo
   + 




More information about the Catalyst-commits mailing list