aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-04-12 14:03:40 -0400
committerGitHub <noreply@github.com>2018-04-12 14:03:40 -0400
commitb7760eb75a9dd002260a361119aea0b255e1f573 (patch)
tree2179c59ba127bf4f22d39f450f3dbdd0816609ed /activesupport/lib
parent5b7de1b70156ed0861e494ab9900c6b87c206a67 (diff)
parent66df366268c4e5b17c9235b5a0c3aabcbd578e01 (diff)
downloadrails-b7760eb75a9dd002260a361119aea0b255e1f573.tar.gz
rails-b7760eb75a9dd002260a361119aea0b255e1f573.tar.bz2
rails-b7760eb75a9dd002260a361119aea0b255e1f573.zip
Merge pull request #32539 from chancancode/anticompress
Fix ActiveSupport::Cache compression
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/cache.rb67
1 files changed, 32 insertions, 35 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index d48a89e7a9..d769e2c8ea 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -713,17 +713,14 @@ module ActiveSupport
DEFAULT_COMPRESS_LIMIT = 1.kilobyte
# Creates a new cache entry for the specified value. Options supported are
- # +:compress+, +:compress_threshold+, and +:expires_in+.
- def initialize(value, options = {})
- @value = value
- if should_compress?(options)
- compress!
- end
-
- @version = options[:version]
+ # +:compress+, +:compress_threshold+, +:version+ and +:expires_in+.
+ def initialize(value, compress: true, compress_threshold: DEFAULT_COMPRESS_LIMIT, version: nil, expires_in: nil, **)
+ @value = value
+ @version = version
@created_at = Time.now.to_f
- @expires_in = options[:expires_in]
- @expires_in = @expires_in.to_f if @expires_in
+ @expires_in = expires_in && expires_in.to_f
+
+ compress!(compress_threshold) if compress
end
def value
@@ -755,17 +752,13 @@ module ActiveSupport
# Returns the size of the cached value. This could be less than
# <tt>value.size</tt> if the data is compressed.
def size
- if defined?(@s)
- @s
+ case value
+ when NilClass
+ 0
+ when String
+ @value.bytesize
else
- case value
- when NilClass
- 0
- when String
- @value.bytesize
- else
- @s = Marshal.dump(@value).bytesize
- end
+ @s ||= Marshal.dump(@value).bytesize
end
end
@@ -782,31 +775,35 @@ module ActiveSupport
end
private
- 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 : marshaled_value).bytesize
+ def compress!(compress_threshold)
+ case @value
+ when nil, true, false, Numeric
+ uncompressed_size = 0
+ when String
+ uncompressed_size = @value.bytesize
+ else
+ serialized = Marshal.dump(@value)
+ uncompressed_size = serialized.bytesize
+ end
+
+ if uncompressed_size >= compress_threshold
+ serialized ||= Marshal.dump(@value)
+ compressed = Zlib::Deflate.deflate(serialized)
- serialized_value_size >= compress_threshold
+ if compressed.bytesize < uncompressed_size
+ @value = compressed
+ @compressed = true
+ end
end
end
def compressed?
- defined?(@compressed) ? @compressed : false
- end
-
- def compress!
- @value = Zlib::Deflate.deflate(marshaled_value)
- @compressed = true
+ defined?(@compressed)
end
def uncompress(value)
Marshal.load(Zlib::Inflate.inflate(value))
end
-
- def marshaled_value
- @marshaled_value ||= Marshal.dump(@value)
- end
end
end
end