aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/cache
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/cache')
-rw-r--r--activesupport/lib/active_support/cache/redis_cache_store.rb49
1 files changed, 36 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/cache/redis_cache_store.rb b/activesupport/lib/active_support/cache/redis_cache_store.rb
index b8227359a5..c5f8c876ba 100644
--- a/activesupport/lib/active_support/cache/redis_cache_store.rb
+++ b/activesupport/lib/active_support/cache/redis_cache_store.rb
@@ -17,6 +17,7 @@ end
require "digest/sha2"
require "active_support/core_ext/marshal"
+require "active_support/core_ext/hash/transform_values"
module ActiveSupport
module Cache
@@ -78,7 +79,7 @@ module ActiveSupport
def write_entry(key, entry, options)
if options[:raw] && local_cache
- raw_entry = Entry.new(entry.value.to_s)
+ raw_entry = Entry.new(serialize_entry(entry, raw: true))
raw_entry.expires_at = entry.expires_at
super(key, raw_entry, options)
else
@@ -89,7 +90,7 @@ module ActiveSupport
def write_multi_entries(entries, options)
if options[:raw] && local_cache
raw_entries = entries.map do |key, entry|
- raw_entry = Entry.new(entry.value.to_s)
+ raw_entry = Entry.new(serialize_entry(entry, raw: true))
raw_entry.expires_at = entry.expires_at
end.to_h
@@ -158,7 +159,7 @@ module ActiveSupport
#
# Compression is enabled by default with a 1kB threshold, so cached
# values larger than 1kB are automatically compressed. Disable by
- # passing <tt>cache: false</tt> or change the threshold by passing
+ # passing <tt>compress: false</tt> or change the threshold by passing
# <tt>compress_threshold: 4.kilobytes</tt>.
#
# No expiry is set on cache entries by default. Redis is expected to
@@ -204,7 +205,11 @@ module ActiveSupport
# fetched values.
def read_multi(*names)
if mget_capable?
- read_multi_mget(*names)
+ instrument(:read_multi, names, options) do |payload|
+ read_multi_mget(*names).tap do |results|
+ payload[:hits] = results.keys
+ end
+ end
else
super
end
@@ -320,6 +325,14 @@ module ActiveSupport
end
end
+ def read_multi_entries(names, _options)
+ if mget_capable?
+ read_multi_mget(*names)
+ else
+ super
+ end
+ end
+
def read_multi_mget(*names)
options = names.extract_options!
options = merged_options(options)
@@ -344,7 +357,7 @@ module ActiveSupport
#
# Requires Redis 2.6.12+ for extended SET options.
def write_entry(key, entry, unless_exist: false, raw: false, expires_in: nil, race_condition_ttl: nil, **options)
- value = raw ? entry.value.to_s : serialize_entry(entry)
+ serialized_entry = serialize_entry(entry, raw: raw)
# If race condition TTL is in use, ensure that cache entries
# stick around a bit longer after they would have expired
@@ -359,9 +372,9 @@ module ActiveSupport
modifiers[:nx] = unless_exist
modifiers[:px] = (1000 * expires_in.to_f).ceil if expires_in
- redis.with { |c| c.set key, value, modifiers }
+ redis.with { |c| c.set key, serialized_entry, modifiers }
else
- redis.with { |c| c.set key, value }
+ redis.with { |c| c.set key, serialized_entry }
end
end
end
@@ -378,7 +391,7 @@ module ActiveSupport
if entries.any?
if mset_capable? && expires_in.nil?
failsafe :write_multi_entries do
- redis.with { |c| c.mapped_mset(entries) }
+ redis.with { |c| c.mapped_mset(serialize_entries(entries, raw: options[:raw])) }
end
else
super
@@ -401,15 +414,25 @@ module ActiveSupport
end
end
- def deserialize_entry(raw_value)
- if raw_value
- entry = Marshal.load(raw_value) rescue raw_value
+ def deserialize_entry(serialized_entry)
+ if serialized_entry
+ entry = Marshal.load(serialized_entry) rescue serialized_entry
entry.is_a?(Entry) ? entry : Entry.new(entry)
end
end
- def serialize_entry(entry)
- Marshal.dump(entry)
+ def serialize_entry(entry, raw: false)
+ if raw
+ entry.value.to_s
+ else
+ Marshal.dump(entry)
+ end
+ end
+
+ def serialize_entries(entries, raw: false)
+ entries.transform_values do |entry|
+ serialize_entry entry, raw: raw
+ end
end
def failsafe(method, returning: nil)