aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2018-03-14 14:52:03 -0600
committerGitHub <noreply@github.com>2018-03-14 14:52:03 -0600
commit2d2da1861576a3900a295dc3950b447027f9ed9d (patch)
treec207aae01b3ff5cf82129193f93e58682e3a558a
parentb3b193f7d65357970e72711d42db8070dcf92ce4 (diff)
parent066919f245e181dd2a9986343e3fabb3c4485fef (diff)
downloadrails-2d2da1861576a3900a295dc3950b447027f9ed9d.tar.gz
rails-2d2da1861576a3900a295dc3950b447027f9ed9d.tar.bz2
rails-2d2da1861576a3900a295dc3950b447027f9ed9d.zip
Merge pull request #32254 from sgrif/sg-dont-marshal-twice
Don't marshal ActiveSupport::Cache::Entry objects twice
-rw-r--r--activesupport/lib/active_support/cache.rb27
1 files changed, 14 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 1ea2d0bbf2..6967c164ab 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -714,11 +714,9 @@ module ActiveSupport
# Creates a new cache entry for the specified value. Options supported are
# +:compress+, +:compress_threshold+, and +:expires_in+.
def initialize(value, options = {})
- if should_compress?(value, options)
- @value = compress(value)
- @compressed = true
- else
- @value = value
+ @value = value
+ if should_compress?(options)
+ compress!
end
@version = options[:version]
@@ -783,28 +781,31 @@ module ActiveSupport
end
private
- def should_compress?(value, options)
- if value && options.fetch(:compress, true)
+ def should_compress?(options)
+ if @value && options.fetch(:compress, true)
compress_threshold = options.fetch(:compress_threshold, DEFAULT_COMPRESS_LIMIT)
- serialized_value_size = (value.is_a?(String) ? value : Marshal.dump(value)).bytesize
+ serialized_value_size = (@value.is_a?(String) ? @value : marshaled_value).bytesize
- return true if serialized_value_size >= compress_threshold
+ serialized_value_size >= compress_threshold
end
-
- false
end
def compressed?
defined?(@compressed) ? @compressed : false
end
- def compress(value)
- Zlib::Deflate.deflate(Marshal.dump(value))
+ def compress!
+ @value = Zlib::Deflate.deflate(marshaled_value)
+ @compressed = true
end
def uncompress(value)
Marshal.load(Zlib::Inflate.inflate(value))
end
+
+ def marshaled_value
+ @marshaled_value ||= Marshal.dump(@value)
+ end
end
end
end