Method: ActiveSupport::Cache::Store#write
- Defined in:
- lib/active_support/cache.rb
#write(name, value, options = nil) ⇒ Object
Writes the value to the cache with the key. The value must be supported by the coder‘s dump and load methods.
Returns true if the write succeeded, nil if there was an error talking to the cache backend, or false if the write failed for another reason.
By default, cache entries larger than 1kB are compressed. Compression allows more data to be stored in the same memory footprint, leading to fewer cache evictions and higher hit rates.
Options
-
compress: false- Disables compression of the cache entry. -
:compress_threshold- The compression threshold, specified in bytes. Cache entries larger than this threshold will be compressed. Defaults to1.kilobyte. -
:expires_in- Sets a relative expiration time for the cache entry, specified in seconds.:expire_inand:expired_inare aliases for:expires_in.cache = ActiveSupport::Cache::MemoryStore.new(expires_in: 5.minutes) cache.write(key, value, expires_in: 1.minute) # Set a lower value for one entry -
:expires_at- Sets an absolute expiration time for the cache entry.cache = ActiveSupport::Cache::MemoryStore.new cache.write(key, value, expires_at: Time.now.at_end_of_hour) -
:version- Specifies a version for the cache entry. When reading from the cache, if the cached version does not match the requested version, the read will be treated as a cache miss. This feature is used to support recyclable cache keys. -
:unless_exist- Prevents overwriting an existing cache entry.
Other options will be handled by the specific cache store implementation.
672 673 674 675 676 677 678 679 680 |
# File 'lib/active_support/cache.rb', line 672 def write(name, value, = nil) = () key = normalize_key(name, ) instrument(:write, key, ) do entry = Entry.new(value, **, version: normalize_version(name, )) write_entry(key, entry, **) end end |