Ticket #5 (new enhancement)

Opened 2 years ago

Last modified 16 months ago

Rails fragment caching for Markaby!

Reported by: murphy@… Owned by: tec
Priority: critical Milestone: 1.0
Component: lib Version: 0.5
Keywords: caching cache rails Cc:

Description

I've found out how to get caching working. So proud.

Reason

Caching Markaby is interesting for speeding up applications. By caching the static header of my site, I got 30% more req/sec.

The new cache method has the same interface as the standard one for rhtml templates. It only links to Rails' caching system.

Example

cache :controller => 'main', :action => 'logo' do
  td.logo do
    link_to capture { image_tag(logo, :id => 'logo') }, home_url
  end
end

What's changed

The file attached (lib/markaby/cache.rb) adds a Builder#cache method that calls a variant of cache_erb_fragment, namely cache_markaby_fragment.

It should be required by the init.rb. Without Rails, it is not loadable.

Compatibility

It is working with 0.4 and with the xhtml-careful branch as well.

Tests & Docs

Code is commented, but I didn't add a test case - difficult to do without Rails in the package.

Problems

Calling cache without Rails would produce an unwanted <cache> tag. You could add a cache method then that ignores its arguments and calls the block.

Attachments

cache.rb (0.8 kB) - added by murphy@… 2 years ago.
Caching for Markaby

Change History

Changed 2 years ago by murphy@…

Caching for Markaby

Changed 2 years ago by why

  • owner changed from somebody to why
  • status changed from new to assigned

This is beautiful! Thankyou!

Changed 2 years ago by why

  • milestone set to 0.5

Changed 2 years ago by why

  • version set to 0.5
  • milestone changed from 0.5 to 1.0

Changed 22 months ago by tec

  • owner changed from why to tec
  • status changed from assigned to new

Changed 21 months ago by joeruby

I tried copying the code to "markaby/cache.rb" and adding "require 'markaby/cache'" to markaby.rb, but caching doesn't appear to work. The cache fragments are either blank, or contain what appears to be item IDs.

Changed 21 months ago by joeruby

  • keywords caching added; cachin removed

Changed 16 months ago by tsg21

This works perfectly for me. Can it be merged into the trunk?

Changed 16 months ago by tsg21

Okay, I lied. It doesn't work if the first Markaby tag method you call has CssProxy? style calls after it:

cache "blah" do
  div.someclass.someid {
    p "some text"
  }
end

This will put the id of the CssProxy? into the cache, and not the content.

to sort it you need to tweak cache.rb so it calls capture on the builder, and not the block directly. I think it's an instance_eval problem.

def cache_markaby_fragment(block, name = {}, options = nil)
    if perform_caching
      # check if already cached, otherwise calculate and write
      cache = read_fragment(name, options) ||
        write_fragment(name, options[:builder].capture(&block), options)
    else # don't cache
      cache = options[:builder].capture(&block)
    end
    options[:builder] << cache
  end
Note: See TracTickets for help on using tickets.