Interlock A Rails plugin for maintainable and high-efficiency caching. == License Copyright 2007, 2008 Cloudburst, LLC. Licensed under the AFL 3. See the included LICENSE file. Portions copyright 2006 Chris Wanstrath and used with permission. The public certificate for the gem is here[http://rubyforge.org/frs/download.php/25331/evan_weaver-original-public_cert.pem]. If you use this software, please {make a donation}[http://blog.evanweaver.com/donate/], or {recommend Evan}[http://www.workingwithrails.com/person/7739-evan-weaver] at Working with Rails. == Requirements * memcached (http://www.danga.com/memcached) * memcache-client gem * Rails 1.2.6 Note that Rails 2.0.2 is required for caching content_for or nesting view_cache blocks. == Features Interlock is an intelligent fragment cache for Rails. It works by making your view fragments and associated controller blocks march along together. If a fragment is fresh, the controller behavior won't run. This eliminates duplicate effort from your request cycle. Your controller blocks run so infrequently that you can use regular ActiveRecord finders and not worry about object caching at all. Invalidations are automatically tracked based on the model lifecyle, and you can scope any block to an arbitrary level. Interlock also caches content_for calls, unlike regular Rails, and can optionally cache simple finders. Interlock uses a tiered caching layer so that multiple lookups of a key only hit memcached once per request. == Installation First, compile and install memcached itself. Get a memcached server running. You also need either memcache-client or {memcached}[http://blog.evanweaver.com/files/doc/fauna/memcached]: sudo gem install memcache-client Then, install the plugin: script/plugin install -x svn://rubyforge.org/var/svn/fauna/interlock/trunk Lastly, configure your Rails app for memcached by creating a config/memcached.yml file. The format is compatible with Cache_fu: defaults: namespace: myapp sessions: false client: memcache-client development: servers: - 127.0.0.1:11211 # Default host and port production: servers - 10.12.128.1 - 10.12.128.2 Now you're ready to go. Note that if you have {memcached 0.7}[http://blog.evanweaver.com/files/doc/fauna/memcached], you can use client: memcached for better performance. == Usage Interlock provides two similar caching methods: behavior_cache for controllers and view_cache for views. They both accept an optional list or hash of model dependencies, and an optional :tag keypair. view_cache also accepts a :ttl keypair. The simplest usage doesn't require any parameters. In the controller: class ItemsController < ActionController::Base def slow_action behavior_cache do @items = Item.find(:all, :conditions => "be slow") end end end Now, in the view, wrap the largest section of ERB you can find that uses data from @items in a view_cache block. No other part of the view can refer to @items, because @items won't get set unless the cache is stale. <% @title = "My Sweet Items" %> <% view_cache do %> <% @items.each do |item| %>