0
  • ruby 2.2.2p95
  • rails 4.2.1

My cache generation is working nincely.

Index Page

http://localhost:3001/produtos
Write fragment views/localhost:3001/produtos (11.0ms)

Pagination Page

http://localhost:3001/produtos/pagina/2
Write fragment views/localhost:3001/produtos/pagina/2 (15.3ms)

But I'm in troubles to expiring them. I'm using a sweeper to expire my action caches and I have not figured out how to expire pagination pages.

class ProductsController < ApplicationControlle
   caches_action [:show, :index]
end


class Admin::ProductsController < Admin::BaseController
    cache_sweeper :product_sweeper
end

class ProductSweeper < ActionController::Caching::Sweeper
    observe Product
    def after_save(product)
        expire_action products_url
        expire_action category_product_url(category_id: product.category.slug)
    end
end

How to expire pagination pages

http://localhost:3001/produtos/pagina/2
http://localhost:3001/produtos/pagina/3
http://localhost:3001/produtos/pagina/4
...

and so on?

maiconsanson
  • 243
  • 3
  • 16

1 Answers1

1

Cache expiring is hard

If you really want to worry about expiring the cache, you can use a solution like this: Pagination and page cache sweepers

The recommended way to deal w/ cache expiration in Rails 4 is to use cache keys and not worry about expiring the cache: How key-based cache expiration works

You can use Product.maximum(:updated_at) as part of the cache key for all product index and pagination pages. It's probably better to change the key for all the product index pages when one of the products is updated than to try and guess which pages will be affected by the change.

If you allow the user to change the number of records per page then that needs to be part of the cache key as well.

Josh
  • 8,329
  • 4
  • 36
  • 33
  • The first link is using page_caching (I'm using action_caching) which has a different way to organize cached files. I cannot remove files that way (`"#{Rails.root}/public/articles/page/#{entry}"`). Fragment caching is perfect for fragments and expiration, of course, but it would be perfect if I could cache/expire the entire page. I can't use page_caching too because this approach does not work on Heroku. – maiconsanson Jun 30 '15 at 13:33