graphrag-cache 3.0.1__tar.gz → 3.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (20) hide show
  1. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/.gitignore +4 -0
  2. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/PKG-INFO +6 -73
  3. graphrag_cache-3.0.3/README.md +42 -0
  4. graphrag_cache-3.0.3/example_notebooks/basic_cache_example.ipynb +96 -0
  5. graphrag_cache-3.0.3/example_notebooks/cache/b458747d203b17cc97749f20a73cb58667baa348777917ef1e2a9384d145e5f0 +1 -0
  6. graphrag_cache-3.0.3/example_notebooks/cache/my_key +1 -0
  7. graphrag_cache-3.0.3/example_notebooks/custom_cache_example.ipynb +129 -0
  8. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/cache_factory.py +6 -12
  9. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/pyproject.toml +3 -3
  10. graphrag_cache-3.0.1/README.md +0 -109
  11. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/LICENSE +0 -0
  12. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/__init__.py +0 -0
  13. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/cache.py +0 -0
  14. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/cache_config.py +0 -0
  15. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/cache_key.py +0 -0
  16. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/cache_type.py +0 -0
  17. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/json_cache.py +0 -0
  18. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/memory_cache.py +0 -0
  19. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/noop_cache.py +0 -0
  20. {graphrag_cache-3.0.1 → graphrag_cache-3.0.3}/graphrag_cache/py.typed +0 -0
@@ -63,3 +63,7 @@ docsite/
63
63
 
64
64
  # Root build assets
65
65
  packages/*/LICENSE
66
+
67
+ # Notebooks outputs
68
+ packages/graphrag-*/example_notebooks/**/output
69
+ packages/graphrag-*/example_notebooks/**/cache
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: graphrag-cache
3
- Version: 3.0.1
3
+ Version: 3.0.3
4
4
  Summary: GraphRAG cache package.
5
5
  Project-URL: Source, https://github.com/microsoft/graphrag
6
6
  Author: Mónica Carvajal
@@ -12,8 +12,8 @@ Classifier: Programming Language :: Python :: 3.11
12
12
  Classifier: Programming Language :: Python :: 3.12
13
13
  Classifier: Programming Language :: Python :: 3.13
14
14
  Requires-Python: <3.14,>=3.11
15
- Requires-Dist: graphrag-common==3.0.1
16
- Requires-Dist: graphrag-storage==3.0.1
15
+ Requires-Dist: graphrag-common==3.0.2
16
+ Requires-Dist: graphrag-storage==3.0.2
17
17
  Description-Content-Type: text/markdown
18
18
 
19
19
  # GraphRAG Cache
@@ -24,80 +24,13 @@ This package contains a collection of utilities to handle GraphRAG caching imple
24
24
 
25
25
  This example shows how to create a JSON cache with file storage using the GraphRAG cache package's configuration system.
26
26
 
27
- ```python
28
- import asyncio
29
- from graphrag_storage import StorageConfig, create_storage, StorageType
30
- from graphrag_cache import CacheConfig, create_cache, CacheType, create_cache_key
31
-
32
- async def run():
33
- cache = create_cache()
34
-
35
- # The above is equivalent to the following:
36
- cache = create_cache(
37
- CacheConfig(
38
- type=CacheType.Json,
39
- storage=StorageConfig(
40
- type=StorageType.File,
41
- base_dir="cache"
42
- )
43
- ),
44
- )
45
-
46
- await cache.set("my_key", {"some": "object to cache"})
47
- print(await cache.get("my_key"))
48
-
49
- # create cache key from data dict.
50
- cache_key = create_cache_key({
51
- "some_arg": "some_value",
52
- "something_else": 5
53
- })
54
- await cache.set(cache_key, {"some": "object to cache"})
55
- print(await cache.get(cache_key))
56
-
57
- if __name__ == "__main__":
58
- asyncio.run(run())
59
- ```
27
+ [Open the notebook to explore the basic example code](example-notebooks/basic_cache_example.ipynb)
60
28
 
61
29
  ### Custom Cache
62
30
 
63
- 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.
31
+ This example 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.
64
32
 
