简体   繁体   English

缓存activerecord结果就像片段一样

[英]caching activerecord results just like fragments

In my views I do a lot of this: 在我看来,我做了很多这样的事情:

<% cache("sports_menu_" +session[:lang], {:expires_in => 60.minutes}) do %>
...... # a load of stuff
<% end %>

However I've discovered a lot of time is spent querying the DB for data that doesn't change that often. 但是我发现花了很多时间来查询数据库中经常没有改变的数据。 Is there a way to cache this data in a similar manner? 有没有办法以类似的方式缓存这些数据?

For instance: 例如:

Model.find(:all, :select => "only a few fields", :conditions => "nasty conditions", :include => "some joins", :order => "date_time desc") 

This takes about 7 seconds, the main table keeps about 20M records. 这需要大约7秒钟,主表保持大约20M的记录。 A lot of users hit this particular action and the query only runs once/hit. 很多用户点击了这个特定的操作,查询只运行一次/命中。 But it would make sense caching that for a number of minutes so that for everyone else it will load from the cache. 但是对于其他所有人来说,从高速缓存中加载它会有意义的缓存。 I'm using memcache by the way. 顺便说一句,我正在使用memcache。

I can't cache the entire action because there are some parameters that change on occasion and some locale-specific code within the view. 我无法缓存整个操作,因为有些参数有时会更改,而视图中会出现一些特定于语言环境的代码。

I have considered moving that to the view level but don't feel too comfortable about that, it would kind of defeat the point of using Rails. 我已经考虑过将其移到视图级别但是对此感觉不太舒服,这会使得使用Rails失败。

TIA! TIA!

It looks to me like ActiveRecord::Cache::Store is what you want. 在我看来,像ActiveRecord :: Cache :: Store就是你想要的。 You can use it like this . 你可以像这样使用它。

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

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