Forum: Ruby on Rails xml builder cache (with a slight problem on initial load)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
12401aebb142896b15d176b1ea34b07c?d=identicon&s=25 jtarchie (Guest)
on 2007-07-19 21:20
(Received via mailing list)
Let me give you the background. I am using rxml for a lot of my views
for a rails project. I also noticed that there is a lot of the XML
being dynamically generated on data that doesn't change that often. I
worked on trying to cache the xml fragment that I needed but came to
the quick realization that cache only handle ERB/RHTML files. Needless
to say, I have spent some time creating an XML fragment cache for rxml

A slight problem with it though.

Let me present the code. It is derived directly from the cache and
cache_erb_fragment routines.

<code>module ActionView
  module Helpers
    module CacheHelper
      def cache_xml(xml, name ={}, &block)
        @controller.cache_xml_fragment(block, xml, name)

module ActionController
  module Caching
    module Fragments
      def cache_xml_fragment(block, xml, name = {}, options = nil)
        unless perform_caching then; return end
        if cache = read_fragment(name, options)
          xml << cache
          buffer = ''
          xml_builder =>buffer)

          write_fragment(name,buffer, options)
          xml << buffer
To use the cache_xml you pass the current xml object and the 'name' of
the fragment. This is a simplified example (w/o the dynamic loading of
data) but the problem that occurs still happens.
<code>directors = ['George Lukas','Steven Speilberg','Michael Bay']
xml.directors do
  directors.each do |name|
    cache_xml(xml,:action=>'director',:name=>name) do
      xml.director :name=>name
On the initial load of the RXML file I can indeed see that each cache
fragment is being created  with a 'Cached fragment' line on my dev

This is the rendering of the initial XML:
   <director name="George Lukas"/>
This is the expected output
   <director name="George Lukas"/>
   <director name="Steven Speilberg"/>
   <director name="Michael Bay"/>
On a second request of the XML, everything renders correctly, but I am
trying to figure out why it doesn't happen on the initial request.

I believe the problem occurs with the block call within
cache_xml_fragment, but I have no idea what is causing the problem.

If you have any questions please feel free to ask.
This topic is locked and can not be replied to.