65
- ```python
66
- import asyncio
67
- from typing import Any
68
- from graphrag_storage import Storage
69
- from graphrag_cache import Cache, CacheConfig, create_cache, register_cache
70
-
71
- class MyCache(Cache):
72
- def __init__(self, some_setting: str, optional_setting: str = "default setting", **kwargs: Any):
73
- # Validate settings and initialize
74
- # View the JsonCache implementation to see how to create a cache that relies on a Storage provider.
75
- ...
76
-
77
- #Implement rest of interface
78
- ...
79
-
80
- register_cache("MyCache", MyCache)
81
-
82
- async def run():
83
- cache = create_cache(
84
- CacheConfig(
85
- type="MyCache",
86
- some_setting="My Setting"
87
- )
88
- )
89
-
90
- # Or use the factory directly to instantiate with a dict instead of using
91
- # CacheConfig + create_factory
92
- # from graphrag_cache.cache_factory import cache_factory
93
- # cache = cache_factory.create(strategy="MyCache", init_args={"some_setting": "My Setting"})
94
-
95
- await cache.set("my_key", {"some": "object to cache"})
96
- print(await cache.get("my_key"))
97
-
98
- if __name__ == "__main__":
99
- asyncio.run(run())
100
- ```
33
+ [Open the notebook to explore the basic custom example code](example-notebooks/custom_cache_example.ipynb)
101
34
 
102
35
  #### Details
103
36
 
