Working with Redis in Python
cuongld2
Posted on December 25, 2019
I.What is Redis:
Redis is a NoSQL databases that is extremely fast, support caching and store value in different format.
Sometimes, if your data is uncategorized or you want to get it fast, you should think of Redis.
II.Installation:
Please go to Redis-official site for download and install Redis in your local environment.
After that, to check if it's working.
redis-server
You should see the following like:
After that, to access to the local running server via command line, you can use
redis-cli
III.Work with remote redis via command line:
For example you need to work with the redis-cluster remote (Check out what is redis-cluster, you would need to know what is the domain name, what is the password for authen..
- Connect The command would be like
redis-cli -h host_name -a pass_word -c
-h for the hostname
-a is for the password
-c is for automatically redirect if the node is moved.
- Common command: keys * : to get all the key info: to get information about redis-cluster get: to get the value of key
IV.Sample project for working with Redis:
I write some sample code for working with Redis-cluster using redis-cluster-python.
def test_masters_slaves():
from rediscluster import RedisCluster
nodes = [{"host": f"hostname",
"port": "6379"}]
r = RedisCluster(startup_nodes=nodes, password="xxxxx", skip_full_coverage_check=True, decode_responses=u'utf-8')
all_keys = r.keys("*")
print(f"\nKeys are : {all_keys}")
print(r.get('coccoc-music-api:ALL_SOURCES'))
You can checkout the source code in here.
Hope this helps!
Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :
This will help me to contributing more valued contents.
Posted on December 25, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.