简体   繁体   English

rails缓存的application.rb

[英]rails caching for application.rb

I have a query that pulls stats which I would like to place on the footer of every page of my application. 我有一个查询,它提取要放在应用程序每一页页脚的统计信息。 So natural I'm putting it in the application.rb, but the statistics in question don't change throughout the day generally... 我很自然地将其放在application.rb中,但是有问题的统计信息通常在一整天内都不会改变...

Basically in the footer it will say: 基本上在页脚中会说:

Currently, 46 Countries, 75 Cities, and 20,000 locations. 目前,在46个国家/地区,75个城市和20,000个位置。

But I would like to cache this query so it doesn't slow down my app, it only needs to update maybe once per day. 但我想缓存此查询,以免降低我的应用程序的速度,它可能只需要每天更新一次。 How would I do this? 我该怎么做?

@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

Try: 尝试:

@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

After that, invalidate the @stat variable on the time basis, or by some other events, like this: 之后,请按时间或通过其他事件使@stat变量无效,如下所示:

@stat = nil

You can also accomplish that by using fragment caching . 您也可以通过使用片段缓存来实现。

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

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