Redis CLI

Start Server

# start server
redis-server

# cli
redis-cli

String

set name "Daily Code Buffer"
get name
getrange name 0 4
getset name Shabbir
get name
set email email@domain.com
mget name email
mset lang English tech redis
get lang
get tech
mget tech lang 
strlen tech
strlen lang  # Will return 0 if key doesn't Exist
set count 1
get count
incr count  # will return new number
decr count  # Will retunr new count
incrby count 10
decrby count 5
set pi 3.14
get pi
incrbyfloat pi 0.0001
get name
append name " Dawoodi"
get name
get a  # Set any value first to A
expire a 10
ttl a
setex session 10 1234
ttl session
ttl name  # Default is expiration is not set, then it is -1

List

Flushall # to remove all the key vlaue pairs
lpush country India
lpush country USA
lrange country 0 -1
lpush country France
lrange country 0 1
lrange country 0 -1
llen country  # Length of the array
lpop country
lrange country 0 -1
rpop country 
lrange country 0 -1
lset country 1 France # To replace the value at current posiiton
lrange country 0 -1
linsert country before France India
lrange country 0 -1
linsert country after USA UAE
lrange country 0 -1
lindex country 1  # Get values based on the index
lpushx movies Avengers  # It wont add
lrange movies 0 -1  # NO data
lpushx country Australia  # will return the length of list
lrnge country 0 -1
rpushx movies "Iron man"
rpoplpush country country1
lrange country 0 -1
lrnage country1 0 -1
lpush coutry "New Zealand" "South Africa"
lrange country 0 -1
blpop movies 10  # This will remove the element and block if not present
lpush movies Avengers  # from new Terminal
brpop movies Endgame
SORT country ALPHA   #
SORT country desc ALPHA
SORT lsit DESC  # If Numeric

Set

sadd technology Java
sadd technology Redis
sadd technology Spring
smembers technology
sadd technology Spring  # Wont allow as unique elements
smembers technology
scard technology  # get number of members in the Key Sets
sismember technology Java  # to check member is available or not  (Return 1 if available)
sissmember technology Node
spop technology   # Used to remove random member form the set
srem technology Spring   # remove particular member
smembers technology
sadd frontend HTML
sadd frontend Javascript
sadd frontend Nodejs
sadd technology Nodejs
sdiff technology frontend  # difference of two set
sdiffstore frontendTechDiff technology frontend  # stores in New Destination set
smembers frontendTechDiff
sinter technology frontend  # intersection in all the sets
sinter technology frontend frontendTechDiff
sinterstore fronttechinter technology frontend
smembers fronttechinter
smembers technology
smembers frontend
smove technology frontend Redis
smembers technology
smembers frontend
sunion technology frontend
suionstore newKey technology frontned

Zset (Sorted Sets)

zadd users 1 Shabbir 
zadd users 2 Steve
zrange users 0 -1
zrange users 0 -1 withscores
zcard users  # to get the count/cardinality of the Sets
zcount users -inf +inf   # all data with all ranks
zcount users 2 10
zadd users 3 Alex
zrange users 0 -1
zrem users Alex
zramge users 0 -1
zrevrange users 0 -1
zrank users Steve
zrevrank users Steve
zscore users Shabbir


zadd users 3 Alex 4 Ryan 5 Nimah 6 Felix  -- > Adding more Data

zrevrange users 0 -1 withscores

zrevrangebyscore users 5 2 withscores

zrange users 0 -1 withscores

zincrby users 2 Felix

zremrangebyscore users -inf 2

zrange users 0 -1 withscores

zremrangebyrank users 0 1

zrange users 0 -1 withscores

HyperLogLog

A HyperLogLog is a probabilistic data structure used to count unique values — or as it's referred to in mathematics: calculating the cardinality of a set. These values can be anything: for example, IP addresses for the visitors of a website, search terms, or email addresses

pfadd hll a b c d e f g h

pfadd hll r s t

pfcount hll

pfadd hll2 1 2 3 4 5 6

pfcount hll2

pfcount hll hll2

pfmerge hllmerge hll hll2

Hash

Last was HyperLogLog

hset myhash name Shabbir 
hget myhash name 
hset myhash email email@domain.com
hget myhash email
hkeys myhash
hvals myhash

