# Use fragment caching from helper function

**URL:** <https://www.ruby-forum.com/t/use-fragment-caching-from-helper-function/75515>\
**Category:** Rails\
**Created:** [November 10, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/use-fragment-caching-from-helper-function/75515 "2006-11-10T16:14:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@pazu](https://www.ruby-forum.com/u/pazu)\
**Post date:** [November 10, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/use-fragment-caching-from-helper-function/75515/1 "2006-11-10T16:14:37Z")

</div>

We have a helper function that fetches data and does some  
computations. This helper is used mostly everywhere. We have now come  
to the phase of optimization and come to the conclusion that some  
caching will help speed things up. I good place to get a lot of bang  
for a small investment would be to make use of the fragment caching in  
Rails and we would like to utilize it in this function in order to  
avoid to have to add the cache call everywhere.

Having looked at the code for ActionView#cache and  
ActionController#cache\_erb\_fragment it seems that a block from the erb  
template is needed. The problem is that we don’t have a block from the  
template available in the helper function (without changing in the  
function call in all templates…). Is there another way to get at the  
caching functionality without having to re-write it ourselves?

/Marcus

---

<div class="post-metadata">

**Author:** ![pazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@pazu](https://www.ruby-forum.com/u/pazu)\
**Post date:** [November 10, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/use-fragment-caching-from-helper-function/75515/2 "2006-11-10T20:40:02Z")

</div>

Marcus B. wrote:

> We have a helper function that fetches data and does some  
> computations. This helper is used mostly everywhere. We have now come  
> to the phase of optimization and come to the conclusion that some  
> caching will help speed things up. I good place to get a lot of bang  
> for a small investment would be to make use of the fragment caching in  
> Rails and we would like to utilize it in this function in order to  
> avoid to have to add the cache call everywhere.
> 
> Having looked at the code for ActionView#cache and  
> ActionController#cache\_erb\_fragment it seems that a block from the erb  
> template is needed. The problem is that we don’t have a block from the  
> template available in the helper function (without changing in the  
> function call in all templates…). Is there another way to get at the  
> caching functionality without having to re-write it ourselves?
> 
> /Marcus

[http://caboo.se/doc/classes/ActionController/Caching/Actions/ActionCacheFilter/Fragments.html#M003918](http://caboo.se/doc/classes/ActionController/Caching/Actions/ActionCacheFilter/Fragments.html#M003918)

You can use read\_fragment and write fragment. So your helper can do  
something like:

def generate\_foo(id)  
fragment\_name = “foo\_helper/#{id}”  
if output = read\_fragment(fragment\_name)  
output  
else  
output = do\_something(id)  
write\_fragment fragment\_name, output  
end

```
output

```

end

if read\_fragment(name) does not read a fragment, it simply returns nil.  
So you can decide wether or not the execute your expensive method based  
on that.
