aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorVijay Dev <vijaydev.cse@gmail.com>2014-03-06 22:53:08 +0530
committerVijay Dev <vijaydev.cse@gmail.com>2014-03-06 22:53:08 +0530
commit507ee79a6ce2756266fbe82d63235fe7aef77594 (patch)
tree2f13b8773a3ee521c9d46e8f1c080b509b49915d /guides
parentb7192143edc8a8988c8ae0bffb13eb038f08ef16 (diff)
parentd361d9303b3673f73d541ffe0c5e0e81abe5391c (diff)
downloadrails-507ee79a6ce2756266fbe82d63235fe7aef77594.tar.gz
rails-507ee79a6ce2756266fbe82d63235fe7aef77594.tar.bz2
rails-507ee79a6ce2756266fbe82d63235fe7aef77594.zip
Merge pull request #14257 from killthekitten/patch-1
Add "Low-Level Caching" part to "Caching With Rails" guide
Diffstat (limited to 'guides')
-rw-r--r--guides/source/caching_with_rails.md20
1 files changed, 20 insertions, 0 deletions
diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md
index 0d45e5fb28..e898d75d1a 100644
--- a/guides/source/caching_with_rails.md
+++ b/guides/source/caching_with_rails.md
@@ -140,6 +140,26 @@ 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 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 passed only a single argument, the key is fetched and value from the cache is returned. If a block is passed, the result of the block will be cached to the given key and the result is returned.
+
+Consider the following example. An application has a `Product` model with an instance method that looks up the product’s price on a competing website. The data returned by this method would be perfect for low-level caching:
+
+```ruby
+class Product < ActiveRecord::Base
+ def competing_price
+ Rails.cache.fetch("#{cache_key}/competing_price", expires_in: 12.hours) do
+ Competitor::API.find_price(id)
+ end
+ end
+end
+```
+
+NOTE: Notice that in this example we used `cache_key` method, so the resulting cache-key will be something like `products/233-20140225082222765838000/competing_price`. `cache_key` generates a string based on the model’s `id` and `updated_at` attributes. This is a common convention and has the benefit of invalidating the cache whenever the product is updated. In general, when you use low-level caching for instance level information, you need to generate a cache key.
+
### SQL Caching
Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again.