Skip to content

Commit 2722895

Browse files
cwli24skipkayhiljonathanhefner
committed
Emphasize cacheable objects in guide and API docs [ci-skip]
Clarify to users what objects may be cached, and highlight the option used to cache default non-serializable data. Purpose: Improving new-to-Rails users' experience, as this detail may not be obvious, costing them time and effort spent debugging. Co-authored-by: Hartley McGuire <skipkayhil@gmail.com> Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> (cherry picked from commit de12c18)
1 parent 0e805db commit 2722895

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,17 @@ def retrieve_store_class(store)
141141
# Some implementations may not support all methods beyond the basic cache
142142
# methods of +fetch+, +write+, +read+, +exist?+, and +delete+.
143143
#
144-
# ActiveSupport::Cache::Store can store any serializable Ruby object.
144+
# ActiveSupport::Cache::Store can store any Ruby object that is supported by
145+
# its +coder+'s +dump+ and +load+ methods.
145146
#
146147
# cache = ActiveSupport::Cache::MemoryStore.new
147148
#
148149
# cache.read('city') # => nil
149150
# cache.write('city', "Duckburgh")
150151
# cache.read('city') # => "Duckburgh"
151152
#
153+
# cache.write('not serializable', Proc.new {}) # => TypeError
154+
#
152155
# Keys are always translated into Strings and are case sensitive. When an
153156
# object is specified as a key and has a +cache_key+ method defined, this
154157
# method will be called to define the key. Otherwise, the +to_param+

guides/source/caching_with_rails.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ simply be explicit in a comment, like:
278278

279279
### Low-Level Caching
280280

281-
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.
281+
Sometimes you need to cache a particular value or query result instead of caching view fragments. Rails' caching mechanism works great for storing any serializable information.
282282

283283
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.
284284

@@ -391,7 +391,7 @@ There are some common options that can be used by all cache implementations. The
391391

392392
* `:race_condition_ttl` - This option is used in conjunction with the `:expires_in` option. It will prevent race conditions when cache entries expire by preventing multiple processes from simultaneously regenerating the same entry (also known as the dog pile effect). This option sets the number of seconds that an expired entry can be reused while a new value is being regenerated. It's a good practice to set this value if you use the `:expires_in` option.
393393

394-
* `:coder` - This option allows to replace the default cache entry serialization mechanism by a custom one. The `coder` must respond to `dump` and `load`, and passing a custom coder disable automatic compression.
394+
* `:coder` - This option replaces the default cache entry serialization mechanism with a custom one. The `coder` must respond to `dump` and `load`, and passing a custom coder disables automatic compression.
395395

396396
#### Connection Pool Options
397397

0 commit comments

Comments
 (0)