CentOS Stream 8
Sponsored Link

Redis 6 : Use on Python
2021/06/28
 
This is an example to use Redis on Python 3.
[1] Install Python 3 Redis Client Library.
# install from EPEL

[root@www ~]#
dnf --enablerepo=epel -y install python3-redis
[2] Basic usage on Python.
[cent@www ~]$
vi use_redis.py
import redis

client = redis.StrictRedis(host='127.0.0.1', port=6379, db=0, password='password')

# set and get Key
client.set('key01', 'value01')
print('key01.value :', client.get('key01'))

# append and get Key
client.append('key01', ',value02')
print('key01.value :', client.get('key01'))

client.set('key02', 1)

# increment
client.incr('key02', 100)
print('key02.value :', client.get('key02'))

# decrement
client.decr("key02", 51)
print('key02.value :', client.get('key02'))

# list
client.lpush('list01', 'value01', 'value02', 'value03')
print('list01.value :', client.lrange('list01', '0', '2'))

# hash
client.hmset('hash01', {'key01': 'value01', 'key02': 'value02', 'key03': 'value03'})
print('hash01.value :', client.hmget('hash01', ['key01', 'key02', 'key03']))

# set
client.sadd('set01', 'member01', 'member02', 'member03')
print('set01.value :', client.smembers('set01'))

# run

[cent@www ~]$
python3 use_redis.py

key01.value : b'value01'
key01.value : b'value01,value02'
key02.value : b'101'
key02.value : b'50'
list01.value : [b'value03', b'value02', b'value01']
hash01.value : [b'value01', b'value02', b'value03']
set01.value : {b'member03', b'member01', b'member02'}
Matched Content