How to use Azure Redis Cache with Python

This topic shows you how to get started with Azure Redis Cache using Python.

Prerequisites

Install redis-py.

Create a Redis cache on Azure

To create a cache, first sign in to the Azure portal, and click New > Databases > Redis Cache.

Note

If you don't have an Azure account, you can Open an Azure account for free in just a couple of minutes.

New cache

Note

In addition to creating caches in the Azure portal, you can also create them using Resource Manager templates, PowerShell, or Azure CLI.

In the New Redis Cache blade, specify the desired configuration for the cache.

Create cache

  • In Dns name, enter a unique cache name to use for the cache endpoint. The cache name 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.
  • For Subscription, select the Azure subscription that you want to use for the cache. If your account has only one subscription, it will be automatically selected and the Subscription drop-down will not be displayed.
  • In Resource group, select or create a resource group for your cache. For more information, see Using Resource groups to manage your Azure resources.
  • Use Location to specify the geographic location in which your cache is hosted. For the best performance, Microsoft strongly recommends that you create the cache in the same region as the cache client application.
  • Use Pricing Tier to select the desired cache size and features.
  • Redis cluster allows you to create caches larger than 53 GB and to shard data across multiple Redis nodes. For more information, see How to configure clustering for a Premium Azure Redis Cache.
  • Redis persistence offers the ability to persist your cache to an Azure Storage account. For instructions on configuring persistence, see How to configure persistence for a Premium Azure Redis Cache.
  • Virtual Network provides enhanced security and isolation by restricting access to your cache to only those clients within the specified Azure Virtual Network. You can use all the features of VNet such as subnets, access control policies, and other features to further restrict access to Redis. For more information, see How to configure Virtual Network support for a Premium Azure Redis Cache.
  • By default, non-SSL access is disabled for new caches. To enable the non-SSL port, check Unblock port 6379 (not SSL encrypted).

Once the new cache options are configured, click Create. It can take a few minutes for the cache to be created. To check the status, you can monitor the progress on the startboard. After the cache has been created, your new cache has a Running status and is ready for use with default settings.

Cache created

Retrieve the host name and access keys

To connect to an Azure Redis Cache instance, cache clients need the host name, ports, and keys of the cache. Some clients may refer to these items by slightly different names. To retrieve these items, browse to your cache in the Azure portal and click the desired item in the Resource menu.

Redis cache settings

Host name and ports

To access the host name and ports click Properties.

Redis cache properties

Access keys

To retrieve the access keys, click Access keys.

Redis cache access keys

Enable the non-SSL endpoint

Some Redis clients don't support SSL, and by default the non-SSL port is disabled for new Azure Redis Cache instances. At the time of this writing, the redis-py client doesn't support SSL.

To enable the non-SSL port, browse to your cache in the Azure portal and click Advanced settings. Click No for Allow access only via SSL on the Advanced settings blade and then click Save.

Redis cache settings

Add something to the cache and retrieve it

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

Replace <name> with your cache name and key with your access key.