Skip to content

Commit 9b99bf9

Browse files
authored
Add set and get examples (#1916)
1 parent 30be3a3 commit 9b99bf9

File tree

2 files changed

+303
-0
lines changed

2 files changed

+303
-0
lines changed

docs/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Examples
88
examples/connection_examples
99
examples/ssl_connection_examples
1010
examples/search_json_examples
11+
examples/set_and_get_examples
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "a3b456e8",
6+
"metadata": {},
7+
"source": [
8+
"# Basic ```set``` and ```get``` operations"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "a59abd54",
14+
"metadata": {},
15+
"source": [
16+
"## Start off by connecting to the redis server\n",
17+
"\n",
18+
"To understand what ```decode_responses=True``` does, refer back to [this document](connection_examples.ipynb)"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 1,
24+
"id": "97aa8747",
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"data": {
29+
"text/plain": [
30+
"True"
31+
]
32+
},
33+
"execution_count": 1,
34+
"metadata": {},
35+
"output_type": "execute_result"
36+
}
37+
],
38+
"source": [
39+
"import redis \n",
40+
"\n",
41+
"r = redis.Redis(decode_responses=True)\n",
42+
"r.ping()"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"id": "218e137f",
48+
"metadata": {},
49+
"source": [
50+
"The most basic usage of ```set``` and ```get```"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 2,
56+
"id": "12992c68",
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"data": {
61+
"text/plain": [
62+
"True"
63+
]
64+
},
65+
"execution_count": 2,
66+
"metadata": {},
67+
"output_type": "execute_result"
68+
}
69+
],
70+
"source": [
71+
"r.set(\"full_name\", \"john doe\")"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 3,
77+
"id": "bc9f3888",
78+
"metadata": {},
79+
"outputs": [
80+
{
81+
"data": {
82+
"text/plain": [
83+
"1"
84+
]
85+
},
86+
"execution_count": 3,
87+
"metadata": {},
88+
"output_type": "execute_result"
89+
}
90+
],
91+
"source": [
92+
"r.exists(\"full_name\")"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 4,
98+
"id": "dc64aec8",
99+
"metadata": {},
100+
"outputs": [
101+
{
102+
"data": {
103+
"text/plain": [
104+
"'john doe'"
105+
]
106+
},
107+
"execution_count": 4,
108+
"metadata": {},
109+
"output_type": "execute_result"
110+
}
111+
],
112+
"source": [
113+
"r.get(\"full_name\")"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"id": "353d334f",
119+
"metadata": {},
120+
"source": [
121+
"We can override the existing value by calling ```set``` method for the same key"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 5,
127+
"id": "c61389ce",
128+
"metadata": {},
129+
"outputs": [
130+
{
131+
"data": {
132+
"text/plain": [
133+
"True"
134+
]
135+
},
136+
"execution_count": 5,
137+
"metadata": {},
138+
"output_type": "execute_result"
139+
}
140+
],
141+
"source": [
142+
"r.set(\"full_name\", \"overridee!\")"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 6,
148+
"id": "5e34a520",
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"data": {
153+
"text/plain": [
154+
"'overridee!'"
155+
]
156+
},
157+
"execution_count": 6,
158+
"metadata": {},
159+
"output_type": "execute_result"
160+
}
161+
],
162+
"source": [
163+
"r.get(\"full_name\")"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"id": "4ae3747b",
169+
"metadata": {},
170+
"source": [
171+
"It is also possible to pass an expiration value to the key by using ```setex``` method"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 7,
177+
"id": "9b87449b",
178+
"metadata": {},
179+
"outputs": [
180+
{
181+
"data": {
182+
"text/plain": [
183+
"True"
184+
]
185+
},
186+
"execution_count": 7,
187+
"metadata": {},
188+
"output_type": "execute_result"
189+
}
190+
],
191+
"source": [
192+
"r.setex(\"important_key\", 100, \"important_value\")"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 8,
198+
"id": "a11fe79d",
199+
"metadata": {},
200+
"outputs": [
201+
{
202+
"data": {
203+
"text/plain": [
204+
"100"
205+
]
206+
},
207+
"execution_count": 8,
208+
"metadata": {},
209+
"output_type": "execute_result"
210+
}
211+
],
212+
"source": [
213+
"r.ttl(\"important_key\")"
214+
]
215+
},
216+
{
217+
"cell_type": "markdown",
218+
"id": "87c16991",
219+
"metadata": {},
220+
"source": [
221+
"A dictionary can be inserted like this"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": 9,
227+
"id": "3cfa5713",
228+
"metadata": {},
229+
"outputs": [
230+
{
231+
"data": {
232+
"text/plain": [
233+
"True"
234+
]
235+
},
236+
"execution_count": 9,
237+
"metadata": {},
238+
"output_type": "execute_result"
239+
}
240+
],
241+
"source": [
242+
"dict_data = {\n",
243+
" \"employee_name\": \"Adam Adams\",\n",
244+
" \"employee_age\": 30,\n",
245+
" \"position\": \"Software Engineer\",\n",
246+
"}\n",
247+
"\n",
248+
"r.mset(dict_data)"
249+
]
250+
},
251+
{
252+
"cell_type": "markdown",
253+
"id": "6d32dbee",
254+
"metadata": {},
255+
"source": [
256+
"To get multiple keys' values, we can use mget. If a non-existing key is also passed, Redis return None for that key"
257+
]
258+
},
259+
{
260+
"cell_type": "code",
261+
"execution_count": 10,
262+
"id": "45ce1231",
263+
"metadata": {},
264+
"outputs": [
265+
{
266+
"data": {
267+
"text/plain": [
268+
"['Adam Adams', '30', 'Software Engineer', None]"
269+
]
270+
},
271+
"execution_count": 10,
272+
"metadata": {},
273+
"output_type": "execute_result"
274+
}
275+
],
276+
"source": [
277+
"r.mget(\"employee_name\", \"employee_age\", \"position\", \"non_existing\")"
278+
]
279+
}
280+
],
281+
"metadata": {
282+
"kernelspec": {
283+
"display_name": "Python 3 (ipykernel)",
284+
"language": "python",
285+
"name": "python3"
286+
},
287+
"language_info": {
288+
"codemirror_mode": {
289+
"name": "ipython",
290+
"version": 3
291+
},
292+
"file_extension": ".py",
293+
"mimetype": "text/x-python",
294+
"name": "python",
295+
"nbconvert_exporter": "python",
296+
"pygments_lexer": "ipython3",
297+
"version": "3.9.5"
298+
}
299+
},
300+
"nbformat": 4,
301+
"nbformat_minor": 5
302+
}

0 commit comments

Comments
 (0)