# I miss Rails's Russian Doll caching

**URL:** <https://discourse.hanakai.org/t/i-miss-railss-russian-doll-caching/474>\
**Category:** questions\
**Created:** [November 5, 2018, 2:03pm UTC](https://discourse.hanakai.org/t/i-miss-railss-russian-doll-caching/474 "2018-11-05T14:03:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KaptajnKold](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/kaptajnkold/32/369_2.png) [@KaptajnKold](https://discourse.hanakai.org/u/KaptajnKold)\
**Post date:** [November 5, 2018, 2:03pm UTC](https://discourse.hanakai.org/t/i-miss-railss-russian-doll-caching/474/1 "2018-11-05T14:03:37Z")

</div>

I’m a Rails developer who is new to Hanami, and so far I like a lot of what I’m seeing 😃

One thing I am missing _a lot_ from Rails however, is Russian doll caching. I am guessing the reason it’s not there, is related to Hanami’s staunch belief that all data required for a view should be loaded in as few queries as possible, preferably just one, and that the nemesis of performance is N+1 queries. If all the data required for a view is available, then Russian doll caching isn’t very valuable.

However, consider this example. I have a relation _Accounts_, and each account has a number of associated relations. Say I want to list all accounts:

| Account | Followers | Posts | Outstanding invitations | Highest voted post | … |
| --- | --- | --- | --- | --- | --- |
| Alice | 205 | 34 | 0 | How to fix bugs | … |
| Bob | 19 | 304 | 2 | How to log out? | … |
| Carol | 23 | 9 | 0 | Preferred color of shed? | … |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋱ |

As you can see, to get all the data for this view requires joining and aggregating many tables. It could get very expensive if the tables are big and the joins are complex. In Rails what I would do, is to embrace N+1 and rely on Russian Doll caching to ensure I don’t actually hit the DB N+1 times. If each row changes relatively infrequently, this is potentially cheaper than fetching all the data in one query (YMMV of course).

Am I missing something fundamental? How would you solve this in Hanami?

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [November 6, 2018, 2:13pm UTC](https://discourse.hanakai.org/t/i-miss-railss-russian-doll-caching/474/2 "2018-11-06T14:13:38Z")

</div>

> [@KaptajnKold](#):
>
> I am guessing the reason it’s not there, is related to Hanami’s staunch belief that all data required for a view should be loaded in as few queries as possible

Your assumption makes sense for all the apps, regardless of Hanami. But that isn’t the reason why we don’t have caching.

The answer is simpler: we didn’t implement because we simply didn’t plan it. It would be an amazing feature to have, but it’s a complex beast that we didn’t had the workforce to implement.

---

<div class="post-metadata">

**Author:** ![buszu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/buszu/32/319_2.png) [@buszu](https://discourse.hanakai.org/u/buszu)\
**Post date:** [November 16, 2018, 3:24pm UTC](https://discourse.hanakai.org/t/i-miss-railss-russian-doll-caching/474/3 "2018-11-16T15:24:17Z")

</div>

Maybe not Russian Doll but talking about caching generally I wonder if [https://github.com/sorentwo/readthis](https://github.com/sorentwo/readthis) would be a good choice to use for caching in hanami.

---

<div class="post-metadata">

**Author:** ![jodosha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/jodosha/32/710_2.png) [@jodosha](https://discourse.hanakai.org/u/jodosha)\
**Post date:** [December 10, 2018, 1:43pm UTC](https://discourse.hanakai.org/t/i-miss-railss-russian-doll-caching/474/4 "2018-12-10T13:43:17Z")

</div>

@buszu Thanks for the suggestion, but `readthis` is a backend.

The most important (and difficult) part of the template caching (a-là Russian Doll), is the caching strategy itself. For instance when evaluating a template if cache a fresh content, or serve it from cache if already cached content, or to expire the cache if stale…
