Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails缓存未过期_Ruby On Rails_Caching_Memcached_Dalli - Fatal编程技术网

Ruby on rails Rails缓存未过期

Ruby on rails Rails缓存未过期,ruby-on-rails,caching,memcached,dalli,Ruby On Rails,Caching,Memcached,Dalli,出于某种原因,以下内容在10秒后不会过期 def rcache_value @random_val_from_cache = Rails.cache.fetch("random_val_from_cache",:expires_in=>10.seconds) do rand 10000 end 我在我的应用程序的页脚中从缓存中输出@random\u val\u。我的参数错了吗?我在Rails 3上。好吧,假设您的示例中的语法错误只是一个输入错误,那么问题很可能是因为您使用的是实例

出于某种原因,以下内容在10秒后不会过期

def rcache_value
  @random_val_from_cache = Rails.cache.fetch("random_val_from_cache",:expires_in=>10.seconds) do
  rand 10000
end

我在我的应用程序的页脚中从缓存中输出
@random\u val\u。我的参数错了吗?我在Rails 3上。

好吧,假设您的示例中的语法错误只是一个输入错误,那么问题很可能是因为您使用的是实例变量。缓存的值设置为该实例变量,除非手动重置,或者除非重新加载该类(在服务器重新启动之前不会发生),否则不会重置该实例变量。你应该这样做:

def rcache_value
  Rails.cache.fetch("random_val_from_cache", :expires_in => 10.seconds) do
    rand 10000
  end
end
并称之为:

<%= rcache_value %>


找到了答案。expires_in在
fetch
中被弃用,而倾向于将其设置为write。所以,您必须写一个检查,看看它是否已经存在于缓存中,如果不存在,则执行写操作,这完全取决于这是一个什么样的实例变量,所以我如何将实例变量设置为缓存值?另外,我的示例中有什么语法错误?第三,我的视图无法访问控制器中的方法。。。