To use fragment caching and Rails 4 cache_digests in XML Builder files, simply use a method cachethat works exactly the same as in other templates. Here is an example of caching a Russian doll (simplified) RSS feed blog:
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
source
share