[ANN] merb_piece_cache 0.1.0 released

M

makoto kuwata

I releaded merb_piece_cache 0.1.0.
http://github.com/kwatch/merb_piece_cache


About
-----

merb_piece_cache is a Merb plug-in to cache html fragments.
You can eliminate database access with this plugin.

Currently only file-base cache store is provided.
(Memory-base cache store may be provided in future.)


Install
-------

### install gem
$ sudo gem install merb_helpers_monkey
## add 'dependency "merb_piece_cache"'
$ vi config/dependencies.rb

This plugin requires 'called_from' library.
If you install this plugin manually, don't forget to install
'called_from'.


Example
-------

* [controller] delay database access by if_not_cached() helper.
* [template] specify cache area by cache_with() helper.


controller:

def index
## @user is necessary every time
@user = User.current()
## @members is necessary only when cache is refreshed
## (block parameter is called only when cache is expired or not
found)
if_not_cached {|cache_key|
@members = Members.all:)order=>[:created_at])
}
render
end


template:

<!-- rendered every time (because never cached) -->
<% if @user %>
Hello <%=h @user %>!
<% else %>
<a href="/login">Login</a>
<% end %>

<!-- rendered only when cache is expired or not found -->
<% lifetime = 5*60 # sec, default is nil(=infinity) %>
<% cache_with("members", lifetime) do %>
<ul>
<% for member in @members %>
<li><%=h @member.name %></li>
<% end %>
<ul>
<% end %>


Reference
---------

Merb_piece_cache plugin provides the following helpers.

* if_not_cached() {|cache_key| ... }
Register block as callback object which is called when fragment
cache is re-generated (In other words, block parameter is called
only when cache is re-generated).
Block paramter can receive cache_key as 1st argument so that
you can switch data processing according to cache_key.

* piece_cache(cache_key, lifetime=nil) { ... }
When cache is found and not expired, then add cache data into
view.
Otherwise, call block to re-generate cache data and cache it.
(Notice that block parameter is called only when cache is re-
generated.)
It is able to specify cache lifetime. For example, if you specify
lifetime=5*60, cache data will be expired after 5 minutes.

* cache_with(cache_key, lifetime=nil) { ... }
Alias of piece_cache().
You can re-define this helper to extend cache behaviour.


Configuration
-------------

You can configure merb_piece_cache plugin in your environment file.
(config/environments/{production,development,test}.rb).
For example, add the followings into your 'development.rb':

Merb::plugins.config[:merb_piece_cache] = {
## show cached area in template (convenient for debug!)
:show_area => true,
## directory to place cache files (default: "MERB_ROOT/tmp/
cache")
:root_dir => "/var/tmp",
}


Tips
----

* You can prepare different data in if_not_exist() according to
cache_key.

def index
if_not_cached {|cache_key|
case cache_key
when /^post\/(\d+)$/
@post = BlogPost.get($1)
when /^post\/(\d+)/comments$/
@comments = BlogComment.all:)post_id=>$1)
end
}
render
end

* piece_cache() (and also cache_with()) checks template timestamp.
If cache data is older than template timestamp, then cache data will
be
re-generated.


License
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top