Skip to content

Commit e859e51

Browse files
redis cli commands
1 parent 48237b5 commit e859e51

1 file changed

Lines changed: 210 additions & 0 deletions

File tree

redis-cli_commands.txt

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
Redis-commands
2+
docker run -d --name redis-lab -p:8888:8888 redisuniversity/ru101-lab
3+
# docker run -d --name redis-lab-102j -p:8888:8888 -p:8081:8081 redisuniversity/ru102j-lab
4+
docker run --name redis -d -p 6379:6379 redis redis-server --requirepass "SUPER_SECRET_PASSWORD"
5+
docker exec -it redis_ru102j redis-cli
6+
docker run -it --rm redis redis-cli -h 192.168.0.8|host.docker.internal
7+
docker run -it --rm redis redis-cli -h host.docker.internal -p 6379 -a SUPER_SECRET_PASSWORD
8+
redis://user:password@host:port/dbnum
9+
redis-cli -u redis://LJenkins:p%40ssw0rd@redis-16379.hosted.com:16379/0 PING
10+
11+
localhost:8888/entry.html
12+
Redis-cli commands
13+
KEYS
14+
dbsize # number of keys
15+
set user:1000:fname thierno EX 10 # 10s expiration time
16+
set user:1000:lname diallo PX 10000 # 10.000 ms expiration time
17+
set user:1000:lname diallo NX # set only if key does not exit
18+
set user:1000:lname diallo XX # set only if key exits
19+
get user:1000:fname
20+
exists user:1000:fname
21+
KEYS user:1000:* # blocks db: not for prod
22+
SCAN 0 MATCH user:1000:* [COUNT 10000]
23+
DEL key user:1000:lname # blocks
24+
UNLINK key user:1000:fname
25+
TTL user:1000:fname # remaing time to expire in seconds
26+
PTTL user:1000:lname # remaing time to expire in milliseconds
27+
PERSIST user:1000:fname # remove expiration
28+
EXPIRE user:1000:fname 10 # expire in 10s
29+
PEXPIRE user:1000:fname 10 # expire in 10 ms
30+
EXPIREAT key unix-time-seconds [NX | XX | GT | LT]
31+
PEXPIREAT key unix-time-milliseconds [NX | XX | GT | LT]
32+
STRINGS
33+
INCR user:1000:pwdErrorCount # set 1 if key does not exist
34+
INCRBY user:1000:pwdErrorCount 2
35+
DECR user:1000:pwdErrorCount
36+
DECRBY user:1000:pwdErrorCount 2
37+
TYPE user:1000:pwdErrorCount
38+
OBJECT encoding user:1000:pwdErrorCount # internal incoding of a key value
39+
HASHES (Maps/dictionaries)
40+
HSET player:42 name thierno race black level 4 hp 20 gold 20
41+
HMSET player:43 name thierno race black level 4 hp 20 gold 20
42+
HGETALL player:42
43+
HGET player:42 name
44+
HMGET player:42 name status " get multiple fields values
45+
HEXISTS player:42 name
46+
HSET player:42 status dazed
47+
HSETNX player:42 status dazed # set if the key does not exist
48+
HDEL player:42 status
49+
HINCRBY player:42 gold 10
50+
HINCRBYFLOAT player:42 gold 10
51+
HSCAN player:42 0 MATCH *e*
52+
HKEYS player:42
53+
HVALS player:42
54+
EXPIRE player:42 100
55+
HLEN player:42 # size
56+
LIST
57+
RPUSH playlist 71
58+
RPUSH playlist 53 45 32
59+
LPOP playlist
60+
LPUSH playlist 20
61+
RPOP playlist
62+
LLEN playlist
63+
LRANGE playlist 0 2
64+
LRANGE playlist 0 -1 # from start to end
65+
LRANGE playlist:1 -4 -2 # from 4th position from end to 2nd from end
66+
LINDEX playlist:2 1
67+
LINSERT playlist:2 BEFORE 56 33
68+
LINSERT playlist:2 AFTER 56 44
69+
LSET playlist:2 3 66
70+
LREM playlist:2 1 60 # remove value 60 one time
71+
LTRIM playlist:3 0 3 # keep element from index 0 to 3
72+
llen playlist:3
73+
SET
74+
SADD users:game:1 thierno
75+
SMEMBERS users:game:1:friends # print content
76+
SCARD users:game:1 # size
77+
SISMEMBER users:game:1 thierno # is in the set
78+
SREM users:game:1:friends amadou # remove from set
79+
SSCAN users:game:1:friends 0 MATCH *
80+
SPOP users:game:1:friends # remove a random element
81+
SPOP users:game:1:friends 2 # remove 2 random element
82+
SDIFF users:game:1 users:game:1:friends # minus
83+
SINTER users:game:1 users:game:1:friends # intersection
84+
SUNION users:game:1 users:game:1:friends # union
85+
scard users:game:1 # size/cardinality
86+
SORTED SET
87+
ZADD leaders:exp 0 42
88+
ZINCRBY leaders:exp 120 42
89+
zadd testsocre +inf A
90+
ZRANGE leaders:exp 0 4 WITHSCORE # order by score asc
91+
ZREVRANGE leaders:exp 0 4 WITHSCORES # order by score desc
92+
ZRANK leaders:exp 42
93+
ZREVRANK leaders:exp 42
94+
ZSCORE leaders:exp 42
95+
ZCOUNT leaders:exp 0 10 # element with score from 0 to 10
96+
ZRANGEBYSCORE leaders:exp 10 20 WITHSCORES
97+
ZRANGEBYLEX leaders:exp - + # print all elements from -INF to +INF
98+
ZRANGEBYLEX leaders:exp (12 [85 # from 12 exclusive to 85 inclusive
99+
ZREM leaders:exp 41
100+
ZREMRANGEBYSCORE leaders:exp 12 25
101+
zrangebyscore hw1-8 (3 (6 WITHSCORES
102+
zrangebyscore hw1-8 (3 +inf WITHSCORES
103+
ZREMRANGEBYLEX leaders:exp [12 [15
104+
ZREMRANGEBYRANK leaders:exp 1 -5
105+
zcard hw1-8 # size/cardinality
106+
zpopmin slist-2
107+
zpopmax slist-2
108+
zinterstore sintersection 2 slist-2 slist-3 aggregate sum
109+
zinterstore sintersection 2 slist-2 a-non-sorted-set weights 2 1 aggregate sum
110+
zunionstore sunion 2 slist-2 slist-3 weights 2 1 aggregate sum
111+
ZDIFF
112+
Redis 6.2 introduced the ZDIFF
113+
For previous versions use:
114+
ZADD all 1 one 2 two 3 three
115+
SADD disabled two # this is the created intersection set
116+
ZUNIONSTORE tmp 2 all disabled WEIGHTS 1 0 AGGREGATE MIN
117+
ZREVRANGEBYSCORE tmp +inf 1 WITHSCORES
118+
TRANSACTION
119+
MULTI # to start a transaction: this will queue and batch commands later
120+
EXEC # execute all queued commands atomically/TRANSACTION
121+
DISCARD # Abort a transaction
122+
OPTIMISTIC LOCKING
123+
MULTI
124+
watch event:signups # exec will abort a transaction (nil is returned by EXEC) if a watched key before MULTI was modified by another client
125+
unwatch # if exec succeeds all keys are unwatched automitically
126+
BIT DATA
127+
setbit player:42:map1 9 1
128+
getbit player:42:map1 90
129+
bitcount player:42:map1
130+
bitcount player:42:map1 1 2 # BYTE offset 1 to 2 instead of BIT like all other commands
131+
bitfield mykey set u8 0 42 # set 8 bits unsigned int 42 at offset 0
132+
bitfield mykey get u8 0
133+
bitfield mykey incrby u8 0 1
134+
bitpos mykey 1 0 -1
135+
bitfield mybfByPosition set u8 #2 10 # set unsigned 8 bits length 10 to offset 2*8
136+
bitfield mybfByPosition get u8 #2
137+
bitfield mybfByPosition get u8 #2 get u8 16
138+
1) (integer) 10
139+
2) (integer) 10
140+
BIT OPERATIONS
141+
bitfield mykey2 set u8 0 2
142+
bitfield mykey4 set u8 0 4
143+
bitop OR mykey6 mykey2 mykey4
144+
bitfield mykey6 get u8 0
145+
1) (integer) 6
146+
PUB / SUB
147+
subscribe ch-1
148+
subscribe ch-1 ch-2
149+
publish ch-1 hello
150+
publish ch-1 hello # print number of active channels
151+
PSUBSCRIBE ch? # pattern subscribe
152+
PUBSUB numsub ch1 ch2 # print number of subscribers excluding pattern subscribers
153+
PUBSUB numpat # print number of pattern subscribers
154+
GEOSPECIAL DATA
155+
GEOADD fouta:attractions -122.2345673 40.9876543 mity
156+
GEOADD fouta:attractions -122.3345673 40.8876543 centreville -122.2395 40.91 sily
157+
zrange fouta:attractions 0 -1 WITHSCORES # geo data is stored in a sorted set with the score being the computed geohash of longitude and latitude
158+
GEOHASH fouta:attractions sily # 11 chars representation of the hash value that can be reduced from right to left with a lost of precistion of the original position
159+
geopos fouta:attractions sily
160+
zrem fouta:attractions sily
161+
GEODIST fouta:attractions centreville sily km
162+
GEORADIUS fouta:attractions -122.239 40.90 20 km
163+
GEORADIUS fouta:attractions -122.239 40.90 20 km WITHDIST WITHCOORD ASC COUNT 2
164+
GEORADIUSBYMEMBER fouta:attractions sily 6 mi WITHDIST WITHCOORD WITHHASH ASC count 2
165+
GEORADIUS fouta:attractions -122.239 40.90 20 km STORE searchres2 STOREDIST resByDist2
166+
GEORADIUSBYMEMBER fouta:attractions sily 6 mi ASC count 2 STORE searchres STOREDIST resByDist
167+
zunionstore geo:event:combined 2 geo:events:Football geo:events:Softball aggregate min
168+
LUA SCRIPTING
169+
HSET names fname thierno lname diallo
170+
EVAL "return redis.call('HGET', KEYS[1], ARGV[1])" 1 names fname
171+
eval "return 42" 0
172+
eval "return {'3.13'}" 0 # to preserve decimal part
173+
set pival 3.14
174+
eval "return redis.call('GET', KEYS[1]) " 1 pival # preserves decimal
175+
set int-key 42
176+
eval "local val = redis.call('GET', KEYS[1]) return val" 1 int-key
177+
EVAL "return redis.call('HMGET', KEYS[1], ARGV[1], ARGV[2])" 1 names fname lname # returns an array
178+
EVAL "return redis.call('HSCAN', KEYS[1], ARGV[1])" 1 names 0 # returns a table
179+
EVAL "return {{KEYS[1]}, {ARGV[1], ARGV[2]}}" 1 names thierno diallo # construct response manually
180+
MANAGING SCRIPTS
181+
SCRIPT LOAD "return redis.call('HGET', KEYS[1], ARGV[1])"
182+
"4688a0f6e1e971a14e2d596031751f0590d37a92"
183+
EVALSHA 4688a0f6e1e971a14e2d596031751f0590d37a92 1 names fname
184+
"thierno"
185+
EVALSHA 4688a0f6e1e971a14e2d596031751f0590d37a92 1 names lname
186+
"diallo"
187+
SCRIPT EXISTS 4688a0f6e1e971a14e2d596031751f0590d37a92
188+
SCRIPT FLUSH # remove all cached SCRIPTS
189+
SCRIPT KILL # kill currently running script
190+
SCRIPT DEBUG YES|SYNC|NO
191+
SHUTDOWN NOSAVE|SAVE
192+
MULTI LINES SCRIPT
193+
LUA_SCRIPT=`cat <<!
194+
-- convert args to numbers
195+
local val1 = redis.call('GET', KEYS[1])
196+
local val2 = redis.call('GET', KEYS[2])
197+
198+
if ARGV[1] == "sum" then
199+
return val1 + val2
200+
elseif ARGV[1] == "max" then
201+
return math.max(val1, val2)
202+
else
203+
return nil
204+
end
205+
!
206+
`
207+
redis-cli eval "$LUA_SCRIPT" 2 int-key int-key2 max
208+
209+
redis-cli SCRIPT LOAD "$LUA_SCRIPT"
210+
evalsha f8d636269925a802a88e86140509a04791ec7b84 2 int-key int-key2 sum

0 commit comments

Comments
 (0)