ahawkins / cashier

Tag based caching for Rails using Redis. Associate different cached content with a tag, then expire by tag instead of key.
MIT License
162 stars 39 forks source link

Tag as a proc in caches_action etc won't get called #14

Open ScotterC opened 11 years ago

ScotterC commented 11 years ago

Trying to set a tag in a controller action with a proc won't work

caches_action :tag => proc {|c|
  # c is the controller
  "users/#{c.current_user.id}/dashboard"      
}

There's no code within action pack (Rails 3.2.9) that will evaluate a proc for tag. It just gets past along as a store_option which you can see here. There also doesn't seem to be a way to evaluate the proc later on in the cashier code because you don't have the controller to execute it from.

In previous versions of Rails did all procs passed to caches_action get evaluated somewhere?

ScotterC commented 11 years ago

Here's a way around it in your controller:

  caches_action :show, cache_path: :show_cache_path.to_proc

  after_filter :cache_tags

  def show_cache_path
     params
  end

  def cache_tags
    fragment = fragment_cache_key(show_cache_path)
    tag = ["post_#{params[:id]}"]

    Cashier.adapter.store_fragment_in_tag(fragment, tag)
    Cashier.adapter.store_tags(tag)
  end
ahawkins commented 11 years ago

In previous versions of Rails did all procs passed to caches_action get evaluated somewhere?

EDIT: Yes the did. I'm not sure how to handle this at this point. I really don't like injecting code into the controller.

I think the only way to get this working is include a module into ApplicationController. What version of rails are you on?

ScotterC commented 11 years ago

I'm using Rails 3.2.9. I looked over action controller quite a bit yesterday, it's being removed from rails core as you probably know, so that doesn't feel like the best way to solve it.

A module in the ApplicationController could work. Some way to listen for the proc and call it before passing it on to Cashier?

ahawkins commented 11 years ago

Hmmm. Ya probably. I'll look into it. On Dec 14, 2012 4:37 PM, "Scott Carleton" notifications@github.com wrote:

I'm using Rails 3.2.9. I looked over action controller quite a bit yesterday, it's being removed from rails core as you probably know, so that doesn't feel like the best way to solve it.

A module in the ApplicationController could work. Some way to listen for the proc and call it before passing it on to Cashier?

— Reply to this email directly or view it on GitHubhttps://github.com/twinturbo/cashier/issues/14#issuecomment-11380294.

namxam commented 9 years ago

Any updates? We are currently having the same issues in a project and it wouldbe awesome if this could get merged

ScotterC commented 9 years ago

@namxam although I have no intention of maintaining a fork, I believe I fixed this in my fork if you'd like to use it: https://github.com/scotterc/cashier/tree/tag-cached-pages