diff options
author | Steve Bennett <steveb@workware.net.au> | 2020-11-14 14:43:41 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2020-11-15 20:05:52 +1000 |
commit | be4c0e8732445172d52dfb38b56c8aa997762309 (patch) | |
tree | 3af6160b81a898427a0a96b45829176361ec3502 /examples | |
parent | c436661c19ac193007af0d2d878f554bad08f905 (diff) | |
download | jimtcl-be4c0e8732445172d52dfb38b56c8aa997762309.zip jimtcl-be4c0e8732445172d52dfb38b56c8aa997762309.tar.gz jimtcl-be4c0e8732445172d52dfb38b56c8aa997762309.tar.bz2 |
redis: Add simple redis client extension
Using the hiredis client library.
Synchronous API only.
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/redis-pubsub.tcl | 61 | ||||
-rw-r--r-- | examples/redis.tcl | 54 |
2 files changed, 115 insertions, 0 deletions
diff --git a/examples/redis-pubsub.tcl b/examples/redis-pubsub.tcl new file mode 100644 index 0000000..ce0f78c --- /dev/null +++ b/examples/redis-pubsub.tcl @@ -0,0 +1,61 @@ +#!/usr/bin/env jimsh + +# Requires the redis extension +package require redis + +# A redis server should be running either on localhost 6379 +# or on the given host port +# +# Usage: redis-pubsub.tcl ?pub|sub? ?host:addr? +# +# If pub or sub is not given, forks and does both + +if {[lindex $argv 0] in {pub sub}} { + # Run in single process mode + set argv [lassign $argv op] +} else { + # fork before connecting so that both processes don't share + # a connection + if {[os.fork] == 0} { + # child subscribes + set op sub + } else { + set op pub + } +} + +try { + lassign $argv addr + if {$addr eq ""} { + set addr localhost:6379 + } + set r [redis [socket stream $addr]] +} on error msg { + puts [errorInfo $msg] + exit 1 +} + +if {$op eq "sub"} { + $r SUBSCRIBE chin + $r SUBSCRIBE chan + + $r readable { + after cancel $afterid + set result [$r read] + puts "$op: $result" + set afterid [after 2000 {incr done}] + } + # If no message for 2 seconds, stop + set afterid [after 2000 {incr done}] + vwait done + puts "$op: quitting on idle" +} else { + loop i 1 15 { + $r PUBLISH chan PONG$i + puts "$op: chan PONG$i" + after 250 + $r PUBLISH chin PING$i + puts "$op: chin PING$i" + after 250 + } +} diff --git a/examples/redis.tcl b/examples/redis.tcl new file mode 100644 index 0000000..3044ab8 --- /dev/null +++ b/examples/redis.tcl @@ -0,0 +1,54 @@ +#!/usr/bin/env jimsh + +# A simple test of the redis extension + +# Requires the redis extension +package require redis + +# A redis server should be running either on localhost 6379 +# or on the given address (e.g. host:port) +try { + lassign $argv addr + if {$addr eq ""} { + set addr localhost:6379 + } + set r [redis [socket stream $addr]] +} on error msg { + puts [errorInfo $msg] + exit 1 +} + +puts "KEYS: [$r KEYS *]" + +# Set a hash +set env(testing) yes +$r HMSET env {*}$env + +set result [$r HGET env testing] +puts "HGET: testing=$result" + +set size [$r HLEN env] +puts "Size of env is $size" + +set time [time { + $r HGETALL env +} 100] +puts "HGETALL: $time" + +# a multi-command transation +$r MULTI +$r SET a A1 +$r SET b B2 +$r EXEC +puts "MGET: [$r MGET a b]" + +# disard +$r MULTI +$r SET a ~A1 +$r SET b ~B2 +$r DISCARD +puts "MGET (DISCARD): [$r MGET a b]" + +set result [$r HGET env testing] + +$r close |