First, I'd like to show you some methods from official document.
ActiveSupport::Cache::DalliStore#with_local_cache
def with_local_cache
use_temporary_local_cache(LocalStore.new) { yield }
end
def use_temporary_local_cache(temporary_cache)
save_cache = LocalCacheRegistry.cache_for(local_cache_key)
begin
LocalCacheRegistry.set_cache_for(local_cache_key, temporary_cache)
yield
ensure
LocalCacheRegistry.set_cache_for(local_cache_key, save_cache)
end
end
def write(name, value, options=nil)
options ||= {}
name = expanded_key name
instrument(:write, name, options) do |payload|
with do |connection|
options = options.merge(:connection => connection)
write_entry(name, value, options)
end
end
end
def write_entry(key, entry, options) # :nodoc:
local_cache.write_entry(key, entry, options) if local_cache
super
end
And now, we use them in practice.
@cache = Rails.cache
@cache.with_local_cache do
@cache.write('key', 'value')
end
If you combine the code above, you'll find that there should have two place which cached ('key', 'value')
pairs. One is LocalCache
, another is our @cache
. It's purpose is declared in source code clearly.
Caches that implement LocalCache will be backed by an in-memory cache for the
duration of a block. Repeated calls to the cache for the same key will hit the
in-memory cache for faster access.
I hope this will do help for you!