如何在Rails 3.1中获取片段到期日期? [英] How to get fragment expiration date in Rails 3.1?

查看:53
本文介绍了如何在Rails 3.1中获取片段到期日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取缓存条目的过期时间或创建时间。



这是针对:站点地图索引,索引中的每个站点地图都是缓存操作。我想添加
< lastmod> 属性,在我的情况下,这将是缓存条目创建时间。



例如动作缓存:

  class ProductsController< ActionController 
caches_action:index

def index
@products = Product.all
end
end

我需要这样的东西:

  Rails.cache .get(:controller =>'products',:action =>'index')。created_at 


解决方案

我找到了一种解决方案,该解决方案可与所有流行的缓存存储(已通过redis_store,mem_cache_store,memory_store,file_store测试)一起使用。 $ p> Time.at(Rails.cache.send(:read_entry,'cache / entry / key',{}))。created_at)

read_entry 方法返回 ActiveSupport :: Cache :: Entry 对象(类文档


I need to get expire or creation time for cache entry.

This is for: sitemap index, each sitemap in index is cached action. I want to add <lastmod> attribute, which, in my case, will be cache entry creation time.

For example for action caching:

class ProductsController < ActionController
  caches_action :index

  def index
    @products = Product.all
  end
end

I need something like this:

Rails.cache.get(:controller=>'products',:action=>'index').created_at

解决方案

I found solution, which work with all popular cache stores (tested with redis_store, mem_cache_store, memory_store, file_store).

Time.at(Rails.cache.send(:read_entry,'cache/entry/key',{})).created_at)

read_entry method returns ActiveSupport::Cache::Entry object (class documentation)

这篇关于如何在Rails 3.1中获取片段到期日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