Reputation: 7810
I am using memcached (gem memcache-client
) in my application, but I want to use the memory cache store at the same time. I currently have the following in my application.rb
:
config.cache_store = :mem_cache_store, {:namespace => "my_namespace_#{Rails.env}"}
and this works fine with commands such as Rails.cache.write(...)
How can I use the ActiveSupport::Cache::MemoryStore
for some of my models, and at the same time use memcached for other reasons as I do until now?
I know that ActiveSupport::Cache.lookup_store
will return to me a new MemoryStore
object. However, I do not know how to continue from this point on? For example, where do I put this command? Where do I store the resulting object? How do I access this object later on from inside my model code? Or shall I follow a completely different way?
Thanks in advance for your help.
Upvotes: 0
Views: 2045
Reputation: 84114
Rails.cache
is just a cache store that is created for your convenience. There's nothing stopping you doing something like
::MEMORY_STORE = ActiveSupport::Cache::MemoryStore.new
and then when you want to use that store instead of Rails.cache
you would do
MEMORY_STORE.fetch('some_key') {}
Although, as @leonardoborges commented, I'm not sure why you would want to do this
Upvotes: 2