Skip to content

Instantly share code, notes, and snippets.

@ssimeonov
Last active December 20, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssimeonov/6041586 to your computer and use it in GitHub Desktop.
Save ssimeonov/6041586 to your computer and use it in GitHub Desktop.
ActiveSupport::Cache::MemoryStore does not count the length of keys as it computes its size. This can lead to much more memory than expected being consumed, especially in the case where the keys are big (URLs, IP addresses, etc.) and the values are small, e.g., integers or booleans. In the example below a cache of size 100 bytes adds two entries…
2.0.0p247 :034 > cache = ActiveSupport::Cache.lookup_store :memory_store, size: 100
=> <#ActiveSupport::Cache::MemoryStore entries=0, size=0, options={:size=>100}>
2.0.0p247 :035 > key_prefix = '*' * 50
=> "**************************************************"
2.0.0p247 :036 > cache.fetch("#{key_prefix}-1") { 0 }
=> 0
2.0.0p247 :037 > cache.fetch("#{key_prefix}-2") { 0 }
=> 0
2.0.0p247 :038 > cache
=> <#ActiveSupport::Cache::MemoryStore entries=2, size=8, options={:size=>100}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment