Created
January 9, 2009 04:32
-
-
Save rust/45021 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
class User < ActiveRecord::Base | |
def access_time=(time = Time.now) | |
raise "#{self.class} isn't saved." unless id | |
access_log = UserAccessLog.find(id) | |
access_log.time = time | |
access_log.save | |
end | |
def access_time | |
return nil unless id | |
access_log = UserAccessLog.find(id) | |
access_log.time | |
end | |
end |
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 = nil | |
@@tt_server = { | |
:host => "localhost", | |
:port => "1978", | |
} | |
attr_accessor :time | |
def initialize(user_id, time) | |
@user_id = user_id | |
@time = time | |
end | |
def save | |
@@connection.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 | |
@@connection = TokyoTyrant::RDB.new | |
unless @@connection.open(@@tt_server[:host], @@tt_server[:port]) | |
raise "Can't connect ttserver. (#{@@connection.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.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