Back to Graphrag

Copyright (c) 2026 Microsoft Corporation.

packages/graphrag-cache/example_notebooks/basic_cache_example.ipynb

3.0.91.2 KB
Original Source
python
# Copyright (c) 2026 Microsoft Corporation.
# Licensed under the MIT License.

Basic cache example

This example shows how to create a JSON cache with file storage using the GraphRAG cache package's configuration system.

python
from graphrag_cache import CacheConfig, CacheType, create_cache, create_cache_key
from graphrag_storage import StorageConfig, StorageType


async def run():
    """Demonstrate basic cache usage with graphrag_cache."""
    cache = create_cache()

    # The above is equivalent to the following:
    cache = create_cache(
        CacheConfig(
            type=CacheType.Json,
            storage=StorageConfig(type=StorageType.File, base_dir="cache"),
        ),
    )

    await cache.set("my_key", {"k1": "object to cache"})
    print("Value stored in cache for 'my_key':")
    print(await cache.get("my_key"))

    # create cache key from data dict.
    cache_key = create_cache_key({"some_arg": "some_value", "something_else": 5})
    await cache.set(cache_key, {"k2": "object to cache"})
    print("\nValue stored in cache for cache_key using data dict:")
    print(await cache.get(cache_key))


if __name__ == "__main__":
    await run()