diff options
author | Simeon Simeonov <sim@fastignite.com> | 2013-07-20 23:08:06 -0400 |
---|---|---|
committer | Simeon Simeonov <sim@fastignite.com> | 2013-07-22 09:53:03 -0400 |
commit | 51d9b9a821a8f3f11fc5f52321df6ee05e4e1327 (patch) | |
tree | 12e94feb9cb5a5e08a94003994fd12530d977fd6 /activesupport/test | |
parent | eda66d89c74cd573f5c0f24877fe9bf3c6a338ba (diff) | |
download | rails-51d9b9a821a8f3f11fc5f52321df6ee05e4e1327.tar.gz rails-51d9b9a821a8f3f11fc5f52321df6ee05e4e1327.tar.bz2 rails-51d9b9a821a8f3f11fc5f52321df6ee05e4e1327.zip |
[Fixes #11512] improves cache size calculation in ActiveSupport::Cache::MemoryStore
Previously, the cache size of `ActiveSupport::Cache::MemoryStore` was calculated
as the sum of the size of its entries, ignoring the size of keys and any data
structure overhead. This could lead to the calculated cache size sometimes being
10-100x smaller than the memory used, e.g., in the case of small values.
The size of a key/entry pair is now calculated via `#cached_size`:
def cached_size(key, entry)
key.to_s.bytesize + entry.size + PER_ENTRY_OVERHEAD
end
The value of `PER_ENTRY_OVERHEAD` is 240 bytes based on an [empirical
estimation](https://gist.github.com/ssimeonov/6047200) for 64-bit MRI on
1.9.3 and 2.0.
Fixes GH#11512 https://github.com/rails/rails/issues/11512
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/caching_test.rb | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index d2382f0f5b..df570d485a 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -713,8 +713,8 @@ end class MemoryStoreTest < ActiveSupport::TestCase def setup - @record_size = ActiveSupport::Cache::Entry.new("aaaaaaaaaa").size - @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => @record_size * 10) + @record_size = ActiveSupport::Cache.lookup_store(:memory_store).send(:cached_size, 1, ActiveSupport::Cache::Entry.new("aaaaaaaaaa")) + @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => @record_size * 10 + 1) end include CacheStoreBehavior @@ -764,6 +764,30 @@ class MemoryStoreTest < ActiveSupport::TestCase assert !@cache.exist?(1), "no entry" end + def test_prune_size_on_write_based_on_key_length + @cache.write(1, "aaaaaaaaaa") && sleep(0.001) + @cache.write(2, "bbbbbbbbbb") && sleep(0.001) + @cache.write(3, "cccccccccc") && sleep(0.001) + @cache.write(4, "dddddddddd") && sleep(0.001) + @cache.write(5, "eeeeeeeeee") && sleep(0.001) + @cache.write(6, "ffffffffff") && sleep(0.001) + @cache.write(7, "gggggggggg") && sleep(0.001) + @cache.write(8, "hhhhhhhhhh") && sleep(0.001) + @cache.write(9, "iiiiiiiiii") && sleep(0.001) + long_key = '*' * 2 * @record_size + @cache.write(long_key, "llllllllll") + assert @cache.exist?(long_key) + assert @cache.exist?(9) + assert @cache.exist?(8) + assert @cache.exist?(7) + assert @cache.exist?(6) + assert !@cache.exist?(5), "no entry" + assert !@cache.exist?(4), "no entry" + assert !@cache.exist?(3), "no entry" + assert !@cache.exist?(2), "no entry" + assert !@cache.exist?(1), "no entry" + end + def test_pruning_is_capped_at_a_max_time def @cache.delete_entry (*args) sleep(0.01) |