Expire action cache with custom cache_path
Asked Answered
M

2

8

I'm having some problem expiring an action cache in my application.

Here is my controller:

class ToplistsController < ApplicationController
  caches_action :songs, cache_path: :custom_cache_path.to_proc

  def custom_cache_path
    "#{params[:when]}-#{params[:what]}-#{params[:controller]}-#{params[:action]}"
  end

  def songs
    # ...
  end  
end

I somehow need to be able to reset the custom cache path, but I can't figure out how.

I've already tried using this technique, without success. It looks like Dalli, which is my cache engine, doesn't support regexp matcher.

I'm getting this error when trying to use this code:

expire_fragment(/songs/)

ActiveSupport::Cache::DalliStore does not support delete_matched

I've tried to use this line of code, for debugging, but it's being ignored.

before_filter only: [:songs] 
  expire_fragment(custom_cache_path)
end

I'm using Rails 3.1.0.rc6, Dalli 1.0.5 and Ruby 1.9.2.

Mcnair answered 22/8, 2011 at 9:46 Comment(1)
You you use a regex with dallicache with this gem: github.com/defconomicron/dalli-store-extensionsVictoir
M
0

The before_filter block was ignored duo to the action cache.
The solution is to use a fragment cache instead.

# Controller
class ToplistsController < ApplicationController
  helper_method :custom_cache_path

  before_filter only: [:songs]
    if params[:reset_cache]
      expire_fragment(custom_cache_path)
    end
  end

  def custom_cache_path
    "#{params[:when]}-#{params[:what]}-#{params[:controller]}-#{params[:action]}"
  end

  def songs
    # ...
  end  
end

# View

<%= cache custom_cache_path do %>
  Content that should be cached
<% end %>
Mcnair answered 31/8, 2011 at 9:55 Comment(0)
B
0

You may also want to check out the solution here. With his approach you can expire actions with extra parameters.

Bertrambertrand answered 6/2, 2012 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.