# Fragment caching strategy

**URL:** <https://www.ruby-forum.com/t/fragment-caching-strategy/176586>\
**Category:** Rails\
**Created:** [October 2, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/fragment-caching-strategy/176586 "2009-10-02T18:45:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [October 2, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/fragment-caching-strategy/176586/1 "2009-10-02T18:45:47Z")

</div>

Right now, I have the :action\_suffix for a fragment I cache such that I  
can find it when I need to delete it from the cache. But an episode on  
Rails Casts gave the idea that another approach is to not worry about  
deleting the old fragments. They will eventually just get pushed out of  
the cache. Instead, just make sure that the name is unique to the  
version of the object(s) for which the fragment renders.

So, the suggestion was to make the :action\_suffix tied to the last  
update field or an equivalent field.

Can anyone comment on this strategy? It seems much easier to me. It  
somewhat solves the problem of trying to kill the old cache entries with  
all the ugly sweeper logic.

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [October 2, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/fragment-caching-strategy/176586/2 "2009-10-02T19:11:09Z")

</div>

> So, the suggestion was to make the :action\_suffix tied to the last  
> update field or an equivalent field.
> 
> Can anyone comment on this strategy? It seems much easier to me. It  
> somewhat solves the problem of trying to kill the old cache entries  
> with  
> all the ugly sweeper logic.

Seems reasonable. As long as you’re sure your cache will expire the  
old entries and not keep piling them up until you run out of ram/disk  
space.

I would consider adding a generic method to all your models named say  
“fragment\_key” that does whatever is necessary to generate a unique  
key. That way in your controllers/views you never have to think what  
fields to put in there to get the right version. It’s always just  
@instance.fragment\_key. And you can hash that if you want to shorten  
it down and avoid any key length issues or invalid characters, etc.

You might also find this interesting…

> **[How to avoid the dog-pile effect on your Rails app « Plataformatec Blog](http://blog.plataformatec.com.br/2009/09/how-to-avoid-dog-pile-effect-rails-app/)**
>
> Everyone already heard about scalability at least once. Everyone already heard about memcached as well. What not everyone might heard is the dog-pile effect and how to avoid it. But before we start, let's take a look on how to use Rails with...

-philip

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [October 2, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/fragment-caching-strategy/176586/3 "2009-10-02T20:26:21Z")

</div>

Philip H. wrote:

> Seems reasonable. As long as you’re sure your cache will expire the  
> old entries and not keep piling them up until you run out of ram/disk  
> space.

I’m using memcache. I’ll research and double check and make sure it  
just pushes things out the cache. The alternative would be to have them  
expire after a few days.

Thanks for the link too.

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [October 2, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/fragment-caching-strategy/176586/4 "2009-10-02T20:30:47Z")

</div>

On Oct 2, 2009, at 11:26 AM, Perry S. wrote:

> them  
> expire after a few days.

Memcache will push things out of the cache to make room for new items.

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [October 3, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/fragment-caching-strategy/176586/5 "2009-10-03T13:45:07Z")

</div>

Would the built-in lock\_version magic column be good enough for a  
fragment\_key?
