The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: Pluggable Controller Caching

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: Pluggable Controller Caching Posted: Dec 19, 2007 5:20 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rwdaigle.
Original Post: What's New in Edge Rails: Pluggable Controller Caching
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

It appears that Rails controller will no longer be limited to file-based page and fragment caching. There is a lot of work being done in the 2-1-caching branch of Rails that will let you specify your preferred caching engine in the config file. To date there are the following options:

1
2
3
4
ActionController::Base.cache_store = :memory_store
ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
ActionController::Base.cache_store = :mem_cache_store, "localhost"

If none is specified, then ActiveSupport::Cache::MemoryStore will be used.

Write Your Own Controller Cache

Interested in writing your own controller caching mechanism? Here are some basic steps:

1. Implement the Cache Class

The easiest way to write your own cache class is to subclass ActiveSupport::Cache::Store and implement the read, write, delete and delete_matched methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module ActiveSupport
  module Cache

    # Pluck and store stuff in the ether.
    # ('Ether' is fictitious - ignore its implementation)
    class EtherStore < Store

      def initialize(location='//myloc')
        @ether = Ether.new(location)
      end

      def read(name, options = nil)
        super
        @ether.get(name)
      end

      def write(name, value, options = nil)
        super
        @ether.store(name, value)
      end

      def delete(name, options = nil)
        super
        @ether.remove(name)
      end
        
      def delete_matched(matcher, options = nil)
        super
        @ether.remove_by_pattern(matcher)
      end
    end
  end
end
A few notes:
  • Invoking super for each method call ensures that the proper messages get logged for each action
  • If the underlying cache mechanism doesn’t support a specific operation (such as delete_matched), have your implementation just raise an exception: raise "delete_matched not supported by Mechanism"
  • The options argument passed to each method is taken directly from the various top-level cache methods which now accept an options hash (you now have a method to pass in options applicable to your caching mechanism). I.e.in your view when fragment caching:
1
2
3
<% cache('name', :expires_in => 60) do %>
  <%= render :partial => "menu" %>
<% end %>
2. Plug-in Cache

Once you have your implementation (and it’s tested, of course), plug it into your app with a simple one-liner in environment.rb:

1
2
# Or EtherStore.new('//etherloc')
ActionController::Base.cache_store = :ether_store, '//etherloc'

There you go, that’s all there is to it.

I’m not sure what the intent is for this functionality, but as of now it only exists in the 2-1-caching branch of rails, I will update this post when it makes it to edge.

tags: ruby, rubyonrails

Read: What's New in Edge Rails: Pluggable Controller Caching

Topic: Debugging Ruby with the 'Cylon' Debugger Previous Topic   Next Topic Topic: BarCamp Follow-up Follow-up: Ignite Phoenix

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use