Skip to content

Instantly share code, notes, and snippets.

@elmacnifico
Created September 19, 2011 12:52
Show Gist options
  • Save elmacnifico/1226437 to your computer and use it in GitHub Desktop.
Save elmacnifico/1226437 to your computer and use it in GitHub Desktop.
redis member stuff
class Poller
def self.call(env)
if env["PATH_INFO"] =~ /^\/pictures\/viewed/
request = Rack::Request.new(env)
params = request.params
$redis.sadd('views', [params['uuid'], params['pic_id']])
[200, {"Content-Type" => "text/json"}, ["OK"]]
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
end
end
task "process_views" do
require "./config/environment"
@view_sets = $redis.smembers('views')
$redis.del('views')
@view_sets.each do |vs|
data = JSON.parse vs
begin
#PicView.create({:uuid => data[0], :picture_id => data[1]})
ActiveRecord::Base.connection.insert("INSERT INTO pic_views (uuid, picture_id) VALUES ('#{data[0]}' , #{data[1]})")
@picture = Picture.find(data[1])
@picture.update_attribute(:views, @picture.views.to_i+1)
rescue
#puts "double.."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment