Last active
August 29, 2015 14:02
-
-
Save billie66/8a8c1f626521d0552f7e to your computer and use it in GitHub Desktop.
store data with redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'redis' | |
redis = Redis.new | |
names = [] | |
keys = redis.keys("connect*:*") | |
if !keys.empty? | |
redis.del(*keys) | |
end | |
if redis.exists("connect_id") | |
redis.del("connect_id") | |
end | |
3.times do |i| | |
puts "plz input a name: " | |
name = gets.chomp() | |
puts "input online time: " | |
t1 = gets.chomp() | |
puts "input offline time: " | |
t2 = gets.chomp() | |
names << name | |
id = redis.incr("connect_id") | |
redis.hset("connect:#{id}", "online", t1) | |
redis.hset("connect:#{id}", "offline", t2) | |
redis.rpush("connects:#{name}", "connect:#{id}") | |
end | |
names.uniq.each do |name| | |
keys = redis.lrange("connects:#{name}", 0, -1) | |
total = 0 | |
keys.each do |key| | |
h = redis.hgetall(key) | |
total += h["offline"].to_i - h["online"].to_i | |
end | |
puts "#{name} total connect time #{total}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment