Skip to content

Commit c6d50c8

Browse files
authored
Update cache-python-get-started.md
1 parent eff9ba2 commit c6d50c8

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

articles/azure-cache-for-redis/cache-python-get-started.md

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,76 @@ If you want to skip straight to the code, see the [Python quickstart](https://gi
2929
## Create an Azure Cache for Redis instance
3030
[!INCLUDE [redis-cache-create](~/reusable-content/ce-skilling/azure/includes/azure-cache-for-redis/includes/redis-cache-create.md)]
3131

32-
[!INCLUDE [redis-cache-create](includes/redis-cache-access-keys.md)]
33-
34-
## Install redis-py
32+
## Install redis-py library
3533

3634
[Redis-py](https://pypi.org/project/redis/) is a Python interface to Azure Cache for Redis. Use the Python packages tool, `pip`, to install the `redis-py` package from a command prompt.
3735

3836
The following example used `pip3` for Python 3 to install `redis-py` on Windows 11 from an Administrator command prompt.
3937

4038
:::image type="content" source="media/cache-python-get-started/cache-python-install-redis-py.png" alt-text="Screenshot of a terminal showing an install of redis-py interface to Azure Cache for Redis.":::
4139

42-
## Read and write to the cache
40+
## [EntraID Authentication (recommended)](#tab/entraid)
41+
42+
## Enable EntraID and add a User or Service Principal
43+
<--Fran, we probably need an include file on enabling EntraID-->
44+
Blah blah blah, do the steps listed [here](cache-azure-active-directory-for-authentication.md)
45+
46+
## Install the Microsoft Authentication Library
47+
The [Microsoft Authentication Library (MSAL)](../../entra/identity-platform/msal-overview.md) allows you to acquire security tokens from Microsoft identity to authenticate users. There is a [Python Azure idenitty client library](../../python/api/overview/azure/identity-readme.md) available that uses MSAL to provide token authentication support. Install this library using `pip`:
48+
49+
```python
50+
pip install azure-identity
51+
```
52+
53+
## Create a sample python app
54+
Create a new text file, add the following script, and save the file as `PythonApplication1.py`. Replace `<Your Host Name>` with the value from your Azure Cache for Redis instance. Your host name is of the form `<DNS name>.redis.cache.windows.net`. Replace `<Your Username>` with the values from your EntraID user.
55+
56+
```python
57+
import redis
58+
from azure.identity import DefaultAzureCredential
59+
60+
scope = "https://redis.azure.com/.default"
61+
host = "<Your Host Name>" # Required
62+
port = 6380 # Required
63+
user_name = "<Your Username>" # Required
64+
65+
66+
def hello_world():
67+
cred = DefaultAzureCredential()
68+
token = cred.get_token(scope)
69+
r = redis.Redis(host=host,
70+
port=port,
71+
ssl=True, # ssl connection is required.
72+
username=user_name,
73+
password=token.token,
74+
decode_responses=True)
75+
result = r.ping()
76+
print("Ping returned : " + str(result))
77+
78+
result = r.set("Message", "Hello!, The cache is working with Python!")
79+
print("SET Message returned : " + str(result))
80+
81+
result = r.get("Message")
82+
print("GET Message returned : " + result)
83+
84+
result = r.client_list()
85+
print("CLIENT LIST returned : ")
86+
for c in result:
87+
print(f"id : {c['id']}, addr : {c['addr']}")
88+
89+
if __name__ == '__main__':
90+
hello_world()
91+
```
92+
93+
Run `PythonApplication1.py` with Python. You should see results like the following example:
94+
95+
:::image type="content" source="media/cache-python-get-started/cache-python-completed.png" alt-text="Screenshot of a terminal showing a Python script to test cache access.":::
96+
97+
## [Access Key Authentication](#tab/accesskey)
98+
[!INCLUDE [redis-cache-create](includes/redis-cache-access-keys.md)]
99+
100+
## Read and write to the cache from the command line
101+
43102

44103
Run [Python from the command line](https://docs.python.org/3/faq/windows.html#id2) to test your cache. First, initiate the python interpreter in your command line by typing `py`, and then use the following code. Replace `<Your Host Name>` and `<Your Access Key>` with the values from your Azure Cache for Redis instance. Your host name is of the form `<DNS name>.redis.cache.windows.net`.
45104

@@ -88,6 +147,8 @@ Run `PythonApplication1.py` with Python. You should see results like the followi
88147

89148
:::image type="content" source="media/cache-python-get-started/cache-python-completed.png" alt-text="Screenshot of a terminal showing a Python script to test cache access.":::
90149

150+
---
151+
91152
## Clean up resources
92153

93154
If you're finished with the Azure resource group and resources you created in this quickstart, you can delete them to avoid charges.

0 commit comments

Comments
 (0)