Created
January 9, 2009 06:16
-
-
Save rust/45040 to your computer and use it in GitHub Desktop.
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 'tokyotyrant' | |
class UserAccessLog | |
@@connection = {:master => nil, :slave => nil} | |
@@tt_server = { | |
:master => { | |
:host => "localhost", | |
:port => "1978", | |
}, | |
:slave => { | |
:host => "localhost", | |
:port => "1979", | |
} | |
} | |
attr_accessor :time | |
def initialize(user_id, time) | |
@user_id = user_id | |
@time = time | |
end | |
def save | |
@@connection[:master].put(Digest::SHA1.hexdigest(@user_id.to_s), time.to_s) | |
end | |
def save! | |
raise "Can't save." unless self.save | |
end | |
class << self | |
# setup connection | |
def connect | |
unless @@connection[:master] | |
@@connection[:master] = TokyoTyrant::RDB.new | |
unless @@connection[:master].open(@@tt_server[:master][:host], @@tt_server[:master][:port]) | |
raise "Can't connect ttserver. (#{@@connection[:master].ecode})" | |
end | |
end | |
unless @@connection[:slave] | |
@@connection[:slave] = TokyoTyrant::RDB.new | |
unless @@connection[:slave].open(@@tt_server[:slave][:host], @@tt_server[:slave][:port]) | |
raise "Can't connect ttserver. (#{@@connection[:slave].ecode})" | |
end | |
end | |
end | |
# find object from TokyoTyrant | |
def find(user_id) | |
raise "id does not specified." unless user_id | |
# connect to ttserver | |
connect | |
access_time = @@connection[:slave].get(Digest::SHA1.hexdigest(user_id.to_s)) | |
access_time = nil if access_time.blank? | |
self.new(user_id, (access_time ? Time.parse(access_time) : nil)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment