Answers for "python redis"

1

python redis

import redis

# Read from standalone REDIS
rd = redis.Redis(host='localhost', 
                 port=6379, 
                 db=0)

# write a key
rd.set("Key1", "hello")
# read a key
rd.get("Key1")
Posted by: Guest on January-11-2022
1

python redis

from redis.sentinel import Sentinel

# This way you can connect to a REDIS cluster via its sentinels
sentinel = Sentinel([('SENTINEL-1', 26379),
                     ('SENTINEL-2', 26379),
                     ('SENTINEL-3', 26379)], 
                    socket_timeout=1, password="password", decode_responses=True)

# Get the current main
main = sentinel.master_for('main_key', socket_timeout=1)
# Get the current subordinates
subordinate = sentinel.slave_for('main_key', socket_timeout=1)

# Set some key on the main
# Writes work only on main
main.set("Py_key1", "hello")

# Read/Get the key from a Subordinate
test = subordinate.get("Py_key1")
print (test)

# Delete a key on the active (main)
main.delete("Py_key1")
Posted by: Guest on January-11-2022

Python Answers by Framework

Browse Popular Code Answers by Language