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 require
Tcl 8.5
29 package provide redis
0.1
31 namespace eval redis
{}
33 array set ::redis::fd {}
34 array set ::redis::blocking {}
35 array set ::redis::deferred {}
36 array set ::redis::callback {}
37 array set ::redis::state {} ;# State in non-blocking reply reading
38 array set ::redis::statestack {} ;# Stack of states, for nested mbulks
39 array set ::redis::bulkarg {}
40 array set ::redis::multibulkarg {}
42 # Flag commands requiring last argument as a bulk write operation
43 foreach redis_bulk_cmd
{
44 set setnx rpush lpush rpushx lpushx
linsert lset lrem sadd srem sismember echo getset smove zadd zrem zscore zincrby
append zrank zrevrank hget hdel hexists setex publish
46 set ::redis::bulkarg($redis_bulk_cmd) {}
49 # Flag commands requiring last argument as a bulk write operation
50 foreach redis_multibulk_cmd
{
51 mset msetnx hset hsetnx hmset hmget
53 set ::redis::multibulkarg($redis_multibulk_cmd) {}
57 unset redis_multibulk_cmd
59 proc redis
{{server
127.0.0.1} {port
6379} {defer
0}} {
60 set fd
[socket $server $port]
61 fconfigure $fd -translation binary
62 set id
[incr ::redis::id]
63 set ::redis::fd($id) $fd
64 set ::redis::blocking($id) 1
65 set ::redis::deferred($id) $defer
66 ::redis::redis_reset_state $id
67 interp alias
{} ::redis::redisHandle$id {} ::redis::__dispatch__ $id
70 proc ::redis::__dispatch__ {id method args
} {
71 set fd
$::redis::fd($id)
72 set blocking
$::redis::blocking($id)
73 set deferred
$::redis::deferred($id)
75 if {[llength $args] == 0} {
76 error "Please provide a callback in non-blocking mode"
78 set callback
[lindex $args end
]
79 set args
[lrange $args 0 end-1
]
81 if {[info command
::redis::__method__$method] eq
{}} {
82 if {[info exists
::redis::bulkarg($method)]} {
84 append cmd
[join [lrange $args 0 end-1
]]
85 append cmd
" [string length [lindex $args end]]\r\n"
86 append cmd
[lindex $args end
]
87 ::redis::redis_writenl $fd $cmd
88 } elseif
{[info exists
::redis::multibulkarg($method)]} {
89 set cmd
"*[expr {[llength $args]+1}]\r\n"
90 append cmd
"$[string length $method]\r\n$method\r\n"
92 append cmd
"$[string length $a]\r\n$a\r\n"
94 ::redis::redis_write $fd $cmd
98 append cmd
[join $args]
99 ::redis::redis_writenl $fd $cmd
103 ::redis::redis_read_reply $fd
105 # Every well formed reply read will pop an element from this
106 # list and use it as a callback. So pipelining is supported
107 # in non blocking mode.
108 lappend ::redis::callback($id) $callback
109 fileevent $fd readable
[list ::redis::redis_readable $fd $id]
113 uplevel 1 [list ::redis::__method__$method $id $fd] $args
117 proc ::redis::__method__blocking {id fd val
} {
118 set ::redis::blocking($id) $val
119 fconfigure $fd -blocking $val
122 proc ::redis::__method__read {id fd
} {
123 ::redis::redis_read_reply $fd
126 proc ::redis::__method__close {id fd
} {
128 catch {unset ::redis::fd($id)}
129 catch {unset ::redis::blocking($id)}
130 catch {unset ::redis::state($id)}
131 catch {unset ::redis::statestack($id)}
132 catch {unset ::redis::callback($id)}
133 catch {interp alias
{} ::redis::redisHandle$id {}}
136 proc ::redis::__method__channel {id fd
} {
140 proc ::redis::redis_write {fd buf
} {
141 puts -nonewline $fd $buf
144 proc ::redis::redis_writenl {fd buf
} {
146 redis_write
$fd "\r\n"
150 proc ::redis::redis_readnl {fd len
} {
151 set buf
[read $fd $len]
152 read $fd 2 ; # discard CR LF
156 proc ::redis::redis_bulk_read {fd
} {
157 set count
[redis_read_line
$fd]
158 if {$count == -1} return {}
159 set buf
[redis_readnl
$fd $count]
163 proc ::redis::redis_multi_bulk_read fd
{
164 set count
[redis_read_line
$fd]
165 if {$count == -1} return {}
167 for {set i
0} {$i < $count} {incr i
} {
168 lappend l
[redis_read_reply
$fd]
173 proc ::redis::redis_read_line fd
{
174 string trim
[gets $fd]
177 proc ::redis::redis_read_reply fd
{
178 set type
[read $fd 1]
179 switch -exact -- $type {
181 + {redis_read_line
$fd}
182 - {return -code error [redis_read_line
$fd]}
183 $ {redis_bulk_read
$fd}
184 * {redis_multi_bulk_read
$fd}
185 default {return -code error "Bad protocol, $type as reply type byte"}
189 proc ::redis::redis_reset_state id
{
190 set ::redis::state($id) [dict create buf
{} mbulk
-1 bulk
-1 reply
{}]
191 set ::redis::statestack($id) {}
194 proc ::redis::redis_call_callback {id type reply
} {
195 set cb
[lindex $::redis::callback($id) 0]
196 set ::redis::callback($id) [lrange $::redis::callback($id) 1 end
]
197 uplevel #0 $cb [list ::redis::redisHandle$id $type $reply]
198 ::redis::redis_reset_state $id
201 # Read a reply in non-blocking mode.
202 proc ::redis::redis_readable {fd id
} {
204 redis_call_callback
$id eof {}
205 ::redis::__method__close $id $fd
208 if {[dict get
$::redis::state($id) bulk
] == -1} {
210 if {$line eq
{}} return ;# No complete line available, return
211 switch -exact -- [string index
$line 0] {
213 + {redis_call_callback
$id reply
[string range
$line 1 end-1
]}
214 - {redis_call_callback
$id err
[string range
$line 1 end-1
]}
216 dict
set ::redis::state($id) bulk
\
217 [expr [string range
$line 1 end-1
]+2]
218 if {[dict get
$::redis::state($id) bulk
] == 1} {
219 # We got a $-1, hack the state to play well with this.
220 dict
set ::redis::state($id) bulk
2
221 dict
set ::redis::state($id) buf
"\r\n"
222 ::redis::redis_readable $fd $id
226 dict
set ::redis::state($id) mbulk
[string range
$line 1 end-1
]
228 if {[dict get
$::redis::state($id) mbulk
] == -1} {
229 redis_call_callback
$id reply
{}
233 redis_call_callback
$id err
\
234 "Bad protocol, $type as reply type byte"
238 set totlen
[dict get
$::redis::state($id) bulk
]
239 set buflen
[string length
[dict get
$::redis::state($id) buf
]]
240 set toread
[expr {$totlen-$buflen}]
241 set data
[read $fd $toread]
242 set nread
[string length
$data]
243 dict
append ::redis::state($id) buf
$data
244 # Check if we read a complete bulk reply
245 if {[string length
[dict get
$::redis::state($id) buf
]] ==
246 [dict get
$::redis::state($id) bulk
]} {
247 if {[dict get
$::redis::state($id) mbulk
] == -1} {
248 redis_call_callback
$id reply
\
249 [string range
[dict get
$::redis::state($id) buf
] 0 end-2
]
251 dict with
::redis::state($id) {
252 lappend reply
[string range
$buf 0 end-2
]
256 if {[dict get
$::redis::state($id) mbulk
] == 0} {
257 redis_call_callback
$id reply
\
258 [dict get
$::redis::state($id) reply
]