简体   繁体   中英

Rails caching: stale? always returns true in development

Following hawkins.io in my person model I have:

def self.cache_key
   Digest::MD5.hexdigest "#{maximum(:updated_at)}.try(:to_i)-#{count}"
end

I am using Pundit for authorization. So in my people controller, I have:

def show
    @person = Person.find(params[:id])
    if authorize @person
      if stale? @person
        @person = Person.basic_details.last_details.find(params[:id]).decorate
        @person_histories = PersonHistory.new(person_id: @person.id).results
        respond_with @person
      end
    end
end

In my development.rb environment:

config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { expires_in: 4.hours }
config.consider_all_requests_local       = true
config.action_controller.perform_caching = true

(I'm on Windows here so don't have memcached etc setup).

When I reload the person show view, immediately after loading it, I would expect it to be fully cached. Yet it requeries the database etc etc. Is there a setting or something I am missing? When I check the cache keys they are the same, but stale? @person stale? @person always appears to return true.

Have you tried this?

Disable Rack::MiniProfiler caching

Rack::MiniProfiler middleware will remove headers related to caching and as such, stale? will ALWAYS RETURN TRUE. We can disable caching altogether using the following initializer:

Rack::MiniProfiler.config.disable_caching = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM