Permalink
Browse files

Merge pull request #30447 from yhirano55/update_caching_with_rails

Update Caching with Rails guide [ci skip]
  • Loading branch information...
eileencodes committed Aug 29, 2017
2 parents ca5621d + ddc8f43 commit 07402aa1307a4ff71b4ef6581f95b8612238a6af
Showing with 3 additions and 3 deletions.
  1. +3 −3 guides/source/caching_with_rails.md
@@ -175,7 +175,7 @@ class Game < ApplicationRecord
end
```
With `touch` set to true, any action which changes `updated_at` for a game
With `touch` set to `true`, any action which changes `updated_at` for a game
record will also change it for the associated product, thereby expiring the
cache.
@@ -272,7 +272,7 @@ Sometimes you need to cache a particular value or query result instead of cachin
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, that block will be executed in the event of a cache miss. The return value of the block will be written to the cache under the given cache key, and that return value will be returned. In case of cache hit, the cached value will be returned without executing the block.
Consider the following example. An application has a `Product` model with an instance method that looks up the products price on a competing website. The data returned by this method would be perfect for low-level caching:
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 < ApplicationRecord
@@ -284,7 +284,7 @@ class Product < ApplicationRecord
end
```
NOTE: Notice that in this example we used the `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 models `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.
NOTE: Notice that in this example we used the `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

0 comments on commit 07402aa

Please sign in to comment.