From 49cbfdbbbe8963ab8250e6ea43c91d5ad676c3cc Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Sun, 30 Jan 2022 17:55:30 +0300 Subject: [PATCH 1/2] Add set and get examples --- docs/examples/set_and_get_examples.ipynb | 302 +++++++++++++++++++++++ 1 file changed, 302 insertions(+) create mode 100644 docs/examples/set_and_get_examples.ipynb diff --git a/docs/examples/set_and_get_examples.ipynb b/docs/examples/set_and_get_examples.ipynb new file mode 100644 index 0000000000..e45c72a665 --- /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 +} From 1e42d87e326ea30cebd917b359ecd0307edb7b6d Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Mon, 31 Jan 2022 15:50:10 +0300 Subject: [PATCH 2/2] Link the file to exapmles.rst --- docs/examples.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/examples.rst b/docs/examples.rst index cf70c09bf9..fb57499e65 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -8,3 +8,4 @@ Examples examples/connection_examples examples/ssl_connection_examples examples/search_json_examples + examples/set_and_get_examples