Quickstart: Use Azure Cache for Redis with Python

Introduction

This quickstart shows how to connect to an Azure Cache for Redis with Python to read and write to a cache.

Python test completed

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

Create an Azure Cache for Redis on Azure

  1. To create a cache, first sign in to the Azure portal. Then select Create a resource > Databases > Azure Cache for Redis.

    New cache

  2. In New Azure Cache for Redis, configure the settings for your new cache.

    Setting Suggested value Description
    DNS name Globally unique name The cache name. It must be a string between 1 and 63 characters and contain only numbers, letters, and the - character. The cache name cannot start or end with the - character, and consecutive - characters are not valid.
    Subscription Your subscription The subscription under which this new Azure Cache for Redis instance is created.
    Resource group TestResources Name for the new resource group in which to create your cache. By putting all the resources for an app in a group, you can manage them together. For example, deleting the resource group deletes all resources that are associated with the app.
    Location East US Choose a region near to other services that will use your cache.
    Pricing tier Basic C0 (250 MB Cache) The pricing tier determines the size, performance, and features that are available for the cache. For more information, see Azure Cache for Redis Overview.
    Pin to dashboard Selected Pin the new cache to your dashboard to make it easy to find.

    Create cache

  3. After the new cache settings are configured, select Create.

    It can take a few minutes for the cache to be created. To check the status, you can monitor the progress on the dashboard. After the cache has been created, it shows the status Running, and is ready for use.

    Cache created

Retrieve host name, ports, and access keys by using the Azure portal

When connecting to an Azure Cache for Redis instance, cache clients need the host name, ports, and a key for the cache. Some clients might refer to these items by slightly different names. You can retrieve this information in the Azure portal.

To retrieve the access keys and host name

  1. To retrieve the access keys by using the Azure portal, browse to your cache and select Access keys.

    Azure Cache for Redis keys

  2. To retrieve the host name and ports, select Properties.

    Azure Cache for Redis properties

Install redis-py

Redis-py is a Python interface to Azure Cache for Redis. Use the Python packages tool, pip, to install the redis-py package.

The following example uses pip3 for Python3 to install the redis-py package on Windows 10 using a Visual Studio 2017 Developer Command prompt running with elevated Administrator privileges.

pip3 install redis

Install redis-py

Read and write to the cache

Run Python and test using the cache from the command line. Replace <Your Host Name> and <Your Access Key> with the values for your Azure Cache for Redis.

>>> import redis
>>> r = redis.StrictRedis(host='<Your Host Name>.redis.cache.windows.net',
        port=6380, db=0, password='<Your Access Key>', ssl=True)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
b'bar'

Create a Python script

Create a new script text file named PythonApplication1.py.

Add the following script to PythonApplication1.py and save the file. This script will test the cache access. Replace <Your Host Name> and <Your Access Key> with the values for your Azure Cache for Redis.

import redis

myHostname = "<Your Host Name>.redis.cache.windows.net"
myPassword = "<Your Access Key>"

r = redis.StrictRedis(host=myHostname, port=6380,password=myPassword,ssl=True)

result = r.ping()
print("Ping returned : " + str(result))

result = r.set("Message", "Hello!, The cache is working with Python!")
print("SET Message returned : " + str(result))

result = r.get("Message")
print("GET Message returned : " + result.decode("utf-8"))

result = r.client_list()
print("CLIENT LIST returned : ") 
for c in result:
    print("id : " + c['id'] + ", addr : " + c['addr'])

Run the script with Python.

Python test completed

Clean up resources

If you will be continuing with another tutorial, you can keep the resources created in this quickstart and reuse them.

Otherwise, if you are finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.

Important

Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually from their respective blades instead of deleting the resource group.

Sign in to the Azure portal and click Resource groups.

In the Filter by name... textbox, type the name of your resource group. The instructions for this article used a resource group named TestResources. On your resource group in the result list, click ... then Delete resource group.

Delete

You will be asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and click Delete.

After a few moments, the resource group and all of its contained resources are deleted.

Next steps