Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 4 Rails 4低级缓存不工作_Ruby On Rails 4_Caching - Fatal编程技术网

Ruby on rails 4 Rails 4低级缓存不工作

Ruby on rails 4 Rails 4低级缓存不工作,ruby-on-rails-4,caching,Ruby On Rails 4,Caching,我认为它不起作用,因为我用一个真实的db环境测试它,并且总是返回db的内容 执行Rails.cache.fetch 修改数据库 再次执行Rails.cache.fetch,这里它not应该返回我在db中修改的新值。但它确实发生了,并没有执行缓存 类转换

我认为它不起作用,因为我用一个真实的db环境测试它,并且总是返回db的内容

  • 执行
    Rails.cache.fetch
  • 修改数据库
  • 再次执行
    Rails.cache.fetch
    ,这里它not应该返回我在db中修改的新值。但它确实发生了,并没有执行缓存

    类转换
    def self.translate(es_text,locale=I18n.locale)
    
      Rails.cache.fetch("#{es_text}/#{locale}", expires_in: 1.month) do
        trad=self.find_by_es_text(es_text)
        translated=eval("trad.#{locale}_text")
        return translated if translated.present?
      end
    
    end
    
    结束

  • 测试:

    我执行
    Translation.translate('Alojamiento','en')
    并返回在DB上找到的内容:
    “acomodation”

    然后,我修改数据库表,将“acomodation”替换为“acomodation--”,然后提交

    回到Rails,执行相同的
    转换。translate('Alojamiento','en')
    并返回新值
    “acomodation----”

    但它不应该!!不是吗?因为我把
    expires\u放在:1.月
    不是
    1.秒

    或者,Rails是否知道何时修改数据库,并自动终止缓存

    我认为缓存不工作,或者我缺少一些配置

    非常感谢


    • 让“它工作”的一种方法(但我不喜欢)是在方法控制器中移动
      Rails.cache…
      code,并调用类似
      www.app/translate/Alojamiento?locale=en的url。
      在这种情况下,它可以工作,但在模型中缓存更为正确

      class ApplicationController < ActionController::Base
        ...  
        def translate
          text_return=Rails.cache.fetch("#{params[:es_text]}/#{params[:locale]}", expires_in: 1.month) do
            Translation.translate(params[:es_text],params[:locale])
          end
          render text: text_to_return
        end
      
      class ApplicationController

    解决方案将缓存结果放入一个变量中,并返回它

    似乎不一样,这是:

    def self.translate(es_text,locale=I18n.locale)
      retorn_text=Rails.cache.fetch("#{es_text}/#{locale}", expires_in: 1.month) do
        trad=self.find_by_es_text(es_text)
    
        eval("trad.#{locale}_text")
      end
    
      retorn_text 
    end
    
    除此之外:

    def self.translate(es_text,locale=I18n.locale)
      Rails.cache.fetch("#{es_text}/#{locale}", expires_in: 1.month) do
        trad=self.find_by_es_text(es_text)
    
        eval("trad.#{locale}_text")
      end
    end
    

    但我不明白为什么您在开发环境中看到了这一点?确保
    config/environments/development.rb
    没有
    config.cache\u store=:null\u store
    这将禁用缓存。不,在生产环境中,在两个不同的服务器(本地,通过rails s-e producion.和Apache生产服务器)中。我认为rails.cache.fetch有一些不同之处,如果它是在模型或控制器中执行的,则我也看到了这一点。将其存储在局部变量“retron_text”中就足够了,不需要“translated”变量。Rails 6,ruby 2.7.1