Thursday, February 17, 2022

How To Use Redis With Python | Example To use Redis on Python 3

 in This Tutorial you will Learn " How To Use Redis With Python | Example To use Redis on Python 3"

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker.
Python is a popular programming language.
_________________________________________________________________________________________
Server - Os:  Rocky Linux 8.5  64Bit      | IP -192.168.1.60        |Hostname - server.yourdomain.com
_________________________________________________________________________________________
cat /etc/system-release ; hostname ; hostname -I ; dnf groupinstall "Development Tools" -y
dnf install epel-release -y
dnf makecache ; dnf install redis python3-redis -y
systemctl start redis ; systemctl enable redis

gedit  /etc/redis.conf &>/dev/null
# requirepass password

firewall-cmd --add-service=redis --permanent ; firewall-cmd --reload
dnf install python3 -y
alternatives --set python /usr/bin/python3
pip3 install redis
________________________________________________________________________________________
Example -1

nano use_redis.py
# Sample Python program that uses Redis-py to connect to a Redis Server
import redis

# Create a redis client
redisClient = redis.StrictRedis(host='127.0.0.1',
                                port=6379,
                                db=0)
# Set a string value for the key named 'Language'
redisClient.set('Language', 'Python 2.x')
 
# Get the value for the key and print it
print(redisClient.get('Language'))

# Change the value of they key
redisClient.set('Language', 'Python 3.x')

# Get the new value of the key and print it
print(redisClient.get('Language'))

python3 use_redis.py
________________________________________________________________________________________
Example -2

nano use_redistwo.py
import redis

redis = redis.Redis(
     host= '127.0.0.1',
     port= '6379')

redis.set('mykey', 'Hello from Python!')
value = redis.get('mykey')
print(value)

redis.zadd('vehicles', {'car' : 0})
redis.zadd('vehicles', {'bike' : 0})
vehicles = redis.zrange('vehicles', 0, -1)
print(vehicles)

python3 use_redistwo.py
________________________________________________________________________________________








No comments:

Post a Comment