Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
9495122b
Commit
9495122b
authored
Apr 08, 2009
by
antirez
Browse files
Ruby client updated
parent
928394cd
Changes
6
Hide whitespace changes
Inline
Side-by-side
client-libraries/README
View file @
9495122b
...
...
@@ -15,6 +15,7 @@ it's possible to grab the most recent version:
Ruby lib source code:
http://github.com/ezmobius/redis-rb/tree/master
git://github.com/ezmobius/redis-rb.git
Erlang lib source code:
http://bitbucket.org/adroll/erldis/
...
...
client-libraries/ruby/lib/better_timeout.rb
deleted
100644 → 0
View file @
928394cd
#--
# = 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
client-libraries/ruby/lib/redis.rb
View file @
9495122b
...
...
@@ -19,8 +19,9 @@ class Redis
def
initialize
(
opts
=
{})
@opts
=
{
:host
=>
'localhost'
,
:port
=>
'6379'
}.
merge
(
opts
)
@opts
=
{
:host
=>
'localhost'
,
:port
=>
'6379'
,
:db
=>
0
}.
merge
(
opts
)
$debug
=
@opts
[
:debug
]
@db
=
@opts
[
:db
]
@server
=
Server
.
new
(
@opts
[
:host
],
@opts
[
:port
])
end
...
...
@@ -47,7 +48,21 @@ class Redis
#Server down
rescue
NoMethodError
=>
e
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
"
\n
Got ^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
...
...
@@ -56,6 +71,7 @@ class Redis
end
def
select_db
(
index
)
@db
=
index
write
"SELECT
#{
index
}
\r\n
"
get_response
end
...
...
@@ -65,6 +81,16 @@ class Redis
get_response
==
OK
end
def
flush_all
ensure_retry
do
puts
"Warning!
\n
Flushing *ALL* databases!
\n
5 Seconds to Hit ^C!"
trap
(
'INT'
)
{
quit
;
return
false
}
sleep
5
write
"FLUSHALL
\r\n
"
get_response
==
OK
end
end
def
last_save
write
"LASTSAVE
\r\n
"
get_response
.
to_i
...
...
@@ -79,7 +105,7 @@ class Redis
info
=
{}
write
(
"INFO
\r\n
"
)
x
=
get_response
x
.
each
_line
do
|
kv
|
x
.
each
do
|
kv
|
k
,
v
=
kv
.
split
(
':'
,
2
)
k
,
v
=
k
.
chomp
,
v
=
v
.
chomp
info
[
k
.
to_sym
]
=
v
...
...
@@ -182,7 +208,7 @@ class Redis
def
decr
(
key
,
decrement
=
nil
)
if
decrement
write
"DECR
R
BY
#{
key
}
#{
decrement
}
\r\n
"
write
"DECRBY
#{
key
}
#{
decrement
}
\r\n
"
else
write
"DECR
#{
key
}
\r\n
"
end
...
...
@@ -376,7 +402,14 @@ class Redis
def
set
(
key
,
val
,
expiry
=
nil
)
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
def
set_unless_exists
(
key
,
val
)
...
...
client-libraries/ruby/lib/server.rb
View file @
9495122b
...
...
@@ -88,12 +88,9 @@ class Server
end
def
connect_to
(
host
,
port
,
timeout
=
nil
)
addrs
=
Socket
.
getaddrinfo
(
'local
host
'
,
nil
)
addrs
=
Socket
.
getaddrinfo
(
host
,
nil
)
addr
=
addrs
.
detect
{
|
ad
|
ad
[
0
]
==
'AF_INET'
}
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
secs
=
Integer
(
timeout
)
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_SNDTIMEO
,
optval
end
sock
.
connect
(
Socket
.
pack_sockaddr_in
(
'6379'
,
addr
[
3
]))
sock
.
connect
(
Socket
.
pack_sockaddr_in
(
port
,
addr
[
3
]))
sock
end
...
...
client-libraries/ruby/spec/redis_spec.rb
View file @
9495122b
...
...
@@ -12,14 +12,20 @@ class Foo
end
describe
"redis"
do
before
(
:
each
)
do
before
(
:
all
)
do
@r
=
Redis
.
new
@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'
end
after
(
:each
)
do
@r
.
keys
(
'*'
).
each
{
|
k
|
@r
.
delete
k
}
end
after
do
@r
.
keys
(
'*'
).
each
{
|
k
|
@r
.
delete
k
}
after
(
:all
)
do
@r
.
quit
end
...
...
@@ -33,6 +39,13 @@ describe "redis" do
@r
[
'foo'
].
should
==
'nik'
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
@r
[
'foo'
]
=
'nik'
@r
[
'foo'
].
should
==
'nik'
...
...
@@ -53,8 +66,7 @@ describe "redis" do
@r
.
incr
(
'counter'
).
should
==
2
@r
.
incr
(
'counter'
).
should
==
3
@r
.
decr
(
'counter'
).
should
==
2
@r
.
decr
(
'counter'
).
should
==
1
@r
.
decr
(
'counter'
).
should
==
0
@r
.
decr
(
'counter'
,
2
).
should
==
0
end
#
it
"should be able to RANDKEY(return a random key)"
do
...
...
@@ -78,6 +90,14 @@ describe "redis" do
@r
[
'bar'
].
should
==
'ohai'
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
@r
[
'foo'
]
=
'nik'
@r
.
key?
(
'foo'
).
should
be_true
...
...
client-libraries/ruby/tasks/redis.tasks.rb
View file @
9495122b
...
...
@@ -31,7 +31,7 @@ class RedisRunner
end
def
self
.
stop
sh
'
killall redis-server
'
sh
'
echo "SHUTDOWN" | nc localhost 6379
'
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment