Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use cache_digests with XML builder?

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

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

Is there any way to use cache_digests in this way?

like image 459
Hayk Saakian Avatar asked Mar 30 '13 22:03

Hayk Saakian


1 Answers

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:

# 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
like image 197
Ben Sheldon Avatar answered Oct 22 '22 04:10

Ben Sheldon