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::callback {}
36 array set ::redis::state {} ;# State in non-blocking reply reading
37 array set ::redis::statestack {} ;# Stack of states, for nested mbulks
38 array set ::redis::bulkarg {}
39 array set ::redis::multibulkarg {}
41 # Flag commands requiring last argument as a bulk write operation
42 foreach redis_bulk_cmd
{
43 set setnx rpush lpush
lset lrem sadd srem sismember echo getset smove zadd zrem zscore zincrby
append zrank zrevrank hget hdel hexists
45 set ::redis::bulkarg($redis_bulk_cmd) {}
48 # Flag commands requiring last argument as a bulk write operation
49 foreach redis_multibulk_cmd
{
50 mset msetnx hset hsetnx hmset hmget
52 set ::redis::multibulkarg($redis_multibulk_cmd) {}
56 unset redis_multibulk_cmd
58 proc redis
{{server
127.0.0.1} {port
6379}} {
59 set fd
[socket $server $port]
60 fconfigure $fd -translation binary
61 set id
[incr ::redis::id]
62 set ::redis::fd($id) $fd
63 set ::redis::blocking($id) 1
64 ::redis::redis_reset_state $id
65 interp alias
{} ::redis::redisHandle$id {} ::redis::__dispatch__ $id
68 proc ::redis::__dispatch__ {id method args
} {
69 set fd
$::redis::fd($id)
70 set blocking
$::redis::blocking($id)
72 if {[llength $args] == 0} {
73 error "Please provide a callback in non-blocking mode"
75 set callback
[lindex $args end
]
76 set args
[lrange $args 0 end-1
]
78 if {[info command
::redis::__method__$method] eq
{}} {
79 if {[info exists
::redis::bulkarg($method)]} {
81 append cmd
[join [lrange $args 0 end-1
]]
82 append cmd
" [string length [lindex $args end]]\r\n"
83 append cmd
[lindex $args end
]
84 ::redis::redis_writenl $fd $cmd
85 } elseif
{[info exists
::redis::multibulkarg($method)]} {
86 set cmd
"*[expr {[llength $args]+1}]\r\n"
87 append cmd
"$[string length $method]\r\n$method\r\n"
89 append cmd
"$[string length $a]\r\n$a\r\n"
91 ::redis::redis_write $fd $cmd
95 append cmd
[join $args]
96 ::redis::redis_writenl $fd $cmd
99 ::redis::redis_read_reply $fd
101 # Every well formed reply read will pop an element from this
102 # list and use it as a callback. So pipelining is supported
103 # in non blocking mode.
104 lappend ::redis::callback($id) $callback
105 fileevent $fd readable
[list ::redis::redis_readable $fd $id]
108 uplevel 1 [list ::redis::__method__$method $id $fd] $args
112 proc ::redis::__method__blocking {id fd val
} {
113 set ::redis::blocking($id) $val
114 fconfigure $fd -blocking $val
117 proc ::redis::__method__close {id fd
} {
119 catch {unset ::redis::fd($id)}
120 catch {unset ::redis::blocking($id)}
121 catch {unset ::redis::state($id)}
122 catch {unset ::redis::statestack($id)}
123 catch {unset ::redis::callback($id)}
124 catch {interp alias
{} ::redis::redisHandle$id {}}
127 proc ::redis::__method__channel {id fd
} {
131 proc ::redis::redis_write {fd buf
} {
132 puts -nonewline $fd $buf
135 proc ::redis::redis_writenl {fd buf
} {
137 redis_write
$fd "\r\n"
141 proc ::redis::redis_readnl {fd len
} {
142 set buf
[read $fd $len]
143 read $fd 2 ; # discard CR LF
147 proc ::redis::redis_bulk_read {fd
} {
148 set count
[redis_read_line
$fd]
149 if {$count == -1} return {}
150 set buf
[redis_readnl
$fd $count]
154 proc ::redis::redis_multi_bulk_read fd
{
155 set count
[redis_read_line
$fd]
156 if {$count == -1} return {}
158 for {set i
0} {$i < $count} {incr i
} {
159 lappend l
[redis_read_reply
$fd]
164 proc ::redis::redis_read_line fd
{
165 string trim
[gets $fd]
168 proc ::redis::redis_read_reply fd
{
169 set type
[read $fd 1]
170 switch -exact -- $type {
172 + {redis_read_line
$fd}
173 - {return -code error [redis_read_line
$fd]}
174 $ {redis_bulk_read
$fd}
175 * {redis_multi_bulk_read
$fd}
176 default {return -code error "Bad protocol, $type as reply type byte"}
180 proc ::redis::redis_reset_state id
{
181 set ::redis::state($id) [dict create buf
{} mbulk
-1 bulk
-1 reply
{}]
182 set ::redis::statestack($id) {}
185 proc ::redis::redis_call_callback {id type reply
} {
186 set cb
[lindex $::redis::callback($id) 0]
187 set ::redis::callback($id) [lrange $::redis::callback($id) 1 end
]
188 uplevel #0 $cb [list ::redis::redisHandle$id $type $reply]
189 ::redis::redis_reset_state $id
192 # Read a reply in non-blocking mode.
193 proc ::redis::redis_readable {fd id
} {
195 redis_call_callback
$id eof {}
196 ::redis::__method__close $id $fd
199 if {[dict get
$::redis::state($id) bulk
] == -1} {
201 if {$line eq
{}} return ;# No complete line available, return
202 switch -exact -- [string index
$line 0] {
204 + {redis_call_callback
$id reply
[string range
$line 1 end-1
]}
205 - {redis_call_callback
$id err
[string range
$line 1 end-1
]}
207 dict
set ::redis::state($id) bulk
\
208 [expr [string range
$line 1 end-1
]+2]
209 if {[dict get
$::redis::state($id) bulk
] == 1} {
210 # We got a $-1, hack the state to play well with this.
211 dict
set ::redis::state($id) bulk
2
212 dict
set ::redis::state($id) buf
"\r\n"
213 ::redis::redis_readable $fd $id
217 dict
set ::redis::state($id) mbulk
[string range
$line 1 end-1
]
219 if {[dict get
$::redis::state($id) mbulk
] == -1} {
220 redis_call_callback
$id reply
{}
224 redis_call_callback
$id err
\
225 "Bad protocol, $type as reply type byte"
229 set totlen
[dict get
$::redis::state($id) bulk
]
230 set buflen
[string length
[dict get
$::redis::state($id) buf
]]
231 set toread
[expr {$totlen-$buflen}]
232 set data
[read $fd $toread]
233 set nread
[string length
$data]
234 dict
append ::redis::state($id) buf
$data
235 # Check if we read a complete bulk reply
236 if {[string length
[dict get
$::redis::state($id) buf
]] ==
237 [dict get
$::redis::state($id) bulk
]} {
238 if {[dict get
$::redis::state($id) mbulk
] == -1} {
239 redis_call_callback
$id reply
\
240 [string range
[dict get
$::redis::state($id) buf
] 0 end-2
]
242 dict with
::redis::state($id) {
243 lappend reply
[string range
$buf 0 end-2
]
247 if {[dict get
$::redis::state($id) mbulk
] == 0} {
248 redis_call_callback
$id reply
\
249 [dict get
$::redis::state($id) reply
]