hgetall myhash

hexists myhash name
hexists myhash name1  # will retuen 0 if not available
hlen myhash
hmset myhash age 25 country India
hmget myhash name email

hincrby myhash age 2
hincrbyfloat myhash age 1.5
hdel myhash country
hstrlen myhash name
hsetnx myhash country India

Transactions in Redis

multi
set name Kimi
get name
set a 1
set b 2
exec

###
multi
get a
get c
discard

Redis PubSub

# Subscriber
subscribe news
# Subscriber
subscribe news
# Publisher
publish news "New Breaking News"
publish hxllo "Hello"
publish ball "New ball"
pubsub channles
pubsub nubsub news
pubsub numpat
psubscribe news* h?llo b[ai]ll

Script

eval "redis.call('set',KEYS[1],ARGV[1])" 1 name1 Shabbir

eval "redis.call('mset',KEYS[1],ARGV[1],KEYS[2],ARGV[2])" 2 name lastnmae Shabbir Dawoodi

hmset country_cap India "New Delhi" USA "Washington, D.C." Russia Moscow Germany Berlin Japan Tokyo Italy Rome

zadd country 1 Italy 2 India 3 USA

zrange order 0 -1

eval "local order = redis.call('zrange', KEYS[1], 0, -1); return redis.call('hmget', KEYS[2], unpack(order));" 2 country country_cap

script load "local order = redis.call('zrange', KEYS[1], 0, -1); return redis.call('hmget', KEYS[2], unpack(order));"

evalsha 1807412636f2f95da7f3cdf6cb3bb0249e2587c7 2 country country_cap

script exists 1807412636f2f95da7f3cdf6cb3bb0249e2587c7

script flush

script exists 1807412636f2f95da7f3cdf6cb3bb0249e2587c7

Basic Connection and Security in Redis

Connection and Security

Flushall
ping
echo message
select 0
set name Shabbir
get name
select 1
get name
set name Dawoodi
client list
client setname ThisTerminal
client getname
client kill id

security

config set requirepass xxxx
auth xxxx
get name

GEOspatial

GEOADD maps 72.585022 23.033863 Ahmedabad 

GEOADD maps 72.877426 19.076090 Mumbai 77.580643 12.972442 Bangalore

zrange maps 0 -1

# 找出來的值可帶入 http://geohash.org/
GEOHASH maps Ahmedabad

# 找出經緯度
GEOPOS maps Mumbai

GEOADD maps 73.856255 18.516726 Pune 

# (km / mi / m)
GEODIST maps Mumbai Pune

# 找出半徑範圍的程式
GEORADIUS maps 72.585022 23.033863 1500 km

GEORADIUS maps 72.585022 23.033863 1500 km withcoord

GEORADIUS maps 72.585022 23.033863 1500 km withcoord withdist

GEORADIUSBYMEMBER maps Ahmedabad 300 km 

GEORADIUSBYMEMBER maps Ahmedabad 300 km withcoord 

GEORADIUSBYMEMBER maps Ahmedabad 300 km withcoord withdist  

GEORADIUSBYMEMBER maps Ahmedabad 300 km withcoord withdist  desc

GEORADIUSBYMEMBER maps Ahmedabad 300 km withcoord withdist  asc

Benchmarking

redis-benchmark -n 10000

redis-benchmark -h 127.0.0.1 -p 6379 -n 1000000    # With Default 3 Bytes payload

redis-benchmark -h 127.0.0.1 -p 6379 -n 1000000 -d 1000000    # With 100kb payload 

redis-benchmark -h 127.0.0.1 -p 6379 -n 1000000 -d 1000000 -c 200   # With 100kb payload and 200 clients

redis-benchmark -h 127.0.0.1 -p 6379 -t set -n 10000 -d 1000000

redis-benchmark -h 127.0.0.1 -p 6379 -t set -n 10000 -d 1000000 --csv

redis-benchmark -h 127.0.0.1 -p 6379 -t set, lpush -n 10000 -d 1000000

Different tools such as Memtier and RDBTools also available. You can also heck that out.
© Kimi Tsai all right reserved.            Updated : 2023-07-12 09:04:53

results matching ""

    No results matching ""

    results matching ""

      No results matching ""