Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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 是否缓存类方法的结果集?_Ruby On Rails_Caching_Rspec - Fatal编程技术网

Ruby on rails 是否缓存类方法的结果集?

Ruby on rails 是否缓存类方法的结果集?,ruby-on-rails,caching,rspec,Ruby On Rails,Caching,Rspec,在我的一个模型中,我有一个非常密集的db类方法: class Page < ApplicationRecord def self.collection_tree pages = [] walk_tree do |page, level| pages << page end pages end end 类页

在我的一个模型中,我有一个非常密集的db类方法:

class Page < ApplicationRecord
  def self.collection_tree
    pages = []
    walk_tree do |page, level|
      pages << page
    end
    pages
  end
end
类页页面直接使用缓存与

def self.collection_树
Rails.cache.fetch('collection_tree')do
集合树=[]
步行树do页面,标高|
集合树
def self.collection_tree
  return @collection_tree if @collection_tree

  @collection_tree = []
  walk_tree do |page, level|
    @collection_tree << page
  end
  @collection_tree
end
def self.collection_tree
  Rails.cache.fetch('collection_tree') do    
    collection_tree = []
    walk_tree do |page, level|
      collection_tree << page
    end
    collection_tree
  end
end