jQuery Redis计数器跟踪缓存的Rails页面的视图 [英] jQuery Redis hit counter to track view of cached Rails pages

查看:78
本文介绍了jQuery Redis计数器跟踪缓存的Rails页面的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有静态的缓存页面,我想跟踪它们的点击率,然后按受欢迎程度排序.

I have static cached pages that I want to track the hits of and then order by popularity.

在Redis中跟踪这些视图然后将它们重新加载到主数据库中的最佳方法是什么?

What would be the best way of tracking these views in Redis and then loading them back into the main database?

现在我正在考虑使用这样的jQuery

Right now I am thinking of using jquery like this

$.get("/track/", { id: "1234" } );

并在跟踪"控制器中使用redis gem进行调用

and using the redis gem in a "track" controller to call

redis.incr "1234"

每天我都会像这样

Pages.each do |p|
   p.hits = redis.get(p.id.to_s)
end

推荐答案

这是我最后的耙任务,以防万一它可以帮助任何人

Here is my final rake task, just in case it helps anyone

require 'redis'
task :cron => :environment do
   uri = URI.parse(REDIS_URL)
   REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

   Pages.all.each do |p|
      views = REDIS.get(p.id.to_s)
      if views
          p.hits = p.hits + views.to_i

          if p.save
             REDIS.set pId, "0"
          end
       end
   end
 end

这篇关于jQuery Redis计数器跟踪缓存的Rails页面的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