@@ -0,0 +1,42 @@
1
+ # GraphRAG Cache
2
+
3
+ This package contains a collection of utilities to handle GraphRAG caching implementation.
4
+
5
+ ### Basic
6
+
7
+ This example shows how to create a JSON cache with file storage using the GraphRAG cache package's configuration system.
8
+
9
+ [Open the notebook to explore the basic example code](example-notebooks/basic_cache_example.ipynb)
10
+
11
+ ### Custom Cache
12
+
13
+ This example 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.
14
+
15
+ [Open the notebook to explore the basic custom example code](example-notebooks/custom_cache_example.ipynb)
16
+
17
+ #### Details
18
+
19
+ By default, the `create_cache` comes with the following cache providers registered that correspond to the entries in the `CacheType` enum.
20
+
21
+ - `JsonCache`
22
+ - `MemoryCache`
23
+ - `NoopCache`
24
+
25
+ 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`.
26
+
27
+ 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`.
28
+
29
+ ```python
30
+ from graphrag_cache.cache_factory import cache_factory
31
+ from graphrag_cache.json_cache import JsonCache
32
+
33
+ # cache_factory has no preregistered providers so you must register any
34
+ # providers you plan on using.
35
+ # May also register a custom implementation, see above for example.
36
+ cache_factory.register("my_cache_impl", JsonCache)
37
+
38
+ cache = cache_factory.create(strategy="my_cache_impl", init_args={"some_setting": "..."})
39
+
40
+ ...
41
+
42
+ ```
@@ -0,0 +1,96 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "fcb917cf",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "# Copyright (c) 2026 Microsoft Corporation.\n",
11
+ "# Licensed under the MIT License."
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "id": "33461307",
17
+ "metadata": {},
18
+ "source": [
19
+ "## Basic cache example\n",
20
+ "\n",
21
+ "This example shows how to create a JSON cache with file storage using the GraphRAG cache package's configuration system. "
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 2,
27
+ "id": "ee33abd6",
28
+ "metadata": {},
29
+ "outputs": [
30
+ {
31
+ "name": "stdout",
32
+ "output_type": "stream",
33
+ "text": [
34
+ "Value stored in cache for 'my_key':\n",
35
+ "{'k1': 'object to cache'}\n",
36
+ "\n",
37
+ "Value stored in cache for cache_key using data dict:\n",
38
+ "{'k2': 'object to cache'}\n"
39
+ ]
40
+ }
41
+ ],
42
+ "source": [
43
+ "from graphrag_cache import CacheConfig, CacheType, create_cache, create_cache_key\n",
44
+ "from graphrag_storage import StorageConfig, StorageType\n",
45
+ "\n",
46
+ "\n",
47
+ "async def run():\n",
48
+ " \"\"\"Demonstrate basic cache usage with graphrag_cache.\"\"\"\n",
49
+ " cache = create_cache()\n",
50
+ "\n",
51
+ " # The above is equivalent to the following:\n",
52
+ " cache = create_cache(\n",
53
+ " CacheConfig(\n",
54
+ " type=CacheType.Json,\n",
55
+ " storage=StorageConfig(type=StorageType.File, base_dir=\"cache\"),\n",
56
+ " ),\n",
57
+ " )\n",
58
+ "\n",
59
+ " await cache.set(\"my_key\", {\"k1\": \"object to cache\"})\n",
60
+ " print(\"Value stored in cache for 'my_key':\")\n",
61
+ " print(await cache.get(\"my_key\"))\n",
62
+ "\n",
63
+ " # create cache key from data dict.\n",
64
+ " cache_key = create_cache_key({\"some_arg\": \"some_value\", \"something_else\": 5})\n",
65
+ " await cache.set(cache_key, {\"k2\": \"object to cache\"})\n",
66
+ " print(\"\\nValue stored in cache for cache_key using data dict:\")\n",
67
+ " print(await cache.get(cache_key))\n",
68
+ "\n",
69
+ "\n",
70
+ "if __name__ == \"__main__\":\n",
71
+ " await run()"
72
+ ]
73
+ }
74
+ ],
75
+ "metadata": {
76
+ "kernelspec": {
77
+ "display_name": "Python 3",
78
+ "language": "python",
79
+ "name": "python3"
80
+ },
81
+ "language_info": {
82
+ "codemirror_mode": {
83
+ "name": "ipython",
84
+ "version": 3
85
+ },
86
+ "file_extension": ".py",
87
+ "mimetype": "text/x-python",
88
+ "name": "python",
89
+ "nbconvert_exporter": "python",
90
+ "pygments_lexer": "ipython3",
91
+ "version": "3.12.9"
92
+ }
93
+ },
94
+ "nbformat": 4,
95
+ "nbformat_minor": 5
96
+ }
@@ -0,0 +1 @@
1
+ {"result": {"k1": "object to cache"}}
@@ -0,0 +1,129 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "42524e97",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "# Copyright (c) 2026 Microsoft Corporation.\n",
11
+ "# Licensed under the MIT License."
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "id": "a7cd6743",
17
+ "metadata": {},
18
+ "source": [
19
+ "## Custom cache example\n",
20
+ "\n",
21
+ "This example 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."
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 2,
27
+ "id": "e7ec8dc1",
28
+ "metadata": {},
29
+ "outputs": [
30
+ {
31
+ "name": "stdout",
32
+ "output_type": "stream",
33
+ "text": [
34
+ "Value stored in cache for 'my_key':\n",
35
+ "{'k1': 'object to cache'}\n"
36
+ ]
37
+ }
38
+ ],
39
+ "source": [
40
+ "from typing import Any\n",
41
+ "\n",
42
+ "from graphrag_cache import Cache, CacheConfig, create_cache, register_cache\n",
43
+ "\n",
44
+ "\n",
45
+ "class MyCache(Cache):\n",
46
+ " \"\"\"Custom cache implementation for storing and retrieving cached data.\"\"\"\n",
47
+ "\n",
48
+ " def __init__(\n",
49
+ " self,\n",
50
+ " some_setting: str,\n",
51
+ " optional_setting: str = \"default setting\",\n",
52
+ " **kwargs: Any,\n",
53
+ " ):\n",
54
+ " # Validate settings and initialize\n",
55
+ " # View the JsonCache implementation to see how to create a cache that relies on a Storage provider.\n",
56
+ " self.some_setting = some_setting\n",
57
+ " self.optional_setting = optional_setting\n",
58
+ " self._cache: dict[str, Any] = {}\n",
59
+ " self._child: Cache = self\n",
60
+ "\n",
61
+ " async def get(self, key: str) -> Any:\n",
62
+ " \"\"\"Retrieve a value from the cache by key.\"\"\"\n",
63
+ " return self._cache.get(key)\n",
64
+ "\n",
65
+ " async def set(self, key: str, value: Any, debug_data: Any = None) -> None:\n",
66
+ " \"\"\"Store a value in the cache with the specified key.\"\"\"\n",
67
+ " self._cache[key] = value\n",
68
+ "\n",
69
+ " async def has(self, key: str) -> bool:\n",
70
+ " \"\"\"Check if a key exists in the cache.\"\"\"\n",
71
+ " return key in self._cache\n",
72
+ "\n",
73
+ " async def delete(self, key: str) -> None:\n",
74
+ " \"\"\"Remove a key and its value from the cache.\"\"\"\n",
75
+ " self._cache.pop(key, None)\n",
76
+ "\n",
77
+ " async def clear(self) -> None:\n",
78
+ " \"\"\"Clear all items from the cache.\"\"\"\n",
79
+ " self._cache.clear()\n",
80
+ "\n",
81
+ " def child(self, name: str) -> Cache:\n",
82
+ " \"\"\"Create or access a child cache (not implemented in this example).\"\"\"\n",
83
+ " return self._child\n",
84
+ "\n",
85
+ "\n",
86
+ "register_cache(\"MyCache\", MyCache)\n",
87
+ "\n",
88
+ "\n",
89
+ "async def run():\n",
90
+ " \"\"\"Demonstrate usage of the custom cache implementation.\"\"\"\n",
91
+ " cache = create_cache(\n",
92
+ " CacheConfig(\n",
93
+ " type=\"MyCache\",\n",
94
+ " some_setting=\"important setting value\", # type: ignore\n",
95
+ " )\n",
96
+ " )\n",
97
+ "\n",
98
+ " await cache.set(\"my_key\", {\"k1\": \"object to cache\"})\n",
99
+ " print(\"Value stored in cache for 'my_key':\")\n",
100
+ " print(await cache.get(\"my_key\"))\n",
101
+ "\n",
102
+ "\n",
103
+ "if __name__ == \"__main__\":\n",
104
+ " await run()"
105
+ ]
106
+ }
107
+ ],
108
+ "metadata": {
109
+ "kernelspec": {
110
+ "display_name": "Python 3",
111
+ "language": "python",
112
+ "name": "python3"
113
+ },
114
+ "language_info": {
115
+ "codemirror_mode": {
116
+ "name": "ipython",
117
+ "version": 3
118
+ },
119
+ "file_extension": ".py",
120
+ "mimetype": "text/x-python",
121
+ "name": "python",
122
+ "nbconvert_exporter": "python",
123
+ "pygments_lexer": "ipython3",
124
+ "version": "3.12.9"
125
+ }
126
+ },
127
+ "nbformat": 4,
128
+ "nbformat_minor": 5
129
+ }
@@ -5,20 +5,14 @@
5
5
  """Cache factory implementation."""
