Dynamic File Store plugin
Plugin details
Documentation
ruby script/plugin install http://dynamic-file-store.googlecode.com/svn/dynamic_file_store/
== Usage
In your environment.rb, you can specify the absolute maximum amount of time that fragments shall live (be valid) in your application.
If you never realistically want to keep fragments cached for longer than, an hour, for example (in your environment.rb):
cache_path = File.join(RAILS_ROOT, 'tmp', 'cache') ActionController::Base.fragment_cache_store = DynamicFileStore.new(cache_path, :mtime => 1.hour)
To mimic default rails fragment caching behavior, set the :mtime to a year or longer:
ActionController::Base.fragment_cache_store = DynamicFileStore.new(cache_path, :mtime => 1.year)
Let's say you've got a Tag Cloud page that you'd only like to render once per day.
Here, DynamicFileStore works best by violating MVC by placing controller logic (only that necessary to generate your tag cloud) in the actual partial whose entire contents will be cached.
In your application.rb:
class ApplicationController < ActionController::Base protected def cached_tag_cloud fragment = read_fragment_or_cache :key => 'tag_cloud', :expire_at => 1.day.ago, :render => {:partial => 'tags/tag_cloud'} return fragment end helper_method :cached_tag_cloud end
Then in tags/tags_controller.rb:
class TagsController < ApplicationController def tag_cloud @newly_tagged_items = Item.find(:all, :limit => 5, :order => 'created_at DESC') end end
Something like this in tags/tag_cloud.rhtml:
div Here we can wrap dynamic, fresh content around our tag cloud. /div div<% # render the newly tagged items... %> /div h2Tag Cloud /h2 div<%= cached_tag_cloud %> /div ...
And then our tag cloud would be in tags/_tag_cloud.rhtml:
<% @tags = Tag.find(:all) %> ... render the tag cloud (computationally expensive) ...
Thus, the first time a user lands on '/tags/tag_cloud', then the 'tags/_tag_cloud.rhtml' partial will be rendered. This makes several expensive database calls & rendering operations.
Upon subsequent loads (1 day since it was cached, as defined in application.rb on the line: ":expire_at => 1.day.ago"), the partial 'tags/_tag_cloud.rhtml' will no longer be called.
This comes in handy when you have a page with 1-2 database requests for the main content, but a lot more little sidebar partials with things like recent comments, popular items, etc.
== Development Environment Note
By default, rails does not enable caching in development mode. Place this in your environment.rb to enable caching for all environments:
ActionController::Base.perform_caching = true
Further Documentation
There is currently no advanced documentation for this plugin.
New documentationEdit plugin | (0 older versions) | Last edited by: scott, 3 months ago

