简体   繁体   English

Rails缓存在服务器上不起作用

[英]Rails caching not working on server

In my application, I have used caching. 在我的应用程序中,我使用了缓存。 This is the code, I have used. 这是我使用的代码。 In after_filter, I called the method which include this one line code. 在after_filter中,我调用了包含这一行代码的方法。

Rails.cache.write("properties", @properties.to_xml)

I try to get this in another action in before_filter like 我试图在before_filter的另一个动作中得到这个

@hotels = Rails.cache.fetch("properties")

this all working fine in development machine. 这一切在开发机器上都能正常工作。 But in server it returns null value. 但是在服务器中,它返回空值。 the application run in same development mode in server. 该应用程序在服务器中以相同的开发模式运行。 Can you please anyone suggest me the right way. 能否请任何人以正确的方式建议我。 Thanks in advance. 提前致谢。

It sounds like you haven't configured a backend for the store, so it will use ActiveSupport::Cache::MemoryStore 听起来您尚未为存储配置后端,因此它将使用ActiveSupport :: Cache :: MemoryStore

From the documentation: 从文档中:

If you're running multiple Ruby on Rails server processes (which is the case if you're using mongrel_cluster or Phusion Passenger), then this means that Rails server process instances won't be able to share cache data with each other. 如果您正在运行多个Ruby on Rails服务器进程(在使用mongrel_cluster或Phusion Passenger的情况下就是这种情况),那么这意味着Rails服务器进程实例将无法彼此共享缓存数据。

This works in development since you are likely using a single server instance, so the cache is only stored in one process. 这在开发中可行,因为您可能使用的是单个服务器实例,因此缓存仅存储在一个进程中。 For production you need to configure an alternative shared store. 对于生产,您需要配置备用共享存储。 I'd recommend running a memcached instance, and installing and using the Dalli Gem as per the README . 我建议运行一个memcached实例,并根据README安装和使用Dalli Gem

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

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