{
 "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 valkey 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 valkey \n",
    "\n",
    "r = valkey.Valkey(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, Valkey 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
}
