1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

fix error with long keys in ActiveSupport::Cache::FileStore

This commit is contained in:
Adam Panzer 2014-06-10 10:53:48 -07:00
parent 3681e1ac2c
commit 9010274627
3 changed files with 13 additions and 0 deletions

View file

@ -1,3 +1,6 @@
* Fixed `ActiveSupport::Cache::FileStore` exploding with long paths.
*Adam Panzer / Michael Grosser*
* Fixed `ActiveSupport::TimeWithZone#-` so precision is not unnecessarily lost
when working with objects with a nanosecond component.

View file

@ -14,6 +14,7 @@ module ActiveSupport
DIR_FORMATTER = "%03X"
FILENAME_MAX_SIZE = 228 # max filename size on file system is 255, minus room for timestamp and random characters appended by Tempfile (used by atomic write)
FILEPATH_MAX_SIZE = 900 # max is 1024, plus some room
EXCLUDED_DIRS = ['.', '..'].freeze
def initialize(cache_path, options = nil)
@ -117,6 +118,10 @@ module ActiveSupport
# Translate a key into a file path.
def key_file_path(key)
if key.size > FILEPATH_MAX_SIZE
key = Digest::MD5.hexdigest(key)
end
fname = URI.encode_www_form_component(key)
hash = Zlib.adler32(fname)
hash, dir_1 = hash.divmod(0x1000)

View file

@ -692,6 +692,11 @@ class FileStoreTest < ActiveSupport::TestCase
assert File.exist?(filepath)
end
def test_long_keys
@cache.write("a"*10000, 1)
assert_equal 1, @cache.read("a"*10000)
end
def test_key_transformation
key = @cache.send(:key_file_path, "views/index?id=1")
assert_equal "views/index?id=1", @cache.send(:file_path_key, key)