用于application.rb的rails缓存

时间:2010-08-20 07:31:04

标签: ruby-on-rails ruby caching

我有一个查询可以提取我想要放在应用程序每个页面页脚的统计信息。很自然我把它放在application.rb中,但是有问题的统计数据一整天都没有改变......

基本上在页脚中会说:

目前,有46个国家,75个城市和20,000个地点。

但是我想缓存这个查询,这样它不会减慢我的应用程序速度,它只需要每天更新一次。我该怎么做?

@stat = Location.find(:all, :select => 'COUNT(locations.id) AS locations, COUNT(DISTINCT(city)) AS cities, COUNT(DISTINCT(countries.name)) AS countries', :joins => [ :places, :country ], :conditions => [ 'email IS NOT NULL' ]).first

1 个答案:

答案 0 :(得分:0)

尝试:

@stat ||= Location.find(:all, :select => 'COUNT(locations.id) AS locations, 
                               COUNT(DISTINCT(city)) AS cities, 
                               COUNT(DISTINCT(countries.name)) AS countries', 
                              :joins => [ :places, :country ], 
                              :conditions => [ 'email IS NOT NULL' ]).first

之后,根据时间或其他一些事件使@stat变量无效,如下所示:

@stat = nil

您也可以使用fragment caching完成此操作。