You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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.
37
35
38
36
The following example used `pip3` for Python 3 to install `redis-py` on Windows 11 from an Administrator command prompt.
39
37
40
38
:::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.":::
## 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
+
defhello_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.":::
## Read and write to the cache from the command line
101
+
43
102
44
103
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`.
45
104
@@ -88,6 +147,8 @@ Run `PythonApplication1.py` with Python. You should see results like the followi
88
147
89
148
:::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.":::
90
149
150
+
---
151
+
91
152
## Clean up resources
92
153
93
154
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