Skip to main content

GraphRAG cache package.

Project description

GraphRAG Cache

This package contains a collection of utilities to handle GraphRAG caching implementation.

Basic

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

import asyncio
from graphrag_storage import StorageConfig, create_storage, StorageType
from graphrag_cache import CacheConfig, create_cache, CacheType, create_cache_key

async def run():
    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", {"some": "object to cache"})
    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, {"some": "object to cache"})
    print(await cache.get(cache_key))

if __name__ == "__main__":
    asyncio.run(run())

Custom Cache

This demonstrates how to create a custom cache implementation by extending the base Cache class and registering it with the GraphRAG cache system. Once registered, the custom cache can be instantiated through the factory pattern using either CacheConfig or directly via cache_factory, allowing for extensible caching solutions tailored to specific needs.

import asyncio
from typing import Any
from graphrag_storage import Storage
from graphrag_cache import Cache, CacheConfig, create_cache, register_cache

class MyCache(Cache):
    def __init__(self, some_setting: str, optional_setting: str = "default setting", **kwargs: Any):
        # Validate settings and initialize
        # View the JsonCache implementation to see how to create a cache that relies on a Storage provider.
        ...

    #Implement rest of interface
    ...

register_cache("MyCache", MyCache)

async def run():
    cache = create_cache(
        CacheConfig(
            type="MyCache",
            some_setting="My Setting"
        )
    )

    # Or use the factory directly to instantiate with a dict instead of using
    # CacheConfig + create_factory
    # from graphrag_cache.cache_factory import cache_factory
    # cache = cache_factory.create(strategy="MyCache", init_args={"some_setting": "My Setting"})

    await cache.set("my_key", {"some": "object to cache"})
    print(await cache.get("my_key"))

if __name__ == "__main__":
    asyncio.run(run())

Details

By default, the create_cache comes with the following cache providers registered that correspond to the entries in the CacheType enum.

  • JsonCache
  • MemoryCache
  • NoopCache

The preregistration happens dynamically, e.g., JsonCache is only imported and registered if you request a JsonCache with create_cache(CacheType.Json, ...). There is no need to manually import and register builtin cache providers when using create_cache.

If you want a clean factory with no preregistered cache providers then directly import cache_factory and bypass using create_cache. The downside is that cache_factory.create uses a dict for init args instead of the strongly typed CacheConfig used with create_cache.

from graphrag_cache.cache_factory import cache_factory
from graphrag_cache.json_cache import JsonCache

# cache_factory has no preregistered providers so you must register any
# providers you plan on using.
# May also register a custom implementation, see above for example.
cache_factory.register("my_cache_impl", JsonCache)

cache = cache_factory.create(strategy="my_cache_impl", init_args={"some_setting": "..."})

...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphrag_cache-3.0.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphrag_cache-3.0.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file graphrag_cache-3.0.1.tar.gz.

File metadata

  • Download URL: graphrag_cache-3.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.4

File hashes

Hashes for graphrag_cache-3.0.1.tar.gz
Algorithm Hash digest
SHA256 97551a707130ef86ea0bf292d93a910d528df00cab793dd6445e16df40465a99
MD5 33fec1449cb3cb2d74829e84e76d411b
BLAKE2b-256 ab8af08a2c4ef8ff6a6b59d0d12c966f9180148de4f912862a8b70a27bd10b19

See more details on using hashes here.

File details

Details for the file graphrag_cache-3.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for graphrag_cache-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 169c42b155059cac1c649ed314fa039513ee0a2bd64e09bd612d7bcba6d10bdd
MD5 f190260c02776f9758d8cd8bdeec690a
BLAKE2b-256 cf60e522521390fb0ca5f91933ddbcb354649413affbb39d322fbf136ae5dcd8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page