0
  • 红宝石 2.2.2p95
  • 导轨 4.2.1

我的缓存生成运行良好。

索引页

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

分页页面

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

但是我很难让它们过期。我正在使用清扫器使我的动作缓存过期,但我还没有弄清楚如何使分页页面过期。

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

如何使分页页面过期

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

等等?

4

1 回答 1

1

缓存过期很难

如果你真的想担心缓存过期,你可以使用这样的解决方案:分页和页面缓存清理器

在 Rails 4 中处理缓存过期的推荐方法是使用缓存键,而不用担心缓存过期: 基于键的缓存过期工作原理

您可以将Product.maximum(:updated_at)其用作所有产品索引和分页页面的缓存键的一部分。当其中一种产品更新时,更改所有产品索引页面的键可能比尝试猜测哪些页面会受到更改影响要好。

如果您允许用户更改每页的记录数,那么这也需要成为缓存键的一部分。

于 2015-06-30T06:42:41.287 回答