JQuery Redis hit a cached Rails pageview tracking counter

I have static cached pages that I want to track and then sort by popularity.

What would be the best way to track these views in Redis and then load them back into the main database?

Now I'm thinking about using jquery like this

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

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

 redis.incr "1234" 

Once a day, I ran cron for something like

 Pages.each do |p| p.hits = redis.get(p.id.to_s) end 
+4
source share
2 answers

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

 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 
+2
source

What you do is a good way to do this, but you can also use the server log to get the number of hits on this page. Using the server’s log allows you not to add code to each page and will allow you to track unique hits, not just hits. IF you're into Redis, then your path is good.

0
source

Source: https://habr.com/ru/post/1337083/


All Articles