我正在尝试使用 Memcached 插件与 Rails 3.1 和 Heroku cedar 堆栈上的 dalli 来设置一个缓存系统,其中有许多项目应该在每天结束时过期。
为此,我使用了实用方法:
def self.seconds_to_next_day
((DateTime.now.end_of_day - DateTime.now) * 24 * 60 * 60).to_i.seconds
end
在 haml View 中像这样缓存:
- cache "user_#{current_user.id}_my_groups", :expires_in => Utils.seconds_to_next_day do
= render "shared/my_groups"
但它并没有像应该的那样在每天结束时过期。我也尝试过以整数形式传递秒数,不带 .seconds,但这没有什么区别。
目前,我已经退回到每天 00:00 运行的计划任务并清除对日期敏感的缓存项目,但我更愿意使用 native 缓存过期。
有什么想法吗?谢谢!
最佳答案
为了在 Rails 和 Heroku 上正确计算时间,您需要正确使用时间和区域。因此,您想要的是以下内容。
Time.zone.now.end_of_day - Time.zone.now
关于ruby-on-rails - 内存缓存 :expires_in on Heroku for fragment caching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10458723/