Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for lazy compress

  • Loading branch information...
commit c268737466abc6483ca21d7239eb7c5ad0b0c27b 1 parent c7ab43f
@kennyj authored
View
36 activesupport/lib/active_support/cache.rb
@@ -543,6 +543,7 @@ def create (raw_value, created_at, options = {})
entry.instance_variable_set(:@value, raw_value)
entry.instance_variable_set(:@created_at, created_at.to_f)
entry.instance_variable_set(:@compressed, !!options[:compressed])
+ entry.instance_variable_set(:@lazy_compress, !!options[:compressed])
entry.instance_variable_set(:@expires_in, options[:expires_in])
entry
end
@@ -551,7 +552,7 @@ def create (raw_value, created_at, options = {})
# Create a new cache entry for the specified value. Options supported are
# +:compress+, +:compress_threshold+, and +:expires_in+.
def initialize(value, options = {})
- @compressed = false
+ @compressed, @lazy_compress = false, false
@expires_in = options[:expires_in]
@expires_in = @expires_in.to_f if @expires_in
@created_at = Time.now.to_f
@@ -559,10 +560,7 @@ def initialize(value, options = {})
@value = nil
else
@value = Marshal.dump(value)
- if should_compress?(@value, options)
- @value = Zlib::Deflate.deflate(@value)
- @compressed = true
- end
+ @lazy_compress = should_compress?(@value, options)
end
end
@@ -574,8 +572,21 @@ def raw_value
# Get the value stored in the cache.
def value
if @value
- Marshal.load(compressed? ? Zlib::Inflate.inflate(@value) : @value)
+ if compressed?
+ @value = Zlib::Inflate.inflate(@value)
+ @compressed = false
+ end
+ Marshal.load(@value)
+ end
+ end
+
+ def compress!
+ if @lazy_compress && !compressed?
+ @value = Zlib::Deflate.deflate(@value)
+ @compressed = true
+ return true
end
+ false
end
def compressed?
@@ -612,6 +623,19 @@ def size
end
end
+ protected
+ def marshal_dump
+ compress!
+ instance_variables.inject({}) do |ret, var|
+ ret[var] = instance_variable_get(var)
+ ret
+ end
+ end
+
+ def marshal_load(hash)
+ hash.each { |k, v| instance_variable_set(k, v) }
+ end
+
private
def should_compress?(serialized_value, options)
if options[:compress]
View
19 activesupport/test/caching_test.rb
@@ -217,9 +217,10 @@ def test_read_and_write_compressed_small_data
def test_read_and_write_compressed_large_data
@cache.write('foo', 'bar', :compress => true, :compress_threshold => 2)
- raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
assert_equal 'bar', @cache.read('foo')
- assert_equal 'bar', Marshal.load(Zlib::Inflate.inflate(raw_value))
+ entry = @cache.send(:read_entry, 'foo', {})
+ entry.compress! unless entry.compressed?
+ assert_equal 'bar', Marshal.load(Zlib::Inflate.inflate(entry.raw_value))
end
def test_read_and_write_compressed_nil
@@ -739,15 +740,29 @@ def test_expired
def test_compress_values
entry = ActiveSupport::Cache::Entry.new("value", :compress => true, :compress_threshold => 1)
+ assert_equal true, entry.instance_variable_get("@lazy_compress")
+ assert_equal false, entry.compressed?
assert_equal "value", entry.value
+ assert_equal true, entry.compress!
assert_equal true, entry.compressed?
assert_equal "value", Marshal.load(Zlib::Inflate.inflate(entry.raw_value))
end
def test_non_compress_values
entry = ActiveSupport::Cache::Entry.new("value")
+ assert_equal false, entry.instance_variable_get("@lazy_compress")
assert_equal "value", entry.value
assert_equal "value", Marshal.load(entry.raw_value)
assert_equal false, entry.compressed?
+ assert_equal false, entry.compress!
+ end
+
+ def test_marshal
+ entry = ActiveSupport::Cache::Entry.new("value", :compress => true, :compress_threshold => 1)
+ cache = Marshal.load(Marshal.dump(entry))
+ assert_equal true, entry.compressed?
+ entry.instance_variables.each do |var|
+ assert_equal cache.instance_variable_get(var), entry.instance_variable_get(var)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.