Created
May 4, 2011 13:46
-
-
Save wheresalice/955237 to your computer and use it in GitHub Desktop.
A basic Redis client in Io.
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
//metadoc Redis Alice Kaerast 2011 | |
/*metadoc Redis description | |
redis is an open source, advanced key-value store | |
<br />An example: | |
<pre> | |
myredis := Redis clone connect("127.0.0.1:6379") | |
myredis send("rpush", "foo", "bar") | |
</pre> | |
*/ | |
Redis := Object clone do( | |
debug ::= true | |
connect := method(addr, | |
if(debug, writeln("connecting")) | |
host := addr split(":") first | |
port := addr split(":") second asNumber | |
self socket := Socket clone setHost(host) setPort(port) connect | |
if(debug, writeln("connected")) | |
self | |
) | |
disconnect := method( | |
socket close | |
self | |
) | |
send := method( | |
methodSize := call message arguments size | |
i := 0 | |
sendString := "" | |
while(i < methodSize, sendString := sendString .. call evalArgAt(i) .. " "; i = i + 1) | |
command(sendString) | |
) | |
command := method(cmd, | |
cmd := cmd split | |
cmdstring := "*#{cmd size}\r\n" interpolate | |
i := 0 | |
while(i < cmd size, cmdstring := cmdstring .. "$#{cmd at(i) size}\r\n#{cmd at(i)}\r\n" interpolate; i := i + 1) | |
cmdstring := cmdstring | |
socket streamWrite(cmdstring) | |
response := socket readUntilSeq("\r\n") split | |
response | |
) | |
) | |
myredis := Redis clone connect("127.0.0.1:6379") | |
myredis send("rpush", "foo", "bar") println | |
myredis send("exists", "foo") println | |
myredis send("exists", "bar") println |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment