diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2013-07-22 08:05:07 -0700 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2013-07-22 08:05:07 -0700 |
commit | 782d2f6f83c39e9cfd2a8e650250a74c65458f88 (patch) | |
tree | d40e9d915860e8a8a53cdd5c7560764e9db7af4c /activesupport/lib/active_support/cache | |
parent | ecd70039ba191371f3735d7a6336e4cfc6be83df (diff) | |
parent | 51d9b9a821a8f3f11fc5f52321df6ee05e4e1327 (diff) | |
download | rails-782d2f6f83c39e9cfd2a8e650250a74c65458f88.tar.gz rails-782d2f6f83c39e9cfd2a8e650250a74c65458f88.tar.bz2 rails-782d2f6f83c39e9cfd2a8e650250a74c65458f88.zip |
Merge pull request #11546 from swoop-inc/ss_memory_store_cache_size
[Fixes #11512] improves cache size calculation in MemoryStore
Diffstat (limited to 'activesupport/lib/active_support/cache')
-rw-r--r-- | activesupport/lib/active_support/cache/memory_store.rb | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/cache/memory_store.rb b/activesupport/lib/active_support/cache/memory_store.rb index e58b7be9f8..b979521c99 100644 --- a/activesupport/lib/active_support/cache/memory_store.rb +++ b/activesupport/lib/active_support/cache/memory_store.rb @@ -123,6 +123,14 @@ module ActiveSupport end protected + + # See https://gist.github.com/ssimeonov/6047200 + PER_ENTRY_OVERHEAD = 240 + + def cached_size(key, entry) + key.to_s.bytesize + entry.size + PER_ENTRY_OVERHEAD + end + def read_entry(key, options) # :nodoc: entry = @data[key] synchronize do @@ -140,8 +148,11 @@ module ActiveSupport synchronize do old_entry = @data[key] return false if @data.key?(key) && options[:unless_exist] - @cache_size -= old_entry.size if old_entry - @cache_size += entry.size + if old_entry + @cache_size -= (old_entry.size - entry.size) + else + @cache_size += cached_size(key, entry) + end @key_access[key] = Time.now.to_f @data[key] = entry prune(@max_size * 0.75, @max_prune_time) if @cache_size > @max_size @@ -153,7 +164,7 @@ module ActiveSupport synchronize do @key_access.delete(key) entry = @data.delete(key) - @cache_size -= entry.size if entry + @cache_size -= cached_size(key, entry) if entry !!entry end end |