DEV Community

cuongld2
cuongld2

Posted on • Updated on

Working with Redis in Python

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

Enter fullscreen mode Exit fullscreen mode

You should see the following like:

Alt Text

After that, to access to the local running server via command line, you can use

redis-cli
Enter fullscreen mode Exit fullscreen mode

Alt Text

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..

  1. Connect The command would be like
redis-cli -h host_name -a pass_word -c
Enter fullscreen mode Exit fullscreen mode

-h for the hostname

-a is for the password

-c is for automatically redirect if the node is moved.

  1. 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'))
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)