You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# View the JsonCache implementation to see how to create a cache that relies on a Storage provider.
40
+
...
41
+
42
+
#Implement rest of interface
43
+
...
44
+
45
+
register_cache("MyCache", MyCache)
46
+
47
+
asyncdefrun():
48
+
cache = create_cache(
49
+
CacheConfig(
50
+
type="MyCache",
51
+
some_setting="My Setting"
52
+
)
53
+
)
54
+
55
+
# Or use the factory directly to instantiate with a dict instead of using
56
+
# CacheConfig + create_factory
57
+
# from graphrag_cache.cache_factory import cache_factory
58
+
# cache = cache_factory.create(strategy="MyCache", init_args={"some_setting": "My Setting"})
59
+
60
+
await cache.set("my_key", {"some": "object to cache"})
61
+
print(await cache.get("my_key"))
62
+
63
+
if__name__=="__main__":
64
+
asyncio.run(run())
65
+
```
66
+
67
+
### Details
68
+
69
+
By default, the `create_cache` comes with the following cache providers registered that correspond to the entries in the `CacheType` enum.
70
+
71
+
-`JsonCache`
72
+
-`MemoryCache`
73
+
-`NoopCache`
74
+
75
+
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`.
76
+
77
+
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`.
78
+
79
+
```python
80
+
from graphrag_cache.cache_factory import cache_factory
81
+
from graphrag_cache.json_cache import JsonCache
82
+
83
+
# cache_factory has no preregistered providers so you must register any
84
+
# providers you plan on using.
85
+
# May also register a custom implementation, see above for example.
0 commit comments