{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-15T07:47:45.640702Z",
     "start_time": "2019-09-15T07:47:15.612475Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Sun Sep 15 15:47:20 2019', 1.5)\n",
      "('Sun Sep 15 15:47:25 2019', 1)\n",
      "('Sun Sep 15 15:47:30 2019', 1.5)\n",
      "('Sun Sep 15 15:47:35 2019', 1)\n",
      "('Sun Sep 15 15:47:40 2019', 1.5)\n",
      "('Sun Sep 15 15:47:45 2019', 1)\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import datetime\n",
    "\n",
    "def get_candy_price(candy_id):\n",
    "    # let's use a sleep to simulate the time your function spends trying to connect to\n",
    "    # the web service, 5 seconds will be enough.\n",
    "    time.sleep(5)\n",
    "\n",
    "    # let's pretend that the price returned by the web service is $1 for candies with a\n",
    "    # odd candy_id and $1,5 for candies with a even candy_id\n",
    "\n",
    "    price = 1.5 if candy_id % 2 == 0 else 1\n",
    "\n",
    "    return (datetime.datetime.now().strftime(\"%c\"), price)\n",
    "\n",
    "\n",
    "# now, let's simulate 20 customers in your show.\n",
    "# They are asking for candy with id 2 and candy with id 3...\n",
    "for i in range(0,3):\n",
    "    print(get_candy_price(2))\n",
    "    print(get_candy_price(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-15T07:51:35.789217Z",
     "start_time": "2019-09-15T07:51:25.766692Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n",
      "('Sun Sep 15 15:51:30 2019', 1.5, 'price')\n",
      "('Sun Sep 15 15:51:35 2019', 1, 'no more price')\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import datetime\n",
    "\n",
    "from cachetools import cached, TTLCache  # 1 - let's import the \"cached\" decorator and the \"TTLCache\" object from cachetools\n",
    "cache = TTLCache(maxsize=100, ttl=300)  # 2 - let's create the cache object.\n",
    "\n",
    "@cached(cache)  # 3 - it's time to decorate the method to use our cache system!\n",
    "def get_candy_price(candy_id, addon):\n",
    "    # let's use a sleep to simulate the time your function spends trying to connect to\n",
    "    # the web service, 5 seconds will be enough.\n",
    "    time.sleep(5)\n",
    "\n",
    "    # let's pretend that the price returned by the web service is $1 for candies with a\n",
    "    # odd candy_id and $1,5 for candies with a even candy_id\n",
    "\n",
    "    price = 1.5 if candy_id % 2 == 0 else 1\n",
    "\n",
    "    return (datetime.datetime.now().strftime(\"%c\"), price, addon)\n",
    "\n",
    "\n",
    "# now, let's simulate 20 customers in your show.\n",
    "# They are asking for candy with id 2 and candy with id 3...\n",
    "for i in range(0,20):\n",
    "    print(get_candy_price(2, 'price'))\n",
    "    print(get_candy_price(3, 'no more price'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
