What's new? | Help | Directory | Sign in
Google
dynamic-file-store
Dynamically expire fragments in Ruby on Rails
  
  
  
  
    
Join project
Project owners:
  shantibraford

This plugin no longer works with Rails 2.1 and has subsequently been discontinued.

As an alternative, try placing this in application.rb:

  helper_method :read_fragment_or_cache
  def read_fragment_or_cache(opts)
    #puts "read_fragment_or_cahce called. opts = #{opts.inspect}"
    # Check to see if key has expired
    return nil unless opts[:key]
    key = opts[:key]
    expire_at = opts[:expire_at] || Time.now
    fragment = Rails.cache.read(key, :raw => true)
    #fragment = read_fragment(key, {:expire_at => expire_at})
    if fragment.nil?
      puts "\n\n Fragment NOT FOUND for key:  #{key}\n"
      # Render whatever should be rendered by the render options to a string for caching.
      fragment = render_to_string(opts[:render])
      Rails.cache.write(key, fragment, :raw => true)
      #result = write_fragment(opts[:key], fragment)
    else
      puts "\n\n Fragment WAS INDEED found for key: #{key}\n"
    end
    return fragment
  end

Then you can define cache helper methods (in application.rb, for example) like:

  helper_method :cached_popular_this_week
  def cached_popular_this_week
    fragment = read_fragment_or_cache :key => "home/popular_this_week",
                                      :expire_at => 1.hour.ago,
                                      :render => {:partial => "shared/popular_this_week"}
    return fragment
  end

And simply call them in your views with:

<%= cached_popular_this_week %>

There might be better ways to do all this. Hopefully this is of some help though if you are migrating to Rails 2.1 and beyond.