From ed100166874fb4a542c5aaba933a4cca5ed72269 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Mon, 13 Nov 2017 19:16:53 -0700 Subject: Cache: Enable compression by default for values > 1kB. Compression has long been available, but opt-in and at a 16kB threshold. It wasn't enabled by default due to CPU cost. Today it's cheap and typical cache data is eminently compressible, such as HTML or JSON fragments. Compression dramatically reduces Memcached/Redis mem usage, which means the same cache servers can store more data, which means higher hit rates. To disable compression, pass `compress: false` to the initializer. --- activesupport/lib/active_support/cache.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'activesupport/lib/active_support/cache.rb') diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 9848e0c623..395aa5e8f1 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -154,12 +154,11 @@ module ActiveSupport # cache.namespace = -> { @last_mod_time } # Set the namespace to a variable # @last_mod_time = Time.now # Invalidate the entire cache by changing namespace # - # Caches can also store values in a compressed format to save space and - # reduce time spent sending data. Since there is overhead, values must be - # large enough to warrant compression. To turn on compression either pass - # compress: true in the initializer or as an option to +fetch+ - # or +write+. To specify the threshold at which to compress values, set the - # :compress_threshold option. The default threshold is 16K. + # Cached data larger than 1kB are compressed by default. To turn off + # compression, pass compress: false to the initializer or to + # individual +fetch+ or +write+ method calls. The 1kB compression + # threshold is configurable with the :compress_threshold option, + # specified in bytes. class Store cattr_accessor :logger, instance_writer: true @@ -218,8 +217,7 @@ module ActiveSupport # ask whether you should force a cache write. Otherwise, it's clearer to # just call Cache#write. # - # Setting :compress will store a large cache entry set by the call - # in a compressed format. + # Setting compress: false disables compression of the cache entry. # # Setting :expires_in will set an expiration time on the cache. # All caches support auto-expiring content after a specified number of @@ -664,7 +662,7 @@ module ActiveSupport class Entry # :nodoc: attr_reader :version - DEFAULT_COMPRESS_LIMIT = 16.kilobytes + DEFAULT_COMPRESS_LIMIT = 1.kilobyte # Creates a new cache entry for the specified value. Options supported are # +:compress+, +:compress_threshold+, and +:expires_in+. @@ -739,8 +737,8 @@ module ActiveSupport private def should_compress?(value, options) - if value && options[:compress] - compress_threshold = options[:compress_threshold] || DEFAULT_COMPRESS_LIMIT + 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 return true if serialized_value_size >= compress_threshold -- cgit v1.2.3