The caching action does not end correctly even when I see that it is called

I have a sweeper that must expire with several action caches. Although the debugger stops immediately before calling expire_action, it does not actually expire. Any idea what could happen?

Here is the appropriate sweeper and controller.

# company_sweeper.rb (in the "models" directory)

class CompanySweeper < ActionController::Caching::Sweeper observe Company def after_save(company) expire_cache(company) if company.final_save && company.valid? end def expire_cache(company) debugger <= #debugger stops here! right before the call I'm trying to make. expire_action :controller => 'reports', :action => 'full_report' end end 

# reports_controller.rb

 class ReportsController < ApplicationController layout false caches_action :full_report, :supplier_list, :service_categories cache_sweeper :company_sweeper def full_report #do stuff... end end 

I know that this does not expire, so that the full report returns the old data and responds almost instantly. Strange, right?

+4
source share
2 answers

Do you have a cache_sweeper ad in your CompanyController? The sweeper should be included in the controller, which performs life cycle actions on the model in question. If you are not doing anything with company instances in ReportController, the cache_sweeper line cache_sweeper not belong there.

Action caching includes an implicit host name. If two hits fall on different host names, caching is performed under one and expiration under another.

+5
source

I don't think there are enough details here to really answer your question, but here are a few questions:

The sweeper should start regardless of the full_report action, so if you make changes to the company, you should see a debugger fire (which looks like this is happening correctly). Then you do not need to run the full_report action, so at this point you can verify that the cached file has been deleted. It would be useful to go through expire_action in the debugger to find out if the rails are missing for another reason.


EDIT: oh, you know that, I was just splitting into this, and it looks like expire_action is expected to work in the controller context (I read the source of the gems in the actionpack). It assumes that the "I" is the controller, so your pass in the option: the controller is ignored.

Other examples provide a specific string instead of parameters (for example, expire_action '/reports/full_report' ). I don’t like it personally - it’s not using a router --- but it seems like it will work.

Perhaps you should switch to this method, make sure that it works, and then in the debugger see if you have access to url_for. it can be as simple as expire_action url_for(:controller => 'reports', :action => 'full_report')

0
source

Source: https://habr.com/ru/post/1301040/


All Articles