lib/redic/client.rb in redic-0.0.5 vs lib/redic/client.rb in redic-0.0.6

- old
+ new

@@ -3,10 +3,11 @@ class Redic class Client def initialize(url) @uri = URI.parse(url) + @ttl = Integer(ENV.fetch("REDIC_TTL", 60)) @connection = nil @semaphore = Mutex.new end def read @@ -17,10 +18,11 @@ @connection.write(command) end def connect establish_connection unless connected? + timestamp_connection @semaphore.synchronize do yield end end @@ -34,19 +36,27 @@ end authenticate end + def timestamp_connection + @timestamp = Time.now.to_i + end + def authenticate if @uri.password @semaphore.synchronize do write [:auth, @uri.password] read end end end def connected? - @connection && @connection.connected? + @connection && @connection.connected? && alive? + end + + def alive? + Time.now.to_i - @timestamp < @ttl end end end