6
6
 
7
7
  from collections.abc import Callable
8
- from typing import TYPE_CHECKING
9
8
 
10
- from graphrag_common.factory import Factory
11
- from graphrag_storage import create_storage
9
+ from graphrag_common.factory import Factory, ServiceScope
10
+ from graphrag_storage import Storage, create_storage
12
11
 
12
+ from graphrag_cache.cache import Cache
13
13
  from graphrag_cache.cache_config import CacheConfig
14
14
  from graphrag_cache.cache_type import CacheType
15
15
 
16
- if TYPE_CHECKING:
17
- from graphrag_common.factory import ServiceScope
18
- from graphrag_storage import Storage
19
-
20
- from graphrag_cache.cache import Cache
21
-
22
16
 
23
17
  class CacheFactory(Factory["Cache"]):
24
18
  """A factory class for cache implementations."""
@@ -29,8 +23,8 @@ cache_factory = CacheFactory()
29
23
 
30
24
  def register_cache(
31
25
  cache_type: str,
32
- cache_initializer: Callable[..., "Cache"],
33
- scope: "ServiceScope" = "transient",
26
+ cache_initializer: Callable[..., Cache],
27
+ scope: ServiceScope = "transient",
34
28
  ) -> None:
35
29
  """Register a custom cache implementation.
36
30
 
@@ -45,7 +39,7 @@ def register_cache(
45
39
 
46
40
 
47
41
  def create_cache(
48
- config: CacheConfig | None = None, storage: "Storage | None" = None
42
+ config: CacheConfig | None = None, storage: Storage | None = None
49
43
  ) -> "Cache":
50
44
  """Create a cache implementation based on the given configuration.
