使用cache_digests与XML构建器?

时间:2021-05-19 03:49:57

Is there any way to leverage the new caching strategy in Rails 4 (cache_digests) for XML?

有没有办法在Rails 4(cache_digests)中利用XML的新缓存策略?

I suppose I could use xml.erb views, but I prefer xml.builder views for their terseness.

我想我可以使用xml.erb视图,但我更喜欢xml.builder视图的简洁性。

Is there any way to use cache_digests in this way?

有没有办法以这种方式使用cache_digests?

1 个解决方案

#1


7  

To use fragment caching and Rails 4 cache_digests in XML Builder files, just use the cache method, which works exactly like in other templates. Here is an example of russian doll caching of a (simplified) blog RSS feed:

要在XML Builder文件中使用片段缓存和Rails 4 cache_digests,只需使用缓存方法,该方法与其他模板完全相同。以下是俄罗斯娃娃缓存(简化)博客RSS提要的示例:

# feed.xml.builder

xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0", 'xmlns:atom': 'http://www.w3.org/2005/Atom' do
  xml.channel do
    xml.title "My Blog"

    cache "articles/feed-#{@articles.count}-#{@articles.maximum(:updated_at).try(:to_i)}" do
      @articles.each do |article|
        cache article do
          xml.item do
            xml.title article.title
            xml.description article.body
          end
        end
      end
    end
  end
end

#1


7  

To use fragment caching and Rails 4 cache_digests in XML Builder files, just use the cache method, which works exactly like in other templates. Here is an example of russian doll caching of a (simplified) blog RSS feed:

要在XML Builder文件中使用片段缓存和Rails 4 cache_digests,只需使用缓存方法,该方法与其他模板完全相同。以下是俄罗斯娃娃缓存(简化)博客RSS提要的示例:

# feed.xml.builder

xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0", 'xmlns:atom': 'http://www.w3.org/2005/Atom' do
  xml.channel do
    xml.title "My Blog"

    cache "articles/feed-#{@articles.count}-#{@articles.maximum(:updated_at).try(:to_i)}" do
      @articles.each do |article|
        cache article do
          xml.item do
            xml.title article.title
            xml.description article.body
          end
        end
      end
    end
  end
end