Rails caching with expires_in lambda

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


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)
    write_fragment_without_lambda_expire(key, content, options)

  alias_method_chain :write_fragment, :lambda_expire

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 )

Connecting to %s