diff options
author | Michael Grosser <michael@grosser.it> | 2015-11-07 12:07:49 -0800 |
---|---|---|
committer | Michael Grosser <michael@grosser.it> | 2015-11-10 21:12:30 -0800 |
commit | a481f07817eb0ea3ea1e4dee07ce4fe7aa865bac (patch) | |
tree | 2cd23235e8e85cb9a471c5cb43d0996245afb0e3 /activesupport/lib/active_support/cache | |
parent | 5388464af6ec229ddf3a1040cd7466f45370cedd (diff) | |
download | rails-a481f07817eb0ea3ea1e4dee07ce4fe7aa865bac.tar.gz rails-a481f07817eb0ea3ea1e4dee07ce4fe7aa865bac.tar.bz2 rails-a481f07817eb0ea3ea1e4dee07ce4fe7aa865bac.zip |
send normalized keys to the cache backends so they do not need to manage this themselves
Diffstat (limited to 'activesupport/lib/active_support/cache')
3 files changed, 24 insertions, 25 deletions
diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 9a88fc286a..ef1f9a3851 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -60,7 +60,7 @@ module ActiveSupport matcher = key_matcher(matcher, options) search_dir(cache_path) do |path| key = file_path_key(path) - delete_entry(key, options) if key.match(matcher) + delete_entry(normalize_key(key, options), options) if key.match(matcher) end end end @@ -68,9 +68,8 @@ module ActiveSupport protected def read_entry(key, options) - file_name = key_file_path(key) - if File.exist?(file_name) - File.open(file_name) { |f| Marshal.load(f) } + if File.exist?(key) + File.open(key) { |f| Marshal.load(f) } end rescue => e logger.error("FileStoreError (#{e}): #{e.message}") if logger @@ -78,23 +77,21 @@ module ActiveSupport end def write_entry(key, entry, options) - file_name = key_file_path(key) - return false if options[:unless_exist] && File.exist?(file_name) - ensure_cache_path(File.dirname(file_name)) - File.atomic_write(file_name, cache_path) {|f| Marshal.dump(entry, f)} + return false if options[:unless_exist] && File.exist?(key) + ensure_cache_path(File.dirname(key)) + File.atomic_write(key, cache_path) {|f| Marshal.dump(entry, f)} true end def delete_entry(key, options) - file_name = key_file_path(key) - if File.exist?(file_name) + if File.exist?(key) begin - File.delete(file_name) - delete_empty_directories(File.dirname(file_name)) + File.delete(key) + delete_empty_directories(File.dirname(key)) true rescue => e # Just in case the error was caused by another process deleting the file first. - raise e if File.exist?(file_name) + raise e if File.exist?(key) false end end @@ -118,7 +115,8 @@ module ActiveSupport end # Translate a key into a file path. - def key_file_path(key) + def normalize_key(key, options) + key = super fname = URI.encode_www_form_component(key) if fname.size > FILEPATH_MAX_SIZE @@ -175,7 +173,7 @@ module ActiveSupport # Modifies the amount of an already existing integer value that is stored in the cache. # If the key is not found nothing is done. def modify_value(name, amount, options) - file_name = key_file_path(namespaced_key(name, options)) + file_name = normalize_key(name, options) lock_file(file_name) do options = merged_options(options) diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index e2f536ef1e..5eaf68a244 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -97,7 +97,7 @@ module ActiveSupport options = merged_options(options) instrument_multi(:read, names, options) do - keys_to_names = Hash[names.map{|name| [escape_key(namespaced_key(name, options)), name]}] + keys_to_names = Hash[names.map{|name| [normalize_key(name, options), name]}] raw_values = @data.get_multi(keys_to_names.keys, :raw => true) values = {} raw_values.each do |key, value| @@ -115,7 +115,7 @@ module ActiveSupport def increment(name, amount = 1, options = nil) # :nodoc: options = merged_options(options) instrument(:increment, name, :amount => amount) do - @data.incr(escape_key(namespaced_key(name, options)), amount) + @data.incr(normalize_key(name, options), amount) end rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.message}") if logger @@ -129,7 +129,7 @@ module ActiveSupport def decrement(name, amount = 1, options = nil) # :nodoc: options = merged_options(options) instrument(:decrement, name, :amount => amount) do - @data.decr(escape_key(namespaced_key(name, options)), amount) + @data.decr(normalize_key(name, options), amount) end rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.message}") if logger @@ -153,7 +153,7 @@ module ActiveSupport protected # Read an entry from the cache. def read_entry(key, options) # :nodoc: - deserialize_entry(@data.get(escape_key(key), options)) + deserialize_entry(@data.get(key, options)) rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.message}") if logger nil @@ -168,7 +168,7 @@ module ActiveSupport # Set the memcache expire a few minutes in the future to support race condition ttls on read expires_in += 5.minutes end - @data.send(method, escape_key(key), value, expires_in, options) + @data.send(method, key, value, expires_in, options) rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.message}") if logger false @@ -176,7 +176,7 @@ module ActiveSupport # Delete an entry from the cache. def delete_entry(key, options) # :nodoc: - @data.delete(escape_key(key)) + @data.delete(key) rescue Dalli::DalliError => e logger.error("DalliError (#{e}): #{e.message}") if logger false @@ -187,8 +187,8 @@ module ActiveSupport # Memcache keys are binaries. So we need to force their encoding to binary # before applying the regular expression to ensure we are escaping all # characters properly. - def escape_key(key) - key = key.to_s.dup + def normalize_key(key, options) + key = super.dup key = key.force_encoding(Encoding::ASCII_8BIT) key = key.gsub(ESCAPE_KEY_CHARS){ |match| "%#{match.getbyte(0).to_s(16).upcase}" } key = "#{key[0, 213]}:md5:#{Digest::MD5.hexdigest(key)}" if key.size > 250 diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index fa007aad56..aec33ad46b 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -93,14 +93,14 @@ module ActiveSupport def increment(name, amount = 1, options = nil) # :nodoc: return super unless local_cache value = bypass_local_cache{super} - set_cache_value(value, name, amount, options) + set_cache_value(name, value, options) value end def decrement(name, amount = 1, options = nil) # :nodoc: return super unless local_cache value = bypass_local_cache{super} - set_cache_value(value, name, amount, options) + set_cache_value(name, value, options) value end @@ -124,6 +124,7 @@ module ActiveSupport end def set_cache_value(value, name, amount, options) # :nodoc: + name = normalize_key(name, options) cache = local_cache cache.mute do if value |