15 8 / 2012
Cachex: tag based fragment caching on steriods
I’ve just released another gem. Its called Cachex.
This gem is inspired by excellent Cashier gem. I wanted to take it a step further and try to automate tag dependency generation.
I had this scenario in mind:
- Blog has many posts
- Each post has an author (user)
- Each post has many comments
- Each comment has its own author (user)
Each model has its own partial. Inside
_post partial we have
_user partial that renders post’s author.
_post partial also include many
_comment partials. Each
_comment partial have author’s name displayed in it.
We’re also using fragment caching in order to cache entire post for faster serving. The burning question is: what happens if user decides to change its name (or perhaps, more commonly, an avatar)?. We have to invalidate every cached partial that displayed that user. How do we do that?
If we’re using auto-expiring key strategy the only way to accomplish this would be to
touch everything that has to do with that user. That just doesn’t make any sense.
If we’re using tag-based strategy, then we would need to manually tag every fragment with all users that have to do something with it (even users from its comments). Such approach is going to be very hard when we get to deeply nested partials. Top most partial will have to be aware of all dependencies down to the deepest one.
And finally, my solution: to automatically extract all dependencies from all sub-partials and to store them to help cache invalidation.
Read more on github.
Permalink 1 note