set
: create stringget
: fetch string, nil
if it doesn’t existsappend
: append to the existing string variabledel
: delete stringexists
: returns 1 if string exists else 0incr
: increment string by 1. It works on if string is actually an integer. It doesn’t work with float or alphanumeric character.decr
: decrement by 1. It works on if string is actually an integer. It doesn’t work with float or alphanumeric character.incrby
: increment by a numberdecrby
: decrement by a numberstrlen
: returns length of stringgetrange
: returns a slice of the string. Negative indexing also works just like pythonsetrange
: set a value of a string over a range starting from an offset127.0.0.1:6379> set a 5
OK
127.0.0.1:6379> get a
"5"
127.0.0.1:6379> append a 2
(integer) 2
127.0.0.1:6379> get a
"52"
127.0.0.1:6379> exists a
(integer) 1
127.0.0.1:6379> incr a
(integer) 53
127.0.0.1:6379> incrby a 100
(integer) 153
127.0.0.1:6379> get a
"153"
127.0.0.1:6379> decr a
(integer) 152
127.0.0.1:6379> decrby a 100
(integer) 52
127.0.0.1:6379> get a
"52"
127.0.0.1:6379> strlen a
(integer) 2
127.0.0.1:6379> del a
(integer) 1
127.0.0.1:6379> exists a
(integer) 0
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379>
127.0.0.1:6379> set a "This is a string"
OK
127.0.0.1:6379> getrange a 0 3
"This"
127.0.0.1:6379> getrange a -6 -1
"string"
127.0.0.1:6379> setrange a 0 "That"
(integer) 16
127.0.0.1:6379> get a
"That is a string"
127.0.0.1:6379> set b what is this
(error) ERR syntax error
127.0.0.1:6379>
import redis
r = redis.Redis()
print(r.set('a','5'))
print(r.get('a'))
print(r.append('a','2'))
print(r.get('a'))
print(r.exists('a'))
print(r.incr('a'))
print(r.incrby('a',100))
print(r.get('a'))
print(r.decr('a'))
print(r.decrby('a',100))
print(r.get('a'))
print(r.strlen('a'))
print(r.delete('a'))
print(r.exists('a'))
print(r.get('a'))
print(r.set('a',"This is a string"))
print(r.getrange('a',0,3))
print(r.getrange('a',-6,-1))
print(r.setrange('a',0,'That'))
print(r.get('a'))
OUTPUT :
True
b'5'
2
b'52'
1
53
153
b'153'
152
52
b'52'
2
1
0
None
True
b'This'
b'string'
16
b'That is a string'
Create
rpush
: It will create the list. If it already exists, then it will append the items to the list
Fetch
lrange
:
To fetch the whole list
lrange list_name 0 -1
To fetch first 5 items
lrange list_name 0 4
Unlike python, it does include the last item of the mentioned index.
To fetch item of a specific index, if the index doesn’t exist it’ll give empty array
lrange list_name 4 4
llen
: gives the length of the list
lindex
: gives the element by its index
lindex a 2
Update
lpush
: append elements to the left of the listrpush
: append elements to the right of the listDelete
lpop
: remove elements from the left of the list.
lpop a
By default it only removes single element from the list
lpop a 2
If we mention the count, it will remove that much elements from the list.
rpop
: remove elements from the right of the list
rpop a
By default it only removes single element from the list
rpop a 2
If we mention the count, it will remove that much elements from the list.
import redis
r = redis.Redis()
print(r.rpush('a',*[1,2,3]))
print(r.lrange('a',0,-1))
print(r.lrange('a',0,2))
print(r.lrange('a',2,2))
print(r.llen('a'))
print(r.lindex('a',2))
print(r.lpush('a',*[-1,0]))
print(r.rpush('a',*[4,5,6]))
print(r.lpop('a'))
print(r.rpop('a'))
OUTPUT :
4
[b'2', b'1', b'2', b'3']
[b'2', b'1', b'2']
[b'2']
4
b'2'
6
9
b'0'
b'6'
Python implementation of rpop and lpop only deletes one element at a time for some reason
Create/Update :
hmset/hset
: create the hash/ updates the hashFetch :
hgetall
: Fetch all the field values
hget
: Fetch only one field at a time
hmget
: Fetch multiple fields at a time
hexists
: Check if a field exists in a hash
hvals
: Gives all the values of a key
hkeys
: Gives all the fields of a key
Delete :
hdel
127.0.0.1:6379> hmset details name rs alias albatross location Neuss country Germany
OK
127.0.0.1:6379> hgetall details
1) "name"
2) "rs"
3) "alias"
4) "albatross"
5) "location"
6) "Neuss"
7) "country"
8) "Germany"
127.0.0.1:6379> hget details name
"rs"
127.0.0.1:6379> hexists details alias
(integer) 1
127.0.0.1:6379> hmget details name location
1) "rs"
2) "Neuss"
127.0.0.1:6379> hvals details
1) "rs"
2) "albatross"
3) "Neuss"
4) "Germany"
127.0.0.1:6379> hkeys details
1) "name"
2) "alias"
3) "location"
4) "country"
127.0.0.1:6379> hmset details name robin city Dusseldorf
OK
127.0.0.1:6379> hgetall details
1) "name"
2) "robin"
3) "alias"
4) "albatross"
5) "location"
6) "Neuss"
7) "country"
8) "Germany"
9) "city"
10) "Dusseldorf"
127.0.0.1:6379> hdel details name
(integer) 1
127.0.0.1:6379> hgetall details
1) "alias"
2) "albatross"
3) "location"
4) "Neuss"
5) "country"
6) "Germany"
7) "city"
8) "Dusseldorf"
import redis
r = redis.Redis()
info = {
'name' : 'rs',
'alias' : 'albatross',
'location' : 'Neuss',
'country' : 'Germany'
}
print(r.hset("details",mapping=info))
print(r.hgetall("details"))
print(r.hget("details","name"))
print(r.hexists("details","alias"))
print(r.hmget("details",["name","location"]))
print(r.hvals("details"))
print(r.hkeys("details"))
print(r.hset("details",mapping = {"name" : "robin", "city" : "Dusseldorf"}))
print(r.hgetall("details"))
OUTPUT :
0
{b'alias': b'albatross', b'location': b'Neuss', b'country': b'Germany', b'city': b'Dusseldorf', b'name': b'rs', b'surname': b'siwach'}
b'rs'
True
[b'rs', b'Neuss']
[b'albatross', b'Neuss', b'Germany', b'Dusseldorf', b'rs', b'siwach']
[b'alias', b'location', b'country', b'city', b'name', b'surname']
0
{b'alias': b'albatross', b'location': b'Neuss', b'country': b'Germany', b'city': b'Dusseldorf', b'name': b'robin', b'surname': b'siwach'}
Create :
sadd
: create/appendFetch :
smembers
: fetch the elementsscard
: Returns total no of elementsDiff
sdiff
: gives difference of setsdiffstore
: stores difference in third setUnion
sunion
: gives union of setssunionstore
: stores union in third setIntersection
sinter
: gives intersection of the setssinterstore
: stores intersection into the third setDelete
srem
: removes element from the setMove
smove
: moves element from one set to another127.0.0.1:6379> sadd myset 10 20 30 40
(integer) 4
127.0.0.1:6379> smembers myset
1) "10"
2) "20"
3) "30"
4) "40"
127.0.0.1:6379> sadd myset 30
(integer) 0
127.0.0.1:6379> smembers myset
1) "10"
2) "20"
3) "30"
4) "40"
127.0.0.1:6379> sadd myset 50
(integer) 1
127.0.0.1:6379> smembers myset
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
127.0.0.1:6379> scard myset
(integer) 5
127.0.0.1:6379> sadd myset2 40 50 60 70 80
(integer) 5
127.0.0.1:6379> smembers myset2
1) "40"
2) "50"
3) "60"
4) "70"
5) "80"
127.0.0.1:6379> sdiff myset myset2
1) "10"
2) "20"
3) "30"
127.0.0.1:6379> sdiffstore myset3 myset myset2
(integer) 3
127.0.0.1:6379> smembers myset3
1) "10"
2) "20"
3) "30"
127.0.0.1:6379> sunion myset myset2
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
7) "70"
8) "80"
127.0.0.1:6379> sunionstore myset4 myset myset2
(integer) 8
127.0.0.1:6379> smembers myset4
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
7) "70"
8) "80"
127.0.0.1:6379> srem myset4 80
(integer) 1
127.0.0.1:6379> smembers myset4
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
7) "70"
127.0.0.1:6379> srem myset4 70 60 50
(integer) 3
127.0.0.1:6379> smembers myset4
1) "10"
2) "20"
3) "30"
4) "40"
127.0.0.1:6379> smembers myset
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
7) "70"
8) "80"
127.0.0.1:6379> smembers myset4
1) "10"
2) "20"
3) "30"
4) "40"
127.0.0.1:6379> sinter myset myset4
1) "10"
2) "20"
3) "30"
4) "40"
127.0.0.1:6379> sinterstore myset5 myset myset4
(integer) 4
127.0.0.1:6379> smembers myset5
1) "10"
2) "20"
3) "30"
4) "40"
127.0.0.1:6379> smove myset myset5 80
(integer) 1
127.0.0.1:6379> smembers myset
1) "10"
2) "20"
3) "30"
4) "40"
5) "50"
6) "60"
7) "70"
import redis
r = redis.Redis()
print(r.sadd('myset','10'))
print(r.sadd('myset','20'))
print(r.sadd('myset','30'))
print(r.sadd('myset','40'))
print(r.smembers('myset'))
print(r.sadd('myset',30))
print(r.smembers('myset'))
print(r.sadd('myset',50))
print(r.smembers('myset'))
print(r.scard('myset'))
print(r.sadd('myset2','40'))
print(r.sadd('myset2','50'))
print(r.sadd('myset2','60'))
print(r.sadd('myset2','70'))
print(r.sadd('myset2','80'))
print(r.smembers('myset2'))
print(r.sdiff('myset','myset2'))
print(r.sdiffstore('myset3','myset','myset2'))
print(r.smembers('myset3'))
print(r.sunion('myset','myset2'))
print(r.sunionstore('myset4','myset','myset2'))
print(r.smembers('myset4'))
print(r.srem('myset4',80))
print(r.smembers('myset4'))
print(r.sinter('myset','myset4'))
print(r.sinterstore('myset5','myset','myset4'))
OUTPUT :
1
1
1
1
{b'20', b'30', b'40', b'10'}
0
{b'20', b'30', b'40', b'10'}
1
{b'20', b'30', b'40', b'50', b'10'}
5
1
1
1
1
1
{b'60', b'40', b'80', b'70', b'50'}
{b'20', b'30', b'10'}
3
{b'20', b'30', b'10'}
{b'20', b'30', b'40', b'60', b'80', b'70', b'50', b'10'}
8
{b'20', b'30', b'40', b'60', b'80', b'70', b'50', b'10'}
1
{b'20', b'30', b'40', b'60', b'70', b'50', b'10'}
{b'20', b'30', b'40', b'50', b'10'}
5
Each set member is assigned a numerical value score. Members are sorted on the basis of score.
zadd
: Create/appendzrange
: list members using rangezcard
: returns total no of elements in the sortedsetzcount
: returns no of elements between rangezrem
: removes element from the setzrank
: gives index of the memberzrevrank
: provides the reverse rankzscore
: provides the score of the memberzrangebyscore
: gives elements by score rangeSame score can be assigned to be multiple elements.
127.0.0.1:6379> zadd myset 1 a 2 b 3 c 5 d
(integer) 4
127.0.0.1:6379> zrange myset 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> zadd myset 100 e
(integer) 1
127.0.0.1:6379> zrange myset 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> zcard myset
(integer) 5
127.0.0.1:6379> zcount myset 1 3
(integer) 3
127.0.0.1:6379> zcount myset 1 101
(integer) 5
127.0.0.1:6379> zcount myset 1 100
(integer) 5
127.0.0.1:6379> zcount myset 1 99
(integer) 4
127.0.0.1:6379> zrem myset b
(integer) 1
127.0.0.1:6379> zrank myset a
(integer) 0
127.0.0.1:6379> zrank myset e
(integer) 3
127.0.0.1:6379> zrevrank myset a
(integer) 3
127.0.0.1:6379> zrevrank myset e
(integer) 0
127.0.0.1:6379> zscore myset a
"1"
127.0.0.1:6379> zscore myset d
"5"
127.0.0.1:6379> zscore myset e
"100"
import redis
r = redis.Redis()
d = {
'a' : 1,
'b' : 2,
'c' : 3,
'd' : 5
}
print(r.zadd('myset',mapping=d))
print(r.zrange('myset',0,-1))
print(r.zadd('myset',mapping={'e' : 100}))
print(r.zrange('myset',0,-1))
print(r.zcard('myset'))
print(r.zcount('myset',1,3))
print(r.zcount('myset',1,101))
print(r.zcount('myset',1,99))
print(r.zrem('myset','b'))
print(r.zrank('myset','a'))
print(r.zrank('myset','e'))
print(r.zrevrank('myset','a'))
print(r.zrevrank('myset','e'))
print(r.zscore('myset','a'))
print(r.zscore('myset','d'))
print(r.zscore('myset','e'))
OUTPUT :
1
[b'a', b'b', b'c', b'd', b'e']
0
[b'a', b'b', b'c', b'd', b'e']
5
3
5
4
1
0
3
3
0
1.0
5.0
100.0