public
Last active

A basic Redis client in Io.

  • Download Gist
redis.io
Io
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
//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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.