|
| 1 | +# GraphRAG Cache |
| 2 | + |
| 3 | +## Basic |
| 4 | + |
| 5 | +```python |
| 6 | +import asyncio |
| 7 | +from graphrag_storage import StorageConfig, create_storage, StorageType |
| 8 | +from graphrag_cache import CacheConfig, create_cache, CacheType |
| 9 | + |
| 10 | +async def run(): |
| 11 | + # Json cache requires a storage implementation. |
| 12 | + storage = create_storage( |
| 13 | + StorageConfig( |
| 14 | + type=StorageType.File |
| 15 | + base_dir="output" |
| 16 | + ) |
| 17 | + ) |
| 18 | + |
| 19 | + cache = create_cache( |
| 20 | + CacheConfig( |
| 21 | + type=CacheType.Json |
| 22 | + ), |
| 23 | + storage=storage |
| 24 | + ) |
| 25 | + |
| 26 | + await cache.set("my_key", {"some": "object to cache"}) |
| 27 | + print(await cache.get("my_key")) |
| 28 | + |
| 29 | +if __name__ == "__main__": |
| 30 | + asyncio.run(run()) |
| 31 | +``` |
| 32 | + |
| 33 | +## Custom Cache |
| 34 | + |
| 35 | +```python |
| 36 | +import asyncio |
| 37 | +from typing import Any |
| 38 | +from graphrag_storage import Storage |
| 39 | +from graphrag_cache import Cache, CacheConfig, create_cache, register_cache |
| 40 | + |
| 41 | +class MyCache(Cache): |
| 42 | + def __init__(self, storage: Storage, some_setting: str, optional_setting: str = "default setting", **kwargs: Any): |
| 43 | + # Validate settings and initialize |
| 44 | + ... |
| 45 | + |
| 46 | + #Implement rest of interface |
| 47 | + ... |
| 48 | + |
| 49 | +register_cache("MyCache", MyCache) |
| 50 | + |
| 51 | +async def run(): |
| 52 | + cache = create_cache( |
| 53 | + CacheConfig( |
| 54 | + type="MyCache" |
| 55 | + some_setting="My Setting" |
| 56 | + ) |
| 57 | + # if your cache relies on a storage implementation you can pass that here |
| 58 | + # storage=some_storage |
| 59 | + ) |
| 60 | + # Or use the factory directly to instantiate with a dict instead of using |
| 61 | + # CacheConfig + create_factory |
| 62 | + # from graphrag_cache.cache_factory import cache_factory |
| 63 | + # cache = cache_factory.create(strategy="MyCache", init_args={"storage": storage_implementation, "some_setting": "My Setting"}) |
| 64 | + |
| 65 | + await cache.set("my_key", {"some": "object to cache"}) |
| 66 | + print(await cache.get("my_key")) |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + asyncio.run(run()) |
| 70 | +``` |
| 71 | + |
| 72 | +### Details |
| 73 | + |
| 74 | +By default, the `create_cache` comes with the following cache providers registered that correspond to the entries in the `CacheType` enum. |
| 75 | + |
| 76 | +- `JsonCache` |
| 77 | +- `MemoryCache` |
| 78 | +- `NoopCache` |
| 79 | + |
| 80 | +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`. |
| 81 | + |
| 82 | +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`. |
| 83 | + |
| 84 | +```python |
| 85 | +from graphrag_cache.cache_factory import cache_factory |
| 86 | +from graphrag_cache.json_cache import JsonCache |
| 87 | + |
| 88 | +# cache_factory has no preregistered providers so you must register any |
| 89 | +# providers you plan on using. |
| 90 | +# May also register a custom implementation, see above for example. |
| 91 | +cache_factory.register("my_cache_impl", JsonCache) |
| 92 | + |
| 93 | +cache = cache_factory.create(strategy="my_cache_impl", init_args={"some_setting": "..."}) |
| 94 | + |
| 95 | +... |
| 96 | + |
| 97 | +``` |
0 commit comments