Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
4a327b4a
Commit
4a327b4a
authored
May 20, 2009
by
antirez
Browse files
Initial version of an alternative Ruby client added
parent
1350d27e
Changes
1
Hide whitespace changes
Inline
Side-by-side
client-libraries/ruby_2/rubyredis.rb
0 → 100644
View file @
4a327b4a
# RubyRedis is an alternative implementatin of Ruby client library written
# by Salvatore Sanfilippo.
#
# The aim of this library is to create an alternative client library that is
# much simpler and does not implement every command explicitly but uses
# method_missing instead.
require
'socket'
class
RedisClient
BulkCommands
=
{
"set"
=>
true
,
"setnx"
=>
true
,
"rpush"
=>
true
,
"lpush"
=>
true
,
"lset"
=>
true
,
"lrem"
=>
true
,
"sadd"
=>
true
,
"srem"
=>
true
,
"sismember"
=>
true
,
"echo"
=>
true
,
"getset"
=>
true
,
"smove"
=>
true
}
def
initialize
(
opts
=
{})
opts
=
{
:host
=>
'localhost'
,
:port
=>
'6379'
,
:db
=>
0
}.
merge
(
opts
)
@host
=
opts
[
:host
]
@port
=
opts
[
:port
]
@db
=
opts
[
:db
]
@sock
=
connect_to_server
end
def
to_s
"Redis Client connected to
#{
@host
}
:
#{
@port
}
against DB
#{
@db
}
"
end
def
connect_to_server
TCPSocket
.
new
(
@host
,
@port
,
0
)
end
def
method_missing
(
*
argv
)
call_command
(
argv
)
end
def
call_command
(
argv
)
bulk
=
nil
argv
[
0
]
=
argv
[
0
].
to_s
.
downcase
if
BulkCommands
[
argv
[
0
]]
bulk
=
argv
[
-
1
]
argv
[
-
1
]
=
bulk
.
length
end
@sock
.
write
(
argv
.
join
(
" "
)
+
"
\r\n
"
)
@sock
.
write
(
bulk
+
"
\r\n
"
)
if
bulk
read_reply
end
def
read_reply
line
=
@sock
.
gets
case
line
[
0
..
0
]
when
"-"
raise
line
.
strip
when
"+"
line
[
1
..-
1
].
strip
when
":"
line
[
1
..-
1
].
to_i
when
"$"
bulklen
=
line
[
1
..-
1
].
to_i
return
nil
if
bulklen
==
-
1
data
=
@sock
.
read
(
bulklen
)
@sock
.
read
(
2
)
# CRLF
data
when
"*"
objects
=
line
[
1
..-
1
].
to_i
return
nil
if
bulklen
==
-
1
res
=
[]
objects
.
times
{
res
<<
read_reply
}
res
end
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