【问题标题】:expire_action for pagination pages分页页面的 expire_action
【发布时间】:2015-09-16 12:05:16
【问题描述】:
  • 红宝石 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
...

等等?

【问题讨论】:

    标签: ruby-on-rails ruby caching pagination cache-expiration


    【解决方案1】:

    缓存过期是困难

    如果你真的想担心缓存过期,你可以使用这样的解决方案:Pagination and page cache sweepers

    在 Rails 4 中处理缓存过期的推荐方法是使用缓存键,而不用担心缓存过期: How key-based cache expiration works

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

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

    【讨论】:

    • 第一个链接是使用 page_caching(我正在使用 action_caching),它有不同的方式来组织缓存文件。我无法以这种方式删除文件 ("#{Rails.root}/public/articles/page/#{entry}")。当然,片段缓存非常适合片段和过期,但如果我可以缓存/过期整个页面,那将是完美的。我也不能使用 page_caching,因为这种方法不适用于 Heroku。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2021-12-02
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多