1 # Tcl clinet library - used by test-redis.tcl script for now
2 # Copyright (C) 2009 Salvatore Sanfilippo
3 # Released under the BSD license like Redis itself
7 # set r [redis 127.0.0.1 6379]
10 # $r lrange mylist 0 -1
13 # Non blocking usage example:
15 # proc handlePong {r type reply} {
16 # puts "PONG $type '$reply'"
17 # if {$reply ne "PONG"} {
18 # $r ping [list handlePong]
24 # $r get fo [list handlePong]
28 package provide redis
0.1
30 namespace eval redis
{}
32 array set ::redis::fd {}
33 array set ::redis::blocking {}
34 array set ::redis::callback {}
35 array set ::redis::state {} ;# State in non-blocking reply reading
36 array set ::redis::statestack {} ;# Stack of states, for nested mbulks
37 array set ::redis::bulkarg {}
38 array set ::redis::multibulkarg {}
40 # Flag commands requiring last argument as a bulk write operation
41 foreach redis_bulk_cmd
{
42 set setnx rpush lpush
lset lrem sadd srem sismember echo getset smove zadd zrem zscore zincrby
append zrank zrevrank hget hdel hexists
44 set ::redis::bulkarg($redis_bulk_cmd) {}
47 # Flag commands requiring last argument as a bulk write operation
48 foreach redis_multibulk_cmd
{
51 set ::redis::multibulkarg($redis_multibulk_cmd) {}
55 unset redis_multibulk_cmd
57 proc redis
{{server
127.0.0.1} {port
6379}} {
58 set fd
[socket $server $port]
59 fconfigure $fd -translation binary
60 set id
[incr ::redis::id]
61 set ::redis::fd($id) $fd
62 set ::redis::blocking($id) 1
63 ::redis::redis_reset_state $id
64 interp alias
{} ::redis::redisHandle$id {} ::redis::__dispatch__ $id
67 proc ::redis::__dispatch__ {id method args
} {
68 set fd
$::redis::fd($id)
69 set blocking
$::redis::blocking($id)
71 if {[llength $args] == 0} {
72 error "Please provide a callback in non-blocking mode"
74 set callback
[lindex $args end
]
75 set args
[lrange $args 0 end-1
]
77 if {[info command
::redis::__method__$method] eq
{}} {
78 if {[info exists
::redis::bulkarg($method)]} {
80 append cmd
[join [lrange $args 0 end-1
]]
81 append cmd
" [string length [lindex $args end]]\r\n"
82 append cmd
[lindex $args end
]
83 ::redis::redis_writenl $fd $cmd
84 } elseif
{[info exists
::redis::multibulkarg($method)]} {
85 set cmd
"*[expr {[llength $args]+1}]\r\n"
86 append cmd
"$[string length $method]\r\n$method\r\n"
88 append cmd
"$[string length $a]\r\n$a\r\n"
90 ::redis::redis_write $fd $cmd
94 append cmd
[join $args]
95 ::redis::redis_writenl $fd $cmd
98 ::redis::redis_read_reply $fd
100 # Every well formed reply read will pop an element from this
101 # list and use it as a callback. So pipelining is supported
102 # in non blocking mode.
103 lappend ::redis::callback($id) $callback
104 fileevent $fd readable
[list ::redis::redis_readable $fd $id]
107 uplevel 1 [list ::redis::__method__$method $id $fd] $args
111 proc ::redis::__method__blocking {id fd val
} {
112 set ::redis::blocking($id) $val
113 fconfigure $fd -blocking $val
116 proc ::redis::__method__close {id fd
} {
118 catch {unset ::redis::fd($id)}
119 catch {unset ::redis::blocking($id)}
120 catch {unset ::redis::state($id)}
121 catch {unset ::redis::statestack($id)}
122 catch {unset ::redis::callback($id)}
123 catch {interp alias
{} ::redis::redisHandle$id {}}
126 proc ::redis::__method__channel {id fd
} {
130 proc ::redis::redis_write {fd buf
} {
131 puts -nonewline $fd $buf
134 proc ::redis::redis_writenl {fd buf
} {
136 redis_write
$fd "\r\n"
140 proc ::redis::redis_readnl {fd len
} {
141 set buf
[read $fd $len]
142 read $fd 2 ; # discard CR LF
146 proc ::redis::redis_bulk_read {fd
} {
147 set count
[redis_read_line
$fd]
148 if {$count == -1} return {}
149 set buf
[redis_readnl
$fd $count]
153 proc ::redis::redis_multi_bulk_read fd
{
154 set count
[redis_read_line
$fd]
155 if {$count == -1} return {}
157 for {set i
0} {$i < $count} {incr i
} {
158 lappend l
[redis_read_reply
$fd]
163 proc ::redis::redis_read_line fd
{
164 string trim
[gets $fd]
167 proc ::redis::redis_read_reply fd
{
168 set type
[read $fd 1]
169 switch -exact -- $type {
171 + {redis_read_line
$fd}
172 - {return -code error [redis_read_line
$fd]}
173 $ {redis_bulk_read
$fd}
174 * {redis_multi_bulk_read
$fd}
175 default {return -code error "Bad protocol, $type as reply type byte"}
179 proc ::redis::redis_reset_state id
{
180 set ::redis::state($id) [dict create buf
{} mbulk
-1 bulk
-1 reply
{}]
181 set ::redis::statestack($id) {}
184 proc ::redis::redis_call_callback {id type reply
} {
185 set cb
[lindex $::redis::callback($id) 0]
186 set ::redis::callback($id) [lrange $::redis::callback($id) 1 end
]
187 uplevel #0 $cb [list ::redis::redisHandle$id $type $reply]
188 ::redis::redis_reset_state $id
191 # Read a reply in non-blocking mode.
192 proc ::redis::redis_readable {fd id
} {
194 redis_call_callback
$id eof {}
195 ::redis::__method__close $id $fd
198 if {[dict get
$::redis::state($id) bulk
] == -1} {
200 if {$line eq
{}} return ;# No complete line available, return
201 switch -exact -- [string index
$line 0] {
203 + {redis_call_callback
$id reply
[string range
$line 1 end-1
]}
204 - {redis_call_callback
$id err
[string range
$line 1 end-1
]}
206 dict
set ::redis::state($id) bulk
\
207 [expr [string range
$line 1 end-1
]+2]
208 if {[dict get
$::redis::state($id) bulk
] == 1} {
209 # We got a $-1, hack the state to play well with this.
210 dict
set ::redis::state($id) bulk
2
211 dict
set ::redis::state($id) buf
"\r\n"
212 ::redis::redis_readable $fd $id
216 dict
set ::redis::state($id) mbulk
[string range
$line 1 end-1
]
218 if {[dict get
$::redis::state($id) mbulk
] == -1} {
219 redis_call_callback
$id reply
{}
223 redis_call_callback
$id err
\
224 "Bad protocol, $type as reply type byte"
228 set totlen
[dict get
$::redis::state($id) bulk
]
229 set buflen
[string length
[dict get
$::redis::state($id) buf
]]
230 set toread
[expr {$totlen-$buflen}]
231 set data
[read $fd $toread]
232 set nread
[string length
$data]
233 dict
append ::redis::state($id) buf
$data
234 # Check if we read a complete bulk reply
235 if {[string length
[dict get
$::redis::state($id) buf
]] ==
236 [dict get
$::redis::state($id) bulk
]} {
237 if {[dict get
$::redis::state($id) mbulk
] == -1} {
238 redis_call_callback
$id reply
\
239 [string range
[dict get
$::redis::state($id) buf
] 0 end-2
]
241 dict with
::redis::state($id) {
242 lappend reply
[string range
$buf 0 end-2
]
246 if {[dict get
$::redis::state($id) mbulk
] == 0} {
247 redis_call_callback
$id reply
\
248 [dict get
$::redis::state($id) reply
]