hishel 0.1.3__tar.gz → 0.1.4__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.
- {hishel-0.1.3 → hishel-0.1.4}/.gitignore +1 -0
- {hishel-0.1.3 → hishel-0.1.4}/CHANGELOG.md +25 -1
- {hishel-0.1.3 → hishel-0.1.4}/PKG-INFO +41 -4
- {hishel-0.1.3 → hishel-0.1.4}/README.md +6 -2
- hishel-0.1.4/hishel/__init__.py +57 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_async/_client.py +1 -1
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_async/_storages.py +10 -14
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_async/_transports.py +9 -4
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_controller.py +2 -3
- hishel-0.1.4/hishel/_lmdb_types_.pyi +53 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_serializers.py +2 -2
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_sync/_client.py +1 -1
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_sync/_storages.py +10 -14
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_sync/_transports.py +9 -4
- hishel-0.1.4/hishel/_utils.py +458 -0
- hishel-0.1.4/hishel/beta/__init__.py +59 -0
- hishel-0.1.4/hishel/beta/_async_cache.py +167 -0
- hishel-0.1.4/hishel/beta/_core/_async/_storages/_sqlite.py +411 -0
- hishel-0.1.4/hishel/beta/_core/_base/_storages/_base.py +260 -0
- hishel-0.1.4/hishel/beta/_core/_base/_storages/_packing.py +165 -0
- hishel-0.1.4/hishel/beta/_core/_headers.py +301 -0
- hishel-0.1.4/hishel/beta/_core/_spec.py +2291 -0
- hishel-0.1.4/hishel/beta/_core/_sync/_storages/_sqlite.py +411 -0
- hishel-0.1.4/hishel/beta/_core/models.py +176 -0
- hishel-0.1.4/hishel/beta/_sync_cache.py +167 -0
- hishel-0.1.4/hishel/beta/httpx.py +317 -0
- hishel-0.1.4/hishel/beta/requests.py +193 -0
- hishel-0.1.4/hishel/py.typed +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/pyproject.toml +44 -44
- hishel-0.1.3/hishel/__init__.py +0 -17
- hishel-0.1.3/hishel/_utils.py +0 -118
- {hishel-0.1.3 → hishel-0.1.4}/LICENSE +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_async/__init__.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_async/_mock.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_async/_pool.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_exceptions.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_files.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_headers.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_lfu_cache.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_s3.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_sync/__init__.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_sync/_mock.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_sync/_pool.py +0 -0
- {hishel-0.1.3 → hishel-0.1.4}/hishel/_synchronization.py +0 -0
- /hishel-0.1.3/hishel/py.typed → /hishel-0.1.4/hishel/beta/_core/__init__.py +0 -0
|
@@ -1,5 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
## [0.1.4] - 2025-10-14
|
|
2
2
|
|
|
3
|
+
### 🚀 Features
|
|
4
|
+
|
|
5
|
+
- Add support for requests library
|
|
6
|
+
- Add support for Python 3.14
|
|
7
|
+
- Add support for a sans-IO API (#366)
|
|
8
|
+
- Allow already consumed streams with `CacheTransport` (#377)
|
|
9
|
+
- Add sqlite storage for beta storages
|
|
10
|
+
- Get rid of some locks from sqlite storage
|
|
11
|
+
- Better async implemetation for sqlite storage
|
|
12
|
+
- Added `Metadata` to public API. (#363)
|
|
13
|
+
- Fix race condition in FileStorage initialization. (#353)
|
|
14
|
+
|
|
15
|
+
### 🐛 Bug Fixes
|
|
16
|
+
|
|
17
|
+
- Create an sqlite file in a cache folder
|
|
18
|
+
- Fix beta imports
|
|
19
|
+
|
|
20
|
+
### ⚙️ Miscellaneous Tasks
|
|
21
|
+
|
|
22
|
+
- Improve CI (#369)
|
|
23
|
+
- *(internal)* Remove src folder (#373)
|
|
24
|
+
- *(internal)* Temporary remove python3.14 from CI
|
|
25
|
+
- *(tests)* Add sqlite tests for new storage
|
|
26
|
+
- *(tests)* Move some tests to beta
|
|
3
27
|
## 0.1.3 (1st July, 2025)
|
|
4
28
|
|
|
5
29
|
- Remove `types-redis` from dev dependencies (#336)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: hishel
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Persistent cache implementation for httpx and httpcore
|
|
5
5
|
Project-URL: Homepage, https://hishel.com
|
|
6
6
|
Project-URL: Source, https://github.com/karpetrosyan/hishel
|
|
@@ -21,11 +21,20 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
21
21
|
Classifier: Programming Language :: Python :: 3.11
|
|
22
22
|
Classifier: Programming Language :: Python :: 3.12
|
|
23
23
|
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
25
|
Classifier: Topic :: Internet :: WWW/HTTP
|
|
25
26
|
Requires-Python: >=3.9
|
|
27
|
+
Requires-Dist: anyio>=4.9.0
|
|
28
|
+
Requires-Dist: anysqlite>=0.0.5
|
|
26
29
|
Requires-Dist: httpx>=0.28.0
|
|
30
|
+
Requires-Dist: msgpack>=1.1.2
|
|
31
|
+
Requires-Dist: typing-extensions>=4.14.1
|
|
32
|
+
Provides-Extra: httpx
|
|
33
|
+
Requires-Dist: httpx>=0.28.1; extra == 'httpx'
|
|
27
34
|
Provides-Extra: redis
|
|
28
35
|
Requires-Dist: redis==6.2.0; extra == 'redis'
|
|
36
|
+
Provides-Extra: requests
|
|
37
|
+
Requires-Dist: requests>=2.32.5; extra == 'requests'
|
|
29
38
|
Provides-Extra: s3
|
|
30
39
|
Requires-Dist: boto3<=1.15.3,>=1.15.0; (python_version < '3.12') and extra == 's3'
|
|
31
40
|
Requires-Dist: boto3>=1.15.3; (python_version >= '3.12') and extra == 's3'
|
|
@@ -59,8 +68,8 @@ Description-Content-Type: text/markdown
|
|
|
59
68
|
<img src="https://img.shields.io/pypi/l/hishel" alt="license">
|
|
60
69
|
</a>
|
|
61
70
|
|
|
62
|
-
<a href="https://
|
|
63
|
-
<img src="https://img.shields.io/
|
|
71
|
+
<a href="https://coveralls.io/github/karpetrosyan/hishel">
|
|
72
|
+
<img src="https://img.shields.io/coverallsCoverage/github/karpetrosyan/hishel" alt="license">
|
|
64
73
|
</a>
|
|
65
74
|
|
|
66
75
|
<a href="https://github.com/karpetrosyan/hishel">
|
|
@@ -68,6 +77,10 @@ Description-Content-Type: text/markdown
|
|
|
68
77
|
</a>
|
|
69
78
|
</p>
|
|
70
79
|
|
|
80
|
+
<p align="center">
|
|
81
|
+
<a href="https://buymeacoffee.com/karpetrosyan" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
|
82
|
+
</p>
|
|
83
|
+
|
|
71
84
|
-----
|
|
72
85
|
|
|
73
86
|
**Hishel (հիշել, remember)** is a library that implements HTTP Caching for [HTTPX](https://github.com/encode/httpx) and [HTTP Core](https://github.com/encode/httpcore) libraries in accordance with [**RFC 9111**](https://www.rfc-editor.org/rfc/rfc9111.html), the most recent caching specification.
|
|
@@ -179,8 +192,32 @@ You have complete control over them; you can change storage or even write your o
|
|
|
179
192
|
You can support the project by simply leaving a GitHub star ⭐ or by [contributing](https://hishel.com/contributing/).
|
|
180
193
|
Help us grow and continue developing good software for you ❤️
|
|
181
194
|
|
|
182
|
-
|
|
195
|
+
## [0.1.4] - 2025-10-14
|
|
196
|
+
|
|
197
|
+
### 🚀 Features
|
|
198
|
+
|
|
199
|
+
- Add support for requests library
|
|
200
|
+
- Add support for Python 3.14
|
|
201
|
+
- Add support for a sans-IO API (#366)
|
|
202
|
+
- Allow already consumed streams with `CacheTransport` (#377)
|
|
203
|
+
- Add sqlite storage for beta storages
|
|
204
|
+
- Get rid of some locks from sqlite storage
|
|
205
|
+
- Better async implemetation for sqlite storage
|
|
206
|
+
- Added `Metadata` to public API. (#363)
|
|
207
|
+
- Fix race condition in FileStorage initialization. (#353)
|
|
208
|
+
|
|
209
|
+
### 🐛 Bug Fixes
|
|
210
|
+
|
|
211
|
+
- Create an sqlite file in a cache folder
|
|
212
|
+
- Fix beta imports
|
|
213
|
+
|
|
214
|
+
### ⚙️ Miscellaneous Tasks
|
|
183
215
|
|
|
216
|
+
- Improve CI (#369)
|
|
217
|
+
- *(internal)* Remove src folder (#373)
|
|
218
|
+
- *(internal)* Temporary remove python3.14 from CI
|
|
219
|
+
- *(tests)* Add sqlite tests for new storage
|
|
220
|
+
- *(tests)* Move some tests to beta
|
|
184
221
|
## 0.1.3 (1st July, 2025)
|
|
185
222
|
|
|
186
223
|
- Remove `types-redis` from dev dependencies (#336)
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
<img src="https://img.shields.io/pypi/l/hishel" alt="license">
|
|
23
23
|
</a>
|
|
24
24
|
|
|
25
|
-
<a href="https://
|
|
26
|
-
<img src="https://img.shields.io/
|
|
25
|
+
<a href="https://coveralls.io/github/karpetrosyan/hishel">
|
|
26
|
+
<img src="https://img.shields.io/coverallsCoverage/github/karpetrosyan/hishel" alt="license">
|
|
27
27
|
</a>
|
|
28
28
|
|
|
29
29
|
<a href="https://github.com/karpetrosyan/hishel">
|
|
@@ -31,6 +31,10 @@
|
|
|
31
31
|
</a>
|
|
32
32
|
</p>
|
|
33
33
|
|
|
34
|
+
<p align="center">
|
|
35
|
+
<a href="https://buymeacoffee.com/karpetrosyan" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
|
36
|
+
</p>
|
|
37
|
+
|
|
34
38
|
-----
|
|
35
39
|
|
|
36
40
|
**Hishel (հիշել, remember)** is a library that implements HTTP Caching for [HTTPX](https://github.com/encode/httpx) and [HTTP Core](https://github.com/encode/httpcore) libraries in accordance with [**RFC 9111**](https://www.rfc-editor.org/rfc/rfc9111.html), the most recent caching specification.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
|
|
3
|
+
from ._async import *
|
|
4
|
+
from ._controller import *
|
|
5
|
+
from ._exceptions import *
|
|
6
|
+
from ._headers import *
|
|
7
|
+
from ._serializers import *
|
|
8
|
+
from ._sync import *
|
|
9
|
+
from ._lfu_cache import *
|
|
10
|
+
|
|
11
|
+
__all__ = (
|
|
12
|
+
# Old API
|
|
13
|
+
"AsyncCacheClient",
|
|
14
|
+
"MockAsyncConnectionPool",
|
|
15
|
+
"MockAsyncTransport",
|
|
16
|
+
"AsyncCacheConnectionPool",
|
|
17
|
+
"AsyncBaseStorage",
|
|
18
|
+
"AsyncFileStorage",
|
|
19
|
+
"AsyncInMemoryStorage",
|
|
20
|
+
"AsyncRedisStorage",
|
|
21
|
+
"AsyncS3Storage",
|
|
22
|
+
"AsyncSQLiteStorage",
|
|
23
|
+
"AsyncCacheTransport",
|
|
24
|
+
"HEURISTICALLY_CACHEABLE_STATUS_CODES",
|
|
25
|
+
"Controller",
|
|
26
|
+
"CacheControlError",
|
|
27
|
+
"ParseError",
|
|
28
|
+
"ValidationError",
|
|
29
|
+
"CacheControl",
|
|
30
|
+
"Vary",
|
|
31
|
+
"BaseSerializer",
|
|
32
|
+
"JSONSerializer",
|
|
33
|
+
"Metadata",
|
|
34
|
+
"PickleSerializer",
|
|
35
|
+
"YAMLSerializer",
|
|
36
|
+
"clone_model",
|
|
37
|
+
"CacheClient",
|
|
38
|
+
"MockConnectionPool",
|
|
39
|
+
"MockTransport",
|
|
40
|
+
"CacheConnectionPool",
|
|
41
|
+
"BaseStorage",
|
|
42
|
+
"FileStorage",
|
|
43
|
+
"InMemoryStorage",
|
|
44
|
+
"RedisStorage",
|
|
45
|
+
"S3Storage",
|
|
46
|
+
"SQLiteStorage",
|
|
47
|
+
"CacheTransport",
|
|
48
|
+
"LFUCache",
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
def install_cache() -> None: # pragma: no cover
|
|
52
|
+
httpx.AsyncClient = AsyncCacheClient # type: ignore
|
|
53
|
+
httpx.Client = CacheClient # type: ignore
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
__version__ = "0.1.4"
|
|
57
|
+
|
|
@@ -4,7 +4,6 @@ import datetime
|
|
|
4
4
|
import logging
|
|
5
5
|
import os
|
|
6
6
|
import time
|
|
7
|
-
import typing as t
|
|
8
7
|
import typing as tp
|
|
9
8
|
import warnings
|
|
10
9
|
from copy import deepcopy
|
|
@@ -24,13 +23,11 @@ except ImportError: # pragma: no cover
|
|
|
24
23
|
|
|
25
24
|
from httpcore import Request, Response
|
|
26
25
|
|
|
27
|
-
if
|
|
26
|
+
if tp.TYPE_CHECKING: # pragma: no cover
|
|
28
27
|
from typing_extensions import TypeAlias
|
|
29
28
|
|
|
30
|
-
from hishel._serializers import BaseSerializer, clone_model
|
|
31
|
-
|
|
32
29
|
from .._files import AsyncFileManager
|
|
33
|
-
from .._serializers import JSONSerializer, Metadata
|
|
30
|
+
from .._serializers import BaseSerializer, JSONSerializer, Metadata, clone_model
|
|
34
31
|
from .._synchronization import AsyncLock
|
|
35
32
|
from .._utils import float_seconds_to_int_milliseconds
|
|
36
33
|
|
|
@@ -39,10 +36,10 @@ logger = logging.getLogger("hishel.storages")
|
|
|
39
36
|
__all__ = (
|
|
40
37
|
"AsyncBaseStorage",
|
|
41
38
|
"AsyncFileStorage",
|
|
42
|
-
"AsyncRedisStorage",
|
|
43
|
-
"AsyncSQLiteStorage",
|
|
44
39
|
"AsyncInMemoryStorage",
|
|
40
|
+
"AsyncRedisStorage",
|
|
45
41
|
"AsyncS3Storage",
|
|
42
|
+
"AsyncSQLiteStorage",
|
|
46
43
|
)
|
|
47
44
|
|
|
48
45
|
StoredResponse: TypeAlias = tp.Tuple[Response, Request, Metadata]
|
|
@@ -106,8 +103,7 @@ class AsyncFileStorage(AsyncBaseStorage):
|
|
|
106
103
|
self._base_path = Path(base_path) if base_path is not None else Path(".cache/hishel")
|
|
107
104
|
self._gitignore_file = self._base_path / ".gitignore"
|
|
108
105
|
|
|
109
|
-
|
|
110
|
-
self._base_path.mkdir(parents=True)
|
|
106
|
+
self._base_path.mkdir(parents=True, exist_ok=True)
|
|
111
107
|
|
|
112
108
|
if not self._gitignore_file.is_file():
|
|
113
109
|
with open(self._gitignore_file, "w", encoding="utf-8") as f:
|
|
@@ -153,7 +149,7 @@ class AsyncFileStorage(AsyncBaseStorage):
|
|
|
153
149
|
"""
|
|
154
150
|
|
|
155
151
|
if isinstance(key, Response): # pragma: no cover
|
|
156
|
-
key =
|
|
152
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
157
153
|
|
|
158
154
|
response_path = self._base_path / key
|
|
159
155
|
|
|
@@ -322,7 +318,7 @@ class AsyncSQLiteStorage(AsyncBaseStorage):
|
|
|
322
318
|
assert self._connection
|
|
323
319
|
|
|
324
320
|
if isinstance(key, Response): # pragma: no cover
|
|
325
|
-
key =
|
|
321
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
326
322
|
|
|
327
323
|
async with self._lock:
|
|
328
324
|
await self._connection.execute("DELETE FROM cache WHERE key = ?", [key])
|
|
@@ -459,7 +455,7 @@ class AsyncRedisStorage(AsyncBaseStorage):
|
|
|
459
455
|
"""
|
|
460
456
|
|
|
461
457
|
if isinstance(key, Response): # pragma: no cover
|
|
462
|
-
key =
|
|
458
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
463
459
|
|
|
464
460
|
await self._client.delete(key)
|
|
465
461
|
|
|
@@ -572,7 +568,7 @@ class AsyncInMemoryStorage(AsyncBaseStorage):
|
|
|
572
568
|
"""
|
|
573
569
|
|
|
574
570
|
if isinstance(key, Response): # pragma: no cover
|
|
575
|
-
key =
|
|
571
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
576
572
|
|
|
577
573
|
async with self._lock:
|
|
578
574
|
self._cache.remove_key(key)
|
|
@@ -720,7 +716,7 @@ class AsyncS3Storage(AsyncBaseStorage): # pragma: no cover
|
|
|
720
716
|
"""
|
|
721
717
|
|
|
722
718
|
if isinstance(key, Response): # pragma: no cover
|
|
723
|
-
key =
|
|
719
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
724
720
|
|
|
725
721
|
async with self._lock:
|
|
726
722
|
await self._s3_manager.remove_entry(key)
|
|
@@ -8,11 +8,10 @@ import httpx
|
|
|
8
8
|
from httpx import AsyncByteStream, Request, Response
|
|
9
9
|
from httpx._exceptions import ConnectError
|
|
10
10
|
|
|
11
|
-
from hishel._utils import extract_header_values_decoded, normalized_url
|
|
12
|
-
|
|
13
11
|
from .._controller import Controller, allowed_stale
|
|
14
12
|
from .._headers import parse_cache_control
|
|
15
13
|
from .._serializers import JSONSerializer, Metadata
|
|
14
|
+
from .._utils import extract_header_values_decoded, normalized_url
|
|
16
15
|
from ._storages import AsyncBaseStorage, AsyncFileStorage
|
|
17
16
|
|
|
18
17
|
if tp.TYPE_CHECKING: # pragma: no cover
|
|
@@ -211,11 +210,17 @@ class AsyncCacheTransport(httpx.AsyncBaseTransport):
|
|
|
211
210
|
)
|
|
212
211
|
|
|
213
212
|
regular_response = await self._transport.handle_async_request(request)
|
|
214
|
-
|
|
213
|
+
try:
|
|
214
|
+
# Prefer already-read content, if available
|
|
215
|
+
stream = fake_stream(regular_response.content)
|
|
216
|
+
except httpx.ResponseNotRead:
|
|
217
|
+
# Fall back to stream if not yet read
|
|
218
|
+
assert isinstance(regular_response.stream, tp.AsyncIterable)
|
|
219
|
+
stream = regular_response.stream
|
|
215
220
|
httpcore_regular_response = httpcore.Response(
|
|
216
221
|
status=regular_response.status_code,
|
|
217
222
|
headers=regular_response.headers.raw,
|
|
218
|
-
content=AsyncCacheStream(
|
|
223
|
+
content=AsyncCacheStream(stream),
|
|
219
224
|
extensions=regular_response.extensions,
|
|
220
225
|
)
|
|
221
226
|
await httpcore_regular_response.aread()
|
|
@@ -3,8 +3,7 @@ import typing as tp
|
|
|
3
3
|
|
|
4
4
|
from httpcore import Request, Response
|
|
5
5
|
|
|
6
|
-
from
|
|
7
|
-
|
|
6
|
+
from ._headers import Vary, parse_cache_control
|
|
8
7
|
from ._utils import (
|
|
9
8
|
BaseClock,
|
|
10
9
|
Clock,
|
|
@@ -21,7 +20,7 @@ logger = logging.getLogger("hishel.controller")
|
|
|
21
20
|
HEURISTICALLY_CACHEABLE_STATUS_CODES = (200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501)
|
|
22
21
|
HTTP_METHODS = ["GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"]
|
|
23
22
|
|
|
24
|
-
__all__ = ("
|
|
23
|
+
__all__ = ("HEURISTICALLY_CACHEABLE_STATUS_CODES", "Controller")
|
|
25
24
|
|
|
26
25
|
|
|
27
26
|
def get_updated_headers(
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from contextlib import contextmanager
|
|
2
|
+
from typing import Any, Iterator
|
|
3
|
+
|
|
4
|
+
class Database: ...
|
|
5
|
+
|
|
6
|
+
class Transaction:
|
|
7
|
+
def get(self, key: bytes, *, db: Database | None = None) -> bytes | None:
|
|
8
|
+
"""
|
|
9
|
+
Get the value associated with the given key.
|
|
10
|
+
"""
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
def put(self, key: bytes, value: bytes, *, db: Database | None = None, dupdata: bool = False) -> None:
|
|
14
|
+
"""
|
|
15
|
+
Put a key-value pair into the database.
|
|
16
|
+
"""
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
def delete(self, key: bytes, *, db: Database | None = None) -> bool:
|
|
20
|
+
"""
|
|
21
|
+
Delete the key from the database.
|
|
22
|
+
"""
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
def cursor(self, db: Database) -> Any:
|
|
26
|
+
"""
|
|
27
|
+
Create a cursor for iterating over key-value pairs in the database.
|
|
28
|
+
"""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
class Environment:
|
|
32
|
+
@contextmanager
|
|
33
|
+
def begin(self, *, db: Database | None = None, write: bool = False) -> Iterator[Transaction]:
|
|
34
|
+
"""
|
|
35
|
+
Begin a transaction in the environment.
|
|
36
|
+
"""
|
|
37
|
+
raise NotImplementedError("It's only for type hinting purposes")
|
|
38
|
+
|
|
39
|
+
def open_db(self, key: bytes, dupsort: bool = False) -> Database:
|
|
40
|
+
"""
|
|
41
|
+
Open a database within the environment.
|
|
42
|
+
"""
|
|
43
|
+
raise NotImplementedError("It's only for type hinting purposes")
|
|
44
|
+
|
|
45
|
+
def open(
|
|
46
|
+
path: str,
|
|
47
|
+
*,
|
|
48
|
+
max_dbs: int = 0,
|
|
49
|
+
) -> Environment:
|
|
50
|
+
"""
|
|
51
|
+
Open an LMDB environment at the specified path.
|
|
52
|
+
"""
|
|
53
|
+
raise NotImplementedError("It's only for type hinting purposes")
|
|
@@ -6,7 +6,7 @@ from datetime import datetime
|
|
|
6
6
|
|
|
7
7
|
from httpcore import Request, Response
|
|
8
8
|
|
|
9
|
-
from
|
|
9
|
+
from ._utils import normalized_url
|
|
10
10
|
|
|
11
11
|
try:
|
|
12
12
|
import yaml
|
|
@@ -17,7 +17,7 @@ HEADERS_ENCODING = "iso-8859-1"
|
|
|
17
17
|
KNOWN_RESPONSE_EXTENSIONS = ("http_version", "reason_phrase")
|
|
18
18
|
KNOWN_REQUEST_EXTENSIONS = ("timeout", "sni_hostname")
|
|
19
19
|
|
|
20
|
-
__all__ = ("
|
|
20
|
+
__all__ = ("BaseSerializer", "JSONSerializer", "Metadata", "PickleSerializer", "YAMLSerializer", "clone_model")
|
|
21
21
|
|
|
22
22
|
T = tp.TypeVar("T", Request, Response)
|
|
23
23
|
|
|
@@ -4,7 +4,6 @@ import datetime
|
|
|
4
4
|
import logging
|
|
5
5
|
import os
|
|
6
6
|
import time
|
|
7
|
-
import typing as t
|
|
8
7
|
import typing as tp
|
|
9
8
|
import warnings
|
|
10
9
|
from copy import deepcopy
|
|
@@ -24,13 +23,11 @@ except ImportError: # pragma: no cover
|
|
|
24
23
|
|
|
25
24
|
from httpcore import Request, Response
|
|
26
25
|
|
|
27
|
-
if
|
|
26
|
+
if tp.TYPE_CHECKING: # pragma: no cover
|
|
28
27
|
from typing_extensions import TypeAlias
|
|
29
28
|
|
|
30
|
-
from hishel._serializers import BaseSerializer, clone_model
|
|
31
|
-
|
|
32
29
|
from .._files import FileManager
|
|
33
|
-
from .._serializers import JSONSerializer, Metadata
|
|
30
|
+
from .._serializers import BaseSerializer, JSONSerializer, Metadata, clone_model
|
|
34
31
|
from .._synchronization import Lock
|
|
35
32
|
from .._utils import float_seconds_to_int_milliseconds
|
|
36
33
|
|
|
@@ -39,10 +36,10 @@ logger = logging.getLogger("hishel.storages")
|
|
|
39
36
|
__all__ = (
|
|
40
37
|
"BaseStorage",
|
|
41
38
|
"FileStorage",
|
|
42
|
-
"RedisStorage",
|
|
43
|
-
"SQLiteStorage",
|
|
44
39
|
"InMemoryStorage",
|
|
40
|
+
"RedisStorage",
|
|
45
41
|
"S3Storage",
|
|
42
|
+
"SQLiteStorage",
|
|
46
43
|
)
|
|
47
44
|
|
|
48
45
|
StoredResponse: TypeAlias = tp.Tuple[Response, Request, Metadata]
|
|
@@ -106,8 +103,7 @@ class FileStorage(BaseStorage):
|
|
|
106
103
|
self._base_path = Path(base_path) if base_path is not None else Path(".cache/hishel")
|
|
107
104
|
self._gitignore_file = self._base_path / ".gitignore"
|
|
108
105
|
|
|
109
|
-
|
|
110
|
-
self._base_path.mkdir(parents=True)
|
|
106
|
+
self._base_path.mkdir(parents=True, exist_ok=True)
|
|
111
107
|
|
|
112
108
|
if not self._gitignore_file.is_file():
|
|
113
109
|
with open(self._gitignore_file, "w", encoding="utf-8") as f:
|
|
@@ -153,7 +149,7 @@ class FileStorage(BaseStorage):
|
|
|
153
149
|
"""
|
|
154
150
|
|
|
155
151
|
if isinstance(key, Response): # pragma: no cover
|
|
156
|
-
key =
|
|
152
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
157
153
|
|
|
158
154
|
response_path = self._base_path / key
|
|
159
155
|
|
|
@@ -322,7 +318,7 @@ class SQLiteStorage(BaseStorage):
|
|
|
322
318
|
assert self._connection
|
|
323
319
|
|
|
324
320
|
if isinstance(key, Response): # pragma: no cover
|
|
325
|
-
key =
|
|
321
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
326
322
|
|
|
327
323
|
with self._lock:
|
|
328
324
|
self._connection.execute("DELETE FROM cache WHERE key = ?", [key])
|
|
@@ -459,7 +455,7 @@ class RedisStorage(BaseStorage):
|
|
|
459
455
|
"""
|
|
460
456
|
|
|
461
457
|
if isinstance(key, Response): # pragma: no cover
|
|
462
|
-
key =
|
|
458
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
463
459
|
|
|
464
460
|
self._client.delete(key)
|
|
465
461
|
|
|
@@ -572,7 +568,7 @@ class InMemoryStorage(BaseStorage):
|
|
|
572
568
|
"""
|
|
573
569
|
|
|
574
570
|
if isinstance(key, Response): # pragma: no cover
|
|
575
|
-
key =
|
|
571
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
576
572
|
|
|
577
573
|
with self._lock:
|
|
578
574
|
self._cache.remove_key(key)
|
|
@@ -720,7 +716,7 @@ class S3Storage(BaseStorage): # pragma: no cover
|
|
|
720
716
|
"""
|
|
721
717
|
|
|
722
718
|
if isinstance(key, Response): # pragma: no cover
|
|
723
|
-
key =
|
|
719
|
+
key = tp.cast(str, key.extensions["cache_metadata"]["cache_key"])
|
|
724
720
|
|
|
725
721
|
with self._lock:
|
|
726
722
|
self._s3_manager.remove_entry(key)
|
|
@@ -8,11 +8,10 @@ import httpx
|
|
|
8
8
|
from httpx import SyncByteStream, Request, Response
|
|
9
9
|
from httpx._exceptions import ConnectError
|
|
10
10
|
|
|
11
|
-
from hishel._utils import extract_header_values_decoded, normalized_url
|
|
12
|
-
|
|
13
11
|
from .._controller import Controller, allowed_stale
|
|
14
12
|
from .._headers import parse_cache_control
|
|
15
13
|
from .._serializers import JSONSerializer, Metadata
|
|
14
|
+
from .._utils import extract_header_values_decoded, normalized_url
|
|
16
15
|
from ._storages import BaseStorage, FileStorage
|
|
17
16
|
|
|
18
17
|
if tp.TYPE_CHECKING: # pragma: no cover
|
|
@@ -211,11 +210,17 @@ class CacheTransport(httpx.BaseTransport):
|
|
|
211
210
|
)
|
|
212
211
|
|
|
213
212
|
regular_response = self._transport.handle_request(request)
|
|
214
|
-
|
|
213
|
+
try:
|
|
214
|
+
# Prefer already-read content, if available
|
|
215
|
+
stream = fake_stream(regular_response.content)
|
|
216
|
+
except httpx.ResponseNotRead:
|
|
217
|
+
# Fall back to stream if not yet read
|
|
218
|
+
assert isinstance(regular_response.stream, tp.Iterable)
|
|
219
|
+
stream = regular_response.stream
|
|
215
220
|
httpcore_regular_response = httpcore.Response(
|
|
216
221
|
status=regular_response.status_code,
|
|
217
222
|
headers=regular_response.headers.raw,
|
|
218
|
-
content=CacheStream(
|
|
223
|
+
content=CacheStream(stream),
|
|
219
224
|
extensions=regular_response.extensions,
|
|
220
225
|
)
|
|
221
226
|
httpcore_regular_response.read()
|