Skip to content

Instantly share code, notes, and snippets.

@rhymes
Created April 10, 2020 13:34
Show Gist options
  • Save rhymes/7bb0e840b2049a8e9de6ac9a1bc0dde4 to your computer and use it in GitHub Desktop.
Save rhymes/7bb0e840b2049a8e9de6ac9a1bc0dde4 to your computer and use it in GitHub Desktop.
Rails cache stores, increment test
require "tempfile"
cache_stores = %i[
file_store
mem_cache_store
memory_store
null_store
redis_cache_store
]
increment_values = {}
cache_stores.each do |cache_store|
store = if cache_store == :file_store
ActiveSupport::Cache.lookup_store(cache_store, Tempfile.new)
else
ActiveSupport::Cache.lookup_store(cache_store)
end
Rails.cache = store
puts "Using #{cache_store}..."
key = "key-#{rand(10_000)}"
# keeping this commented out, will show you the different behavior in cache stores
# Rails.cache.write(key, 0, raw: true)
puts "Initial value: #{Rails.cache.read(key).inspect}"
Rails.cache.increment(key, 1)
value = Rails.cache.read(key)
puts "Final value: #{value}, #{value.inspect}"
increment_values[cache_store] = value
puts
end
puts increment_values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment