lib/active_support/cache.rb in activesupport-6.0.2.2 vs lib/active_support/cache.rb in activesupport-6.0.3.rc1

- old
+ new

@@ -50,16 +50,17 @@ # # If the first argument is not a Symbol, then it will simply be returned: # # ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new) # # => returns MyOwnCacheStore.new - def lookup_store(*store_option) - store, *parameters = *Array.wrap(store_option).flatten - + def lookup_store(store = nil, *parameters) case store when Symbol - retrieve_store_class(store).new(*parameters) + options = parameters.extract_options! + retrieve_store_class(store).new(*parameters, **options) + when Array + lookup_store(*store) when nil ActiveSupport::Cache::MemoryStore.new else store end @@ -316,21 +317,21 @@ options = merged_options(options) key = normalize_key(name, options) entry = nil instrument(:read, name, options) do |payload| - cached_entry = read_entry(key, options) unless options[:force] + cached_entry = read_entry(key, **options) unless options[:force] entry = handle_expired_entry(cached_entry, key, options) entry = nil if entry && entry.mismatched?(normalize_version(name, options)) payload[:super_operation] = :fetch if payload payload[:hit] = !!entry if payload end if entry - get_entry_value(entry, name, options) + get_entry_value(entry, name, **options) else - save_block_result_to_cache(name, options) { |_name| yield _name } + save_block_result_to_cache(name, **options) { |_name| yield _name } end elsif options && options[:force] raise ArgumentError, "Missing block: Calling `Cache#fetch` with `force: true` requires a block." else read(name, options) @@ -350,15 +351,15 @@ options = merged_options(options) key = normalize_key(name, options) version = normalize_version(name, options) instrument(:read, name, options) do |payload| - entry = read_entry(key, options) + entry = read_entry(key, **options) if entry if entry.expired? - delete_entry(key, options) + delete_entry(key, **options) payload[:hit] = false if payload nil elsif entry.mismatched?(version) payload[:hit] = false if payload nil @@ -382,11 +383,11 @@ def read_multi(*names) options = names.extract_options! options = merged_options(options) instrument :read_multi, names, options do |payload| - read_multi_entries(names, options).tap do |results| + read_multi_entries(names, **options).tap do |results| payload[:hits] = results.keys end end end @@ -394,14 +395,14 @@ def write_multi(hash, options = nil) options = merged_options(options) instrument :write_multi, hash, options do |payload| entries = hash.each_with_object({}) do |(name, value), memo| - memo[normalize_key(name, options)] = Entry.new(value, options.merge(version: normalize_version(name, options))) + memo[normalize_key(name, options)] = Entry.new(value, **options.merge(version: normalize_version(name, options))) end - write_multi_entries entries, options + write_multi_entries entries, **options end end # Fetches data from the cache, using the given keys. If there is data in # the cache with the given keys, then that data is returned. Otherwise, @@ -436,20 +437,20 @@ options = names.extract_options! options = merged_options(options) instrument :read_multi, names, options do |payload| - reads = read_multi_entries(names, options) + reads = read_multi_entries(names, **options) writes = {} ordered = names.each_with_object({}) do |name, hash| hash[name] = reads.fetch(name) { writes[name] = yield(name) } end payload[:hits] = reads.keys payload[:super_operation] = :fetch_multi - write_multi(writes, options) + write_multi(writes, **options) ordered end end @@ -458,34 +459,34 @@ # Options are passed to the underlying cache implementation. def write(name, value, options = nil) options = merged_options(options) instrument(:write, name, options) do - entry = Entry.new(value, options.merge(version: normalize_version(name, options))) - write_entry(normalize_key(name, options), entry, options) + entry = Entry.new(value, **options.merge(version: normalize_version(name, options))) + write_entry(normalize_key(name, options), entry, **options) end end # Deletes an entry in the cache. Returns +true+ if an entry is deleted. # # Options are passed to the underlying cache implementation. def delete(name, options = nil) options = merged_options(options) instrument(:delete, name) do - delete_entry(normalize_key(name, options), options) + delete_entry(normalize_key(name, options), **options) end end # Returns +true+ if the cache contains an entry for the given key. # # Options are passed to the underlying cache implementation. def exist?(name, options = nil) options = merged_options(options) instrument(:exist?, name) do - entry = read_entry(normalize_key(name, options), options) + entry = read_entry(normalize_key(name, options), **options) (entry && !entry.expired? && !entry.mismatched?(normalize_version(name, options))) || false end end # Deletes all entries with keys matching the pattern. @@ -554,32 +555,32 @@ end end # Reads an entry from the cache implementation. Subclasses must implement # this method. - def read_entry(key, options) + def read_entry(key, **options) raise NotImplementedError.new end # Writes an entry to the cache implementation. Subclasses must implement # this method. - def write_entry(key, entry, options) + def write_entry(key, entry, **options) raise NotImplementedError.new end # Reads multiple entries from the cache implementation. Subclasses MAY # implement this method. - def read_multi_entries(names, options) + def read_multi_entries(names, **options) results = {} names.each do |name| key = normalize_key(name, options) version = normalize_version(name, options) - entry = read_entry(key, options) + entry = read_entry(key, **options) if entry if entry.expired? - delete_entry(key, options) + delete_entry(key, **options) elsif entry.mismatched?(version) # Skip mismatched versions else results[name] = entry.value end @@ -588,19 +589,19 @@ results end # Writes multiple entries to the cache implementation. Subclasses MAY # implement this method. - def write_multi_entries(hash, options) + def write_multi_entries(hash, **options) hash.each do |key, entry| - write_entry key, entry, options + write_entry key, entry, **options end end # Deletes an entry from the cache implementation. Subclasses must # implement this method. - def delete_entry(key, options) + def delete_entry(key, **options) raise NotImplementedError.new end # Merges the default options with ones specific to a method call. def merged_options(call_options) @@ -697,11 +698,11 @@ # When an entry has a positive :race_condition_ttl defined, put the stale entry back into the cache # for a brief period while the entry is being recalculated. entry.expires_at = Time.now + race_ttl write_entry(key, entry, expires_in: race_ttl * 2) else - delete_entry(key, options) + delete_entry(key, **options) end entry = nil end entry end @@ -709,10 +710,10 @@ def get_entry_value(entry, name, options) instrument(:fetch_hit, name, options) { } entry.value end - def save_block_result_to_cache(name, options) + def save_block_result_to_cache(name, **options) result = instrument(:generate, name, options) do yield(name) end write(name, result, options) unless result.nil? && options[:skip_nil]