timed_fragment_cache on Rails 2.1

posted in: cache, fragment caching, rails, ruby | 0

With Rails 2.1 cache_erb_fragment has been replaced by write_fragment. This breaks a plugin I really enjoy to use: timed_fragment_cache by Richard Livsey.

It basically allows you to specify an expiry for the fragments you cache, e.g. inside memcache, by adding a “meta” fragment for the fragment you store containing the expiry time.

Some usage examples – pretty self-explaining:

In your view:

<%- cache 'my_key', 1.hour.from_now do -%>
<%- end -%>

With extensive queries – your view …:

<%- cache 'some_posts' -%>
	<%- @posts.each do |post| -%>
		<%= @post.title %>
	<%- end -%>
<%- end -%>

… and your controller:

when_fragment_expired 'some_posts', 1.hour.from_now do
	@posts = Post.find( :all )

Since timed_fragment_cache is so convient to use and essentially because I simply cannot live without it, I’ve fixed Richards version to work with Rails 2.1, i.e. let it use write_fragment instead of cache_erb_fragment.

Download version 0.2.