aboutsummaryrefslogtreecommitdiff
path: root/examples/redis.tcl
blob: aa3d246bb7859caea2cea470fc4b09e9f9d554d3 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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"

# Now the same with -type
set result [$r -type HGET env testing]
puts "HGET (-type): testing=$result"

# Now a missing value with -type
set result [$r -type HGET env doesnotexist]
puts "HGET (-type): doesnotexist=$result"

set result [$r -type HGETALL env]
puts "HGETALL (-type): $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