简体   繁体   English

如何同时使用Memcache和Ruby On Rails默认内存存储?

[英]How to use Memcache and Ruby On Rails default memory store at the same time?

I am using memcached (gem memcache-client ) in my application, but I want to use the memory cache store at the same time. 我在我的应用程序中使用memcached(gem memcache-client ),但我想同时使用内存缓存存储。 I currently have the following in my application.rb : 我目前在我的application.rb有以下内容:

config.cache_store = :mem_cache_store, {:namespace => "my_namespace_#{Rails.env}"}

and this works fine with commands such as Rails.cache.write(...) 这适用于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? 我怎样才能为我的某些模型使用ActiveSupport::Cache::MemoryStore ,同时出于其他原因使用memcached,直到现在呢?

I know that ActiveSupport::Cache.lookup_store will return to me a new MemoryStore object. 我知道ActiveSupport::Cache.lookup_store会给我一个新的MemoryStore对象。 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. 在此先感谢您的帮助。

Rails.cache is just a cache store that is created for your convenience. Rails.cache只是为方便起见而创建的缓存存储。 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 然后当你想使用那个商店而不是Rails.cache你会这样做

MEMORY_STORE.fetch('some_key') {}

Although, as @leonardoborges commented, I'm not sure why you would want to do this 虽然,正如@leonardoborges评论的那样,我不确定你为什么要这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM