-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "Low-Level Caching" part to "Caching With Rails" guide #14257
Conversation
@@ -140,6 +140,41 @@ You can also combine the two schemes which is called "Russian Doll Caching": | |||
|
|||
It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment. | |||
|
|||
### Low-level Caching | |||
|
|||
Sometimes you don't need to mess with view caching and just want to cache some calculated value or query result. Not everybody understands that it's not the case to reinvent the wheel: Rails caching mechanism works great for storing __any__ kind of information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, I would say:
"Sometimes you need to cache a particular value or query result, instead of caching view fragments. Rails caching mechanism works great for storing any kind of information."
@zzak I updated PR according to your comments and also wrapped a code snippet into a |
@killthekitten 👍 looks good to me now, maybe @fxn can merge it Could you try to remember Thanks! 💖 |
The idea is good. Needs some copy-editing, I see a few grammatical errors and some fixed-width fonts missing. The example has two methods, but unless I am missing something one of them would be enough. |
Agree, two examples might be overkill since our goal isn't to explain how to implement model methods in this case. |
I believe to be idiomatic and cover this important API, the manual cache fetch should base the key on Since the example would obviously be using a hand-crafted cache key, it would also depict you define it yourself. |
@fxn something like that? |
|
||
Sometimes you need to cache a particular value or query result, instead of caching view fragments. Rails caching mechanism works great for storing __any__ kind of information. | ||
|
||
The most efficient way to implement low-level caching is using the `Rails.cache.fetch` method. This method does both reading and writing to the cache. When only a single argument, the key is fetched and value from the cache is returned. If a block was also passed, the result of the block will be cached to the given key and the result is returned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/When only a single argument/When passed only a single argument.
s/a block was also passed/a block is passed
I'm ok to merge this. @fxn ? |
@vijaydev please go ahead! |
Add "Low-Level Caching" part to "Caching With Rails" guide
I noticed that most of the people that are working with Rails (including me) are not familiar enough with low-level concepts of Rails caching mechanism and treat it only as a mechanism to cache view data. So I decided to add some useful info, and used a part of an article by Heroku for this purpose.