Skip to content
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

Merged
merged 1 commit into from
Mar 6, 2014

Conversation

killthekitten
Copy link
Contributor

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.

@robin850 robin850 added the docs label Mar 3, 2014
@@ -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.
Copy link
Member

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."

@killthekitten
Copy link
Contributor Author

@zzak I updated PR according to your comments and also wrapped a code snippet into a class Product < ActiveRecord::Base

@zzak
Copy link
Member

zzak commented Mar 3, 2014

@killthekitten 👍 looks good to me now, maybe @fxn can merge it

Could you try to remember [ci skip] next time on your commits that are doc only?

Thanks! 💖

@fxn
Copy link
Member

fxn commented Mar 3, 2014

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.

@zzak
Copy link
Member

zzak commented Mar 3, 2014

Agree, two examples might be overkill since our goal isn't to explain how to implement model methods in this case.

@killthekitten
Copy link
Contributor Author

@fxn @zzak thank you for help, I've made some changes, but I'm not sure about grammatical correctness.

@fxn
Copy link
Member

fxn commented Mar 4, 2014

I believe to be idiomatic and cover this important API, the manual cache fetch should base the key on cache_key. That would be real world code, and you could leverage what cache_key generates to explain generational caching.

Since the example would obviously be using a hand-crafted cache key, it would also depict you define it yourself.

@killthekitten
Copy link
Contributor Author

@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.
Copy link
Member

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

@killthekitten
Copy link
Contributor Author

@vijaydev nice 👍 /cc @fxn

@vijaydev
Copy link
Member

vijaydev commented Mar 6, 2014

I'm ok to merge this. @fxn ?

@fxn
Copy link
Member

fxn commented Mar 6, 2014

@vijaydev please go ahead!

vijaydev added a commit that referenced this pull request Mar 6, 2014
Add "Low-Level Caching" part to "Caching With Rails" guide
@vijaydev vijaydev merged commit 507ee79 into rails:master Mar 6, 2014
@killthekitten killthekitten deleted the patch-1 branch January 15, 2017 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants