{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Look-Aside Cache for MongoDB\n", "### This is a sample notebook for using Aerospike as a read/look-aside cache\n", "\n", "- This notebook demonstrates the use of Aerospike as a cache using Mongo as another primary datastore\n", "- It is required to run Mongo as a separte container using `docker run --name some-mongo -d mongo:latest`\n", "\n", "To test: Run the `get_data(key, value)` method once - to fetch from Mongo and populate Aerospike\n", "\n", "Another run will fetch the data from Aerospike cache\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Ensure that the Aerospike Database is running" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Aerospike database is running!\r\n" ] } ], "source": [ "!asd >& /dev/null\n", "!pgrep -x asd >/dev/null && echo \"Aerospike database is running!\" || echo \"**Aerospike database is not running!**\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Import all dependencies" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import aerospike\n", "import pymongo\n", "from pymongo import MongoClient\n", "import sys" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configure the clients\n", "\n", "The configuration is for \n", " - Aerospike database running on port 3000 of localhost (IP 127.0.0.1) which is the default. \n", " - Mongo running in a separate container whose IP can be found by `docker inspect | grep -i ipaddress`\n", "\n", "\n", "Modify config if your environment is different (Aerospike database running on a different host or different port)." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# Define a few constants\n", "\n", "AEROSPIKE_HOST = \"0.0.0.0\"\n", "AEROSPIKE_PORT = 3000\n", "AEROSPIKE_NAMESPACE = \"test\"\n", "AEROSPIKE_SET = \"demo\"\n", "MONGO_HOST = \"172.17.0.3\"\n", "MONGO_PORT = 27017\n", "MONGO_DB = \"test-database\"\n", "MONGO_COLLECTION = \"test-collection\"" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connected to Aerospike\n", "Connected to Mongo\n" ] } ], "source": [ "#Aerospike configuration\n", "aero_config = {\n", " 'hosts': [ (AEROSPIKE_HOST, AEROSPIKE_PORT) ]\n", "}\n", "try:\n", " aero_client = aerospike.client(aero_config).connect()\n", "except:\n", " print(\"Failed to connect to the cluster with\", aero_config['hosts'])\n", " sys.exit(1)\n", "print(\"Connected to Aerospike\")\n", "\n", "#Mongo configuration\n", "try:\n", " mongo_client = MongoClient(MONGO_HOST, MONGO_PORT)\n", " print(\"Connected to Mongo\")\n", "except:\n", " print(\"Failed to connect to Mongo\")\n", " sys.exit(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Store data in Mongo and clear the keys in Aerospike if any" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "db = mongo_client[MONGO_DB]\n", "collection = db[MONGO_COLLECTION]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "def store_data(data_id, data):\n", " m_data = {data_id: data}\n", " collection.drop()\n", " aero_key = ('test', 'demo', data_id)\n", " #aero_client.remove(aero_key)\n", " post_id = collection.insert_one(m_data)\n", "store_data(\"key\", \"value\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Fetch the data. In this instance we are using a simple key value pair.\n", "If the data exists in the cache it is returned, if not data is read from Mongo, put in the cache and then returned" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data retrieved from Aerospike cache\n", "Record::: key value\n" ] } ], "source": [ "def get_data(data_id, data):\n", " aero_key = (AEROSPIKE_NAMESPACE, AEROSPIKE_SET, data_id)\n", " #aero_client.remove(aero_key)\n", " data_check = aero_client.exists(aero_key)\n", " if data_check[1]:\n", " (key, metadata, record) = aero_client.get(aero_key)\n", " print(\"Data retrieved from Aerospike cache\")\n", " print(\"Record::: {} {}\".format(data_id, record['value']))\n", " else:\n", " mongo_data = collection.find_one({data_id: data})\n", " print(\"Data not present in Aerospike cache, retrieved from mongo {}\".format(mongo_data))\n", " aero_client.put(aero_key, {'value': mongo_data[data_id]})\n", "get_data(\"key\", \"value\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }