# Merb\_piece\_cache 0.1.0 released

**URL:** <https://www.ruby-forum.com/t/merb-piece-cache-0-1-0-released/178652>\
**Category:** Ruby\
**Created:** [November 9, 2009, 12:30am UTC](https://www.ruby-forum.com/t/merb-piece-cache-0-1-0-released/178652 "2009-11-09T00:30:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [November 9, 2009, 12:30am UTC](https://www.ruby-forum.com/t/merb-piece-cache-0-1-0-released/178652/1 "2009-11-09T00:30:58Z")

</div>

I releaded merb\_piece\_cache 0.1.0.

> **[GitHub - kwatch/merb\_piece\_cache: html fragment cache module for Merb](https://github.com/kwatch/merb_piece_cache)**
>
> html fragment cache module for Merb. Contribute to kwatch/merb\_piece\_cache development by creating an account on GitHub.

## 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.

- 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

MIT License
