{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "fcb917cf", "metadata": {}, "outputs": [], "source": [ "# Copyright (c) 2026 Microsoft Corporation.\n", "# Licensed under the MIT License." ] }, { "cell_type": "markdown", "id": "33461307", "metadata": {}, "source": [ "## Basic cache example\n", "\n", "This example shows how to create a JSON cache with file storage using the GraphRAG cache package's configuration system. " ] }, { "cell_type": "code", "execution_count": 2, "id": "ee33abd6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value stored in cache for 'my_key':\n", "{'k1': 'object to cache'}\n", "\n", "Value stored in cache for cache_key using data dict:\n", "{'k2': 'object to cache'}\n" ] } ], "source": [ "from graphrag_cache import CacheConfig, CacheType, create_cache, create_cache_key\n", "from graphrag_storage import StorageConfig, StorageType\n", "\n", "\n", "async def run():\n", " \"\"\"Demonstrate basic cache usage with graphrag_cache.\"\"\"\n", " cache = create_cache()\n", "\n", " # The above is equivalent to the following:\n", " cache = create_cache(\n", " CacheConfig(\n", " type=CacheType.Json,\n", " storage=StorageConfig(type=StorageType.File, base_dir=\"cache\"),\n", " ),\n", " )\n", "\n", " await cache.set(\"my_key\", {\"k1\": \"object to cache\"})\n", " print(\"Value stored in cache for 'my_key':\")\n", " print(await cache.get(\"my_key\"))\n", "\n", " # create cache key from data dict.\n", " cache_key = create_cache_key({\"some_arg\": \"some_value\", \"something_else\": 5})\n", " await cache.set(cache_key, {\"k2\": \"object to cache\"})\n", " print(\"\\nValue stored in cache for cache_key using data dict:\")\n", " print(await cache.get(cache_key))\n", "\n", "\n", "if __name__ == \"__main__\":\n", " await run()" ] } ], "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }