简体   繁体   English

Ruby on Rails - 缓存变量

[英]Ruby on Rails - Caching Variables

I have these two variables being set in the controller. 我在控制器中设置了这两个变量。 How do I cache these so they don't communicate with the database each time, only the first time. 如何缓存这些,以便它们不会每次都与数据库通信,而只是第一次。

@tablenutrients = Nutrient.find(:all)
@columnnutrients = @tablenutrients.collect {|x| x.nutrient}

what @djlumley said. 什么@djlumley说。

In general, you can also configure and use ActiveSupport::Cache::Store to explicitly store your own custom variables. 通常,您还可以配置和使用ActiveSupport :: Cache :: Store来显式存储您自己的自定义变量。 You could then get/set cached values, for example, like this: 然后,您可以获取/设置缓存值,例如,如下所示:

@tablenutrients = Rails.cache.fetch("tablenutrients") do 
  Nutrient.find(:all)
end

If your database is setup correctly, it should be caching your data by default. 如果您的数据库设置正确,则默认情况下应该缓存您的数据。 If you're using MySQL or postgresql you can change the amoutn of RAM the cache uses to ensure you're getting a high cache hit rate. 如果您正在使用MySQL或postgresql,您可以更改缓存使用的RAM的大小,以确保您获得高缓存命中率。

Beyond simple database caching, using something like Dalli to connect to memcached should make improving performance fairly easy. 除了简单的数据库缓存之外,使用像Dalli这样的东西连接到memcached应​​该可以很容易地提高性能。

Rails should take advantage of memcached to cache all your active record queries in memcached provided it's setup correctly. 如果正确设置,Rails应该利用memcached来缓存memcached中的所有活动记录查询。 The Rails guide on caching together with the Dalli documentation should help you get started based on the version of Rails you're running. 有关缓存Rails指南以及Dalli文档应该可以帮助您根据正在运行的Rails版本开始。

Rails has a few built in caching options for you, two of which would likely work for you, depending on what you're doing with the query result: Rails有一些内置的缓存选项,其中两个可能适合您,具体取决于您对查询结果的处理方式:

Fragment Caching 片段缓存

If you were using this as a collection for a select box an often used form I would go with this option. 如果您使用此作为选择框的集合,这是一个经常使用的表单,我将使用此选项。 This would allow you to cache not only the database result, but the actual HTML section of the page . 这将允许您不仅缓存数据库结果,还缓存页面的实际HTML部分。 This is done simply by throwing a <%= cache do %> around the section, like so: 这只需在部分周围抛出<%= cache do %>完成,如下所示:

<html>
   ...
   <body>
     ...
     <div class='content'>
       ...
       <%= cache do %>
         <%= select "nutrient", "nutrient", Nutrient.all.collect(&:nutrient) } %>
       <% end %>

Rail.cache Rail.cache

You could also write a method to talk directly to the built in cache store, by dropping a method in your ApplicationController and then have it run on a before_filter callback like so: 您还可以编写一个方法直接与内置缓存存储进行对话,方法是在ApplicationController中删除一个方法,然后让它在before_filter回调上运行,如下所示:

application_controller.rb application_controller.rb

class ApplicationController < ActionController::Base
  before_filter :retrieve_nutrients

  def retrieve_nutrients
    @nutrients = Rails.cache.fetch("nutrients") do
      Nutrient.all.collect(&:nutrient)
    end
  end
end

In both cases in a production environment you're going to want to setup either Memcached or Redis to act as a caching layer (they sit behind Rails.cache and are a snap to implement). 在生产环境中的两种情况下,您都希望设置Memcached或Redis作为缓存层(它们位于Rails.cache后面并且很容易实现)。 I would checkout Rails Guides for a deeper look at it. 我会查看Rails指南以深入了解它。

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

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