summaryrefslogtreecommitdiff
path: root/docs/examples/set_and_get_examples.ipynb
diff options
context:
space:
mode:
authordogukanteber <47397379+dogukanteber@users.noreply.github.com>2022-02-06 16:08:51 +0300
committerGitHub <noreply@github.com>2022-02-06 15:08:51 +0200
commit9b99bf98375574540a5f1642d0fd7900781d0761 (patch)
tree35fe6c505aa3b88881c6878499f3f1725484c627 /docs/examples/set_and_get_examples.ipynb
parent30be3a35b18c0041e96e6a1b17fa6b323253d2b2 (diff)
downloadredis-py-9b99bf98375574540a5f1642d0fd7900781d0761.tar.gz
Add set and get examples (#1916)
Diffstat (limited to 'docs/examples/set_and_get_examples.ipynb')
-rw-r--r--docs/examples/set_and_get_examples.ipynb302
1 files changed, 302 insertions, 0 deletions
diff --git a/docs/examples/set_and_get_examples.ipynb b/docs/examples/set_and_get_examples.ipynb
new file mode 100644
index 0000000..e45c72a
--- /dev/null
+++ b/docs/examples/set_and_get_examples.ipynb
@@ -0,0 +1,302 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "a3b456e8",
+ "metadata": {},
+ "source": [
+ "# Basic ```set``` and ```get``` operations"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a59abd54",
+ "metadata": {},
+ "source": [
+ "## Start off by connecting to the redis server\n",
+ "\n",
+ "To understand what ```decode_responses=True``` does, refer back to [this document](connection_examples.ipynb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "97aa8747",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 1,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "import redis \n",
+ "\n",
+ "r = redis.Redis(decode_responses=True)\n",
+ "r.ping()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "218e137f",
+ "metadata": {},
+ "source": [
+ "The most basic usage of ```set``` and ```get```"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "12992c68",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.set(\"full_name\", \"john doe\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "bc9f3888",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.exists(\"full_name\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "dc64aec8",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'john doe'"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.get(\"full_name\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "353d334f",
+ "metadata": {},
+ "source": [
+ "We can override the existing value by calling ```set``` method for the same key"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "c61389ce",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.set(\"full_name\", \"overridee!\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "5e34a520",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'overridee!'"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.get(\"full_name\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "4ae3747b",
+ "metadata": {},
+ "source": [
+ "It is also possible to pass an expiration value to the key by using ```setex``` method"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "9b87449b",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.setex(\"important_key\", 100, \"important_value\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "a11fe79d",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "100"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.ttl(\"important_key\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "87c16991",
+ "metadata": {},
+ "source": [
+ "A dictionary can be inserted like this"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "3cfa5713",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "dict_data = {\n",
+ " \"employee_name\": \"Adam Adams\",\n",
+ " \"employee_age\": 30,\n",
+ " \"position\": \"Software Engineer\",\n",
+ "}\n",
+ "\n",
+ "r.mset(dict_data)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "6d32dbee",
+ "metadata": {},
+ "source": [
+ "To get multiple keys' values, we can use mget. If a non-existing key is also passed, Redis return None for that key"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "45ce1231",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['Adam Adams', '30', 'Software Engineer', None]"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "r.mget(\"employee_name\", \"employee_age\", \"position\", \"non_existing\")"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.9.5"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}