简体   繁体   English

Rails 3.1记录计数器主页

[英]Rails 3.1 Record Counter for Homepage

I had a quick question that's been bugging me. 我有一个快速的问题一直困扰着我。 I'd like to place a counter on the homepage of my Rails 3.1.rc4 site to advertise how many micro-blogging posts are in the site's database. 我想在Rails 3.1.rc4网站的主页上放置一个计数器,以宣传该网站的数据库中有多少个微博客帖子。 The problem is this number is going to grow to be huge over time and I don't want to have to use something like Post.find(:all).count . 问题在于,随着时间的流逝,这个数字将变得越来越大,我不想使用诸如Post.find(:all).count东西。 So this is where my question lies: is there an easy, cache-able (for let's say 8 hours), and non-resource intensive way to display a count of records in a database? 所以这就是我的问题所在:是否存在一种简单,可缓存(例如8个小时)且非资源密集型的方式来显示数据库中的记录数? I'm using ActiveRecord and Heroku hosting (Cedar Stack with no Varnish cache). 我正在使用ActiveRecord和Heroku托管(没有Varnish缓存的Cedar Stack)。

Thanks for your help! 谢谢你的帮助!

Yeah, use Post.count and if you want to cache the result for 8 hours, do: 是的,请使用Post.count ,如果要将结果缓存8小时,请执行以下操作:

Rails.cache.fetch(:posts_count, :expires_in => 8.hours) do
  Post.count
end

Just use Post.count which does select count(id) from posts; 只需使用Post.count select count(id) from posts;select count(id) from posts; in SQL. 在SQL中。

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

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