File: C:/Ruby27-x64/lib/ruby/gems/2.7.0/doc/activesupport-7.0.4.2/ri/ActiveSupport/Cache/Store/fetch-i.ri
U:RDoc::AnyMethod[iI"
fetch:ETI"&ActiveSupport::Cache::Store#fetch;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"KFetches data from the cache, using the given key. If there is data in ;TI">the cache with the given key, then that data is returned.;To:RDoc::Markup::BlankLine o;
; [
I"NIf there is no such data in the cache (a cache miss), then +nil+ will be ;TI"Nreturned. However, if a block has been passed, that block will be passed ;TI"Pthe key and executed in the event of a cache miss. The return value of the ;TI"Lblock will be written to the cache under the given cache key, and that ;TI"#return value will be returned.;T@o:RDoc::Markup::Verbatim; [
I"$cache.write('today', 'Monday')
;TI")cache.fetch('today') # => "Monday"
;TI"
;TI"$cache.fetch('city') # => nil
;TI"cache.fetch('city') do
;TI" 'Duckburgh'
;TI" end
;TI",cache.fetch('city') # => "Duckburgh"
;T:@format0S:RDoc::Markup::Heading:
leveli : textI"Options;T@o;
; [I"NInternally, +fetch+ calls #read_entry, and calls #write_entry on a cache ;TI"Hmiss. Thus, +fetch+ supports the same options as #read and #write. ;TI":Additionally, +fetch+ supports the following options:;T@o:RDoc::Markup::List:
@type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [
o;
; [I"H<tt>force: true</tt> - Forces a cache "miss," meaning we treat the ;TI"Ecache value as missing even if it's present. Passing a block is ;TI"Krequired when +force+ is true so this always results in a cache write.;T@o;; [I"$cache.write('today', 'Monday')
;TI"Dcache.fetch('today', force: true) { 'Tuesday' } # => 'Tuesday'
;TI":cache.fetch('today', force: true) # => ArgumentError
;T;
0o;
; [I"LThe +:force+ option is useful when you're calling some other method to ;TI"Lask whether you should force a cache write. Otherwise, it's clearer to ;TI"just call +write+.;T@o;;0; [o;
; [I"=<tt>skip_nil: true</tt> - Prevents caching a nil result:;T@o;; [ I" cache.fetch('foo') { nil }
;TI"0cache.fetch('bar', skip_nil: true) { nil }
;TI"#cache.exist?('foo') # => true
;TI"$cache.exist?('bar') # => false
;T;
0o;;0; [o;
; [
I"J+:race_condition_ttl+ - Specifies the number of seconds during which ;TI"Jan expired value can be reused while a new value is being generated. ;TI"LThis can be used to prevent race conditions when cache entries expire, ;TI"Kby preventing multiple processes from simultaneously regenerating the ;TI"4same entry (also known as the dog pile effect).;T@o;
; [I"HWhen a process encounters a cache entry that has expired less than ;TI"L+:race_condition_ttl+ seconds ago, it will bump the expiration time by ;TI"I+:race_condition_ttl+ seconds before generating a new value. During ;TI"Ithis extended time window, while the process generates a new value, ;TI"Iother processes will continue to use the old value. After the first ;TI"Dprocess writes the new value, other processes will then use it.;T@o;
; [I"KIf the first process errors out while generating a new value, another ;TI"Lprocess can try to generate a new value after the extended time window ;TI"has elapsed.;T@o;; [I"2# Set all values to expire after one minute.
;TI"Icache = ActiveSupport::Cache::MemoryStore.new(expires_in: 1.minute)
;TI"
;TI"*cache.write('foo', 'original value')
;TI"val_1 = nil
;TI"val_2 = nil
;TI"sleep 60
;TI"
;TI"Thread.new do
;TI"E val_1 = cache.fetch('foo', race_condition_ttl: 10.seconds) do
;TI" sleep 1
;TI" 'new value 1'
;TI" end
;TI" end
;TI"
;TI"Thread.new do
;TI"E val_2 = cache.fetch('foo', race_condition_ttl: 10.seconds) do
;TI" 'new value 2'
;TI" end
;TI" end
;TI"
;TI".cache.fetch('foo') # => "original value"
;TI"Nsleep 10 # First thread extended the life of cache by another 10 seconds
;TI"+cache.fetch('foo') # => "new value 1"
;TI"val_1 # => "new value 1"
;TI" val_2 # => "original value";T;
0:
@fileI" lib/active_support/cache.rb;T:0@omit_headings_from_table_of_contents_below000[ I""(name, options = nil, &block);T@xFI"
Store;TcRDoc::NormalClass00