Rails caching with expires_in lambda

When expires_in gets complicated you can simply use
:expires_in => lambda{ Time.now.to_i % 100 }

Code

class ActionController::Base
  def write_fragment_with_lambda_expire(key, content, options = nil)
    if options.is_a? Hash and options[:expires_in].is_a? Proc
      options = options.merge(:expires_in  => options[:expires_in].call)
    end
    write_fragment_without_lambda_expire(key, content, options)
  end

  alias_method_chain :write_fragment, :lambda_expire
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s