mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-23 00:28:26 -05:00
Ruby client updated
This commit is contained in:
parent
928394cd12
commit
9495122b18
@ -15,6 +15,7 @@ it's possible to grab the most recent version:
|
|||||||
|
|
||||||
Ruby lib source code:
|
Ruby lib source code:
|
||||||
http://github.com/ezmobius/redis-rb/tree/master
|
http://github.com/ezmobius/redis-rb/tree/master
|
||||||
|
git://github.com/ezmobius/redis-rb.git
|
||||||
|
|
||||||
Erlang lib source code:
|
Erlang lib source code:
|
||||||
http://bitbucket.org/adroll/erldis/
|
http://bitbucket.org/adroll/erldis/
|
||||||
|
@ -1,191 +0,0 @@
|
|||||||
#--
|
|
||||||
# = timeout.rb
|
|
||||||
#
|
|
||||||
# execution timeout
|
|
||||||
#
|
|
||||||
# = Copyright
|
|
||||||
#
|
|
||||||
# Copyright - (C) 2008 Evan Phoenix
|
|
||||||
# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
|
|
||||||
# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
|
|
||||||
#
|
|
||||||
#++
|
|
||||||
#
|
|
||||||
# = Description
|
|
||||||
#
|
|
||||||
# A way of performing a potentially long-running operation in a thread, and
|
|
||||||
# terminating it's execution if it hasn't finished within fixed amount of
|
|
||||||
# time.
|
|
||||||
#
|
|
||||||
# Previous versions of timeout didn't use a module for namespace. This version
|
|
||||||
# provides both Timeout.timeout, and a backwards-compatible #timeout.
|
|
||||||
#
|
|
||||||
# = Synopsis
|
|
||||||
#
|
|
||||||
# require 'timeout'
|
|
||||||
# status = Timeout::timeout(5) {
|
|
||||||
# # Something that should be interrupted if it takes too much time...
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
|
|
||||||
require 'thread'
|
|
||||||
|
|
||||||
module Timeout
|
|
||||||
|
|
||||||
##
|
|
||||||
# Raised by Timeout#timeout when the block times out.
|
|
||||||
|
|
||||||
class Error<Interrupt
|
|
||||||
end
|
|
||||||
|
|
||||||
# A mutex to protect @requests
|
|
||||||
@mutex = Mutex.new
|
|
||||||
|
|
||||||
# All the outstanding TimeoutRequests
|
|
||||||
@requests = []
|
|
||||||
|
|
||||||
# Represents +thr+ asking for it to be timeout at in +secs+
|
|
||||||
# seconds. At timeout, raise +exc+.
|
|
||||||
class TimeoutRequest
|
|
||||||
def initialize(secs, thr, exc)
|
|
||||||
@left = secs
|
|
||||||
@thread = thr
|
|
||||||
@exception = exc
|
|
||||||
end
|
|
||||||
|
|
||||||
attr_reader :thread, :left
|
|
||||||
|
|
||||||
# Called because +time+ seconds have gone by. Returns
|
|
||||||
# true if the request has no more time left to run.
|
|
||||||
def elapsed(time)
|
|
||||||
@left -= time
|
|
||||||
@left <= 0
|
|
||||||
end
|
|
||||||
|
|
||||||
# Raise @exception if @thread.
|
|
||||||
def cancel
|
|
||||||
if @thread and @thread.alive?
|
|
||||||
@thread.raise @exception, "execution expired"
|
|
||||||
end
|
|
||||||
|
|
||||||
@left = 0
|
|
||||||
end
|
|
||||||
|
|
||||||
# Abort this request, ie, we don't care about tracking
|
|
||||||
# the thread anymore.
|
|
||||||
def abort
|
|
||||||
@thread = nil
|
|
||||||
@left = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.add_timeout(time, exc)
|
|
||||||
|
|
||||||
@controller ||= Thread.new do
|
|
||||||
while true
|
|
||||||
if @requests.empty?
|
|
||||||
sleep
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
min = nil
|
|
||||||
|
|
||||||
@mutex.synchronize do
|
|
||||||
min = @requests.min { |a,b| a.left <=> b.left }
|
|
||||||
end
|
|
||||||
|
|
||||||
slept_for = sleep(min.left)
|
|
||||||
|
|
||||||
@mutex.synchronize do
|
|
||||||
@requests.delete_if do |r|
|
|
||||||
if r.elapsed(slept_for)
|
|
||||||
r.cancel
|
|
||||||
true
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
req = TimeoutRequest.new(time, Thread.current, exc)
|
|
||||||
|
|
||||||
@mutex.synchronize do
|
|
||||||
@requests << req
|
|
||||||
end
|
|
||||||
|
|
||||||
@controller.run
|
|
||||||
|
|
||||||
return req
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Executes the method's block. If the block execution terminates before +sec+
|
|
||||||
# seconds has passed, it returns true. If not, it terminates the execution
|
|
||||||
# and raises +exception+ (which defaults to Timeout::Error).
|
|
||||||
#
|
|
||||||
# Note that this is both a method of module Timeout, so you can 'include
|
|
||||||
# Timeout' into your classes so they have a #timeout method, as well as a
|
|
||||||
# module method, so you can call it directly as Timeout.timeout().
|
|
||||||
|
|
||||||
def timeout(sec, exception=Error)
|
|
||||||
return yield if sec == nil or sec.zero?
|
|
||||||
raise ThreadError, "timeout within critical session" if Thread.critical
|
|
||||||
|
|
||||||
req = Timeout.add_timeout sec, exception
|
|
||||||
|
|
||||||
begin
|
|
||||||
yield sec
|
|
||||||
ensure
|
|
||||||
req.abort
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module_function :timeout
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Identical to:
|
|
||||||
#
|
|
||||||
# Timeout::timeout(n, e, &block).
|
|
||||||
#
|
|
||||||
# Defined for backwards compatibility with earlier versions of timeout.rb, see
|
|
||||||
# Timeout#timeout.
|
|
||||||
|
|
||||||
def timeout(n, e=Timeout::Error, &block) # :nodoc:
|
|
||||||
Timeout::timeout(n, e, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Another name for Timeout::Error, defined for backwards compatibility with
|
|
||||||
# earlier versions of timeout.rb.
|
|
||||||
|
|
||||||
class Object
|
|
||||||
remove_const(:TimeoutError) if const_defined?(:TimeoutError)
|
|
||||||
end
|
|
||||||
TimeoutError = Timeout::Error # :nodoc:
|
|
||||||
|
|
||||||
if __FILE__ == $0
|
|
||||||
p timeout(5) {
|
|
||||||
45
|
|
||||||
}
|
|
||||||
p timeout(5, TimeoutError) {
|
|
||||||
45
|
|
||||||
}
|
|
||||||
p timeout(nil) {
|
|
||||||
54
|
|
||||||
}
|
|
||||||
p timeout(0) {
|
|
||||||
54
|
|
||||||
}
|
|
||||||
p timeout(5) {
|
|
||||||
loop {
|
|
||||||
p 10
|
|
||||||
sleep 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
@ -19,8 +19,9 @@ class Redis
|
|||||||
|
|
||||||
|
|
||||||
def initialize(opts={})
|
def initialize(opts={})
|
||||||
@opts = {:host => 'localhost', :port => '6379'}.merge(opts)
|
@opts = {:host => 'localhost', :port => '6379', :db => 0}.merge(opts)
|
||||||
$debug = @opts[:debug]
|
$debug = @opts[:debug]
|
||||||
|
@db = @opts[:db]
|
||||||
@server = Server.new(@opts[:host], @opts[:port])
|
@server = Server.new(@opts[:host], @opts[:port])
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -47,7 +48,21 @@ class Redis
|
|||||||
#Server down
|
#Server down
|
||||||
rescue NoMethodError => e
|
rescue NoMethodError => e
|
||||||
puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
|
puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
|
||||||
exit
|
raise Errno::ECONNREFUSED
|
||||||
|
#exit
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def monitor
|
||||||
|
with_socket_management(@server) do |socket|
|
||||||
|
trap("INT") { puts "\nGot ^C! Dying!"; exit }
|
||||||
|
write "MONITOR\r\n"
|
||||||
|
puts "Now Monitoring..."
|
||||||
|
socket.read(12)
|
||||||
|
loop do
|
||||||
|
x = socket.gets
|
||||||
|
puts x unless x.nil?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -56,6 +71,7 @@ class Redis
|
|||||||
end
|
end
|
||||||
|
|
||||||
def select_db(index)
|
def select_db(index)
|
||||||
|
@db = index
|
||||||
write "SELECT #{index}\r\n"
|
write "SELECT #{index}\r\n"
|
||||||
get_response
|
get_response
|
||||||
end
|
end
|
||||||
@ -65,6 +81,16 @@ class Redis
|
|||||||
get_response == OK
|
get_response == OK
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def flush_all
|
||||||
|
ensure_retry do
|
||||||
|
puts "Warning!\nFlushing *ALL* databases!\n5 Seconds to Hit ^C!"
|
||||||
|
trap('INT') {quit; return false}
|
||||||
|
sleep 5
|
||||||
|
write "FLUSHALL\r\n"
|
||||||
|
get_response == OK
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def last_save
|
def last_save
|
||||||
write "LASTSAVE\r\n"
|
write "LASTSAVE\r\n"
|
||||||
get_response.to_i
|
get_response.to_i
|
||||||
@ -79,7 +105,7 @@ class Redis
|
|||||||
info = {}
|
info = {}
|
||||||
write("INFO\r\n")
|
write("INFO\r\n")
|
||||||
x = get_response
|
x = get_response
|
||||||
x.each_line do |kv|
|
x.each do |kv|
|
||||||
k,v = kv.split(':', 2)
|
k,v = kv.split(':', 2)
|
||||||
k,v = k.chomp, v = v.chomp
|
k,v = k.chomp, v = v.chomp
|
||||||
info[k.to_sym] = v
|
info[k.to_sym] = v
|
||||||
@ -182,7 +208,7 @@ class Redis
|
|||||||
|
|
||||||
def decr(key, decrement=nil)
|
def decr(key, decrement=nil)
|
||||||
if decrement
|
if decrement
|
||||||
write "DECRRBY #{key} #{decrement}\r\n"
|
write "DECRBY #{key} #{decrement}\r\n"
|
||||||
else
|
else
|
||||||
write "DECR #{key}\r\n"
|
write "DECR #{key}\r\n"
|
||||||
end
|
end
|
||||||
@ -376,7 +402,14 @@ class Redis
|
|||||||
|
|
||||||
def set(key, val, expiry=nil)
|
def set(key, val, expiry=nil)
|
||||||
write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
|
write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
|
||||||
get_response == OK
|
s = get_response == OK
|
||||||
|
return expire(key, expiry) if s && expiry
|
||||||
|
s
|
||||||
|
end
|
||||||
|
|
||||||
|
def expire(key, expiry=nil)
|
||||||
|
write("EXPIRE #{key} #{expiry}\r\n")
|
||||||
|
get_response == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_unless_exists(key, val)
|
def set_unless_exists(key, val)
|
||||||
|
@ -88,12 +88,9 @@ class Server
|
|||||||
end
|
end
|
||||||
|
|
||||||
def connect_to(host, port, timeout=nil)
|
def connect_to(host, port, timeout=nil)
|
||||||
addrs = Socket.getaddrinfo('localhost', nil)
|
addrs = Socket.getaddrinfo(host, nil)
|
||||||
addr = addrs.detect { |ad| ad[0] == 'AF_INET' }
|
addr = addrs.detect { |ad| ad[0] == 'AF_INET' }
|
||||||
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
||||||
#addr = Socket.getaddrinfo(host, nil)
|
|
||||||
#sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
|
|
||||||
|
|
||||||
if timeout
|
if timeout
|
||||||
secs = Integer(timeout)
|
secs = Integer(timeout)
|
||||||
usecs = Integer((timeout - secs) * 1_000_000)
|
usecs = Integer((timeout - secs) * 1_000_000)
|
||||||
@ -101,7 +98,7 @@ class Server
|
|||||||
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
|
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
|
||||||
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
|
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
|
||||||
end
|
end
|
||||||
sock.connect(Socket.pack_sockaddr_in('6379', addr[3]))
|
sock.connect(Socket.pack_sockaddr_in(port, addr[3]))
|
||||||
sock
|
sock
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,14 +12,20 @@ class Foo
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "redis" do
|
describe "redis" do
|
||||||
before(:each) do
|
before(:all) do
|
||||||
@r = Redis.new
|
@r = Redis.new
|
||||||
@r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
|
@r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
|
||||||
|
end
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
@r['foo'] = 'bar'
|
@r['foo'] = 'bar'
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after(:each) do
|
||||||
@r.keys('*').each {|k| @r.delete k}
|
@r.keys('*').each {|k| @r.delete k}
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:all) do
|
||||||
@r.quit
|
@r.quit
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -33,6 +39,13 @@ describe "redis" do
|
|||||||
@r['foo'].should == 'nik'
|
@r['foo'].should == 'nik'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should be able to SET a key with an expiry" do
|
||||||
|
@r.set('foo', 'bar', 1)
|
||||||
|
@r['foo'].should == 'bar'
|
||||||
|
sleep 2
|
||||||
|
@r['foo'].should == nil
|
||||||
|
end
|
||||||
|
|
||||||
it "should be able to SETNX(set_unless_exists)" do
|
it "should be able to SETNX(set_unless_exists)" do
|
||||||
@r['foo'] = 'nik'
|
@r['foo'] = 'nik'
|
||||||
@r['foo'].should == 'nik'
|
@r['foo'].should == 'nik'
|
||||||
@ -53,8 +66,7 @@ describe "redis" do
|
|||||||
@r.incr('counter').should == 2
|
@r.incr('counter').should == 2
|
||||||
@r.incr('counter').should == 3
|
@r.incr('counter').should == 3
|
||||||
@r.decr('counter').should == 2
|
@r.decr('counter').should == 2
|
||||||
@r.decr('counter').should == 1
|
@r.decr('counter', 2).should == 0
|
||||||
@r.decr('counter').should == 0
|
|
||||||
end
|
end
|
||||||
#
|
#
|
||||||
it "should be able to RANDKEY(return a random key)" do
|
it "should be able to RANDKEY(return a random key)" do
|
||||||
@ -78,6 +90,14 @@ describe "redis" do
|
|||||||
@r['bar'].should == 'ohai'
|
@r['bar'].should == 'ohai'
|
||||||
end
|
end
|
||||||
#
|
#
|
||||||
|
it "should be able to EXPIRE a key" do
|
||||||
|
@r['foo'] = 'bar'
|
||||||
|
@r.expire('foo', 1)
|
||||||
|
@r['foo'].should == "bar"
|
||||||
|
sleep 2
|
||||||
|
@r['foo'].should == nil
|
||||||
|
end
|
||||||
|
#
|
||||||
it "should be able to EXISTS(check if key exists)" do
|
it "should be able to EXISTS(check if key exists)" do
|
||||||
@r['foo'] = 'nik'
|
@r['foo'] = 'nik'
|
||||||
@r.key?('foo').should be_true
|
@r.key?('foo').should be_true
|
||||||
|
@ -31,7 +31,7 @@ class RedisRunner
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.stop
|
def self.stop
|
||||||
sh 'killall redis-server'
|
sh 'echo "SHUTDOWN" | nc localhost 6379'
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user