51
45
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "graphrag-cache"
3
- version = "3.0.1"
3
+ version = "3.0.3"
4
4
  description = "GraphRAG cache package."
5
5
  authors = [
6
6
  {name = "Alonso Guevara Fernández", email = "alonsog@microsoft.com"},
@@ -31,8 +31,8 @@ classifiers = [
31
31
  "Programming Language :: Python :: 3.13",
32
32
  ]
33
33
  dependencies = [
34
- "graphrag-common==3.0.1",
35
- "graphrag-storage==3.0.1",
34
+ "graphrag-common==3.0.2",
35
+ "graphrag-storage==3.0.2",
36
36
  ]
37
37
 
38
38
  [project.urls]
@@ -1,109 +0,0 @@
1
- # GraphRAG Cache
2
-
3
- This package contains a collection of utilities to handle GraphRAG caching implementation.
4
-
5
- ### Basic
6
-
7
- This example shows how to create a JSON cache with file storage using the GraphRAG cache package's configuration system.
8
-
9
- ```python
10
- import asyncio
11
- from graphrag_storage import StorageConfig, create_storage, StorageType
12
- from graphrag_cache import CacheConfig, create_cache, CacheType, create_cache_key
13
-
14
- async def run():
15
- cache = create_cache()
16
-
17
- # The above is equivalent to the following:
18
- cache = create_cache(
19
- CacheConfig(
20
- type=CacheType.Json,
21
- storage=StorageConfig(
22
- type=StorageType.File,
23
- base_dir="cache"
24
- )
25
- ),
26
- )
27
-
28
- await cache.set("my_key", {"some": "object to cache"})
29
- print(await cache.get("my_key"))
30
-
31
- # create cache key from data dict.
32
- cache_key = create_cache_key({
33
- "some_arg": "some_value",
34
- "something_else": 5
35
- })
36
- await cache.set(cache_key, {"some": "object to cache"})
37
- print(await cache.get(cache_key))
38
-
39
- if __name__ == "__main__":
40
- asyncio.run(run())
41
- ```
42
-
43
- ### Custom Cache
44
-
45
- 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.
46
-
47
- ```python
48
- import asyncio
49
- from typing import Any
50
- from graphrag_storage import Storage
51
- from graphrag_cache import Cache, CacheConfig, create_cache, register_cache
52
-
53
- class MyCache(Cache):
54
- def __init__(self, some_setting: str, optional_setting: str = "default setting", **kwargs: Any):
55
- # Validate settings and initialize
56
- # View the JsonCache implementation to see how to create a cache that relies on a Storage provider.
57
- ...
58
-
59
- #Implement rest of interface
60
- ...
61
-
62
- register_cache("MyCache", MyCache)
63
-
64
- async def run():
65
- cache = create_cache(
66
- CacheConfig(
67
- type="MyCache",
68
- some_setting="My Setting"
69
- )
70
- )
71
-
72
- # Or use the factory directly to instantiate with a dict instead of using
73
- # CacheConfig + create_factory
74
- # from graphrag_cache.cache_factory import cache_factory
75
- # cache = cache_factory.create(strategy="MyCache", init_args={"some_setting": "My Setting"})
76
-
77
- await cache.set("my_key", {"some": "object to cache"})
78
- print(await cache.get("my_key"))
79
-
80
- if __name__ == "__main__":
81
- asyncio.run(run())
82
- ```
83
-
84
- #### Details
85
-
86
- By default, the `create_cache` comes with the following cache providers registered that correspond to the entries in the `CacheType` enum.
87
-
88
- - `JsonCache`
89
- - `MemoryCache`
90
- - `NoopCache`
91
-
92
- 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`.
93
-
94
- 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`.
95
-
96
- ```python
97
- from graphrag_cache.cache_factory import cache_factory
98
- from graphrag_cache.json_cache import JsonCache
99
-
100
- # cache_factory has no preregistered providers so you must register any
101
- # providers you plan on using.
102
- # May also register a custom implementation, see above for example.
103
- cache_factory.register("my_cache_impl", JsonCache)
104
-
105
- cache = cache_factory.create(strategy="my_cache_impl", init_args={"some_setting": "..."})
106
-
107
- ...
108
-
109
- ```
File without changes