hishel 0.1.3__py3-none-any.whl → 0.1.4__py3-none-any.whl
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/__init__.py +41 -1
- hishel/_async/_client.py +1 -1
- hishel/_async/_storages.py +10 -14
- hishel/_async/_transports.py +9 -4
- hishel/_controller.py +2 -3
- hishel/_lmdb_types_.pyi +53 -0
- hishel/_serializers.py +2 -2
- hishel/_sync/_client.py +1 -1
- hishel/_sync/_storages.py +10 -14
- hishel/_sync/_transports.py +9 -4
- hishel/_utils.py +340 -0
- hishel/beta/__init__.py +59 -0
- hishel/beta/_async_cache.py +167 -0
- hishel/beta/_core/__init__.py +0 -0
- hishel/beta/_core/_async/_storages/_sqlite.py +411 -0
- hishel/beta/_core/_base/_storages/_base.py +260 -0
- hishel/beta/_core/_base/_storages/_packing.py +165 -0
- hishel/beta/_core/_headers.py +301 -0
- hishel/beta/_core/_spec.py +2291 -0
- hishel/beta/_core/_sync/_storages/_sqlite.py +411 -0
- hishel/beta/_core/models.py +176 -0
- hishel/beta/_sync_cache.py +167 -0
- hishel/beta/httpx.py +317 -0
- hishel/beta/requests.py +193 -0
- {hishel-0.1.3.dist-info → hishel-0.1.4.dist-info}/METADATA +41 -4
- hishel-0.1.4.dist-info/RECORD +41 -0
- hishel-0.1.3.dist-info/RECORD +0 -27
- {hishel-0.1.3.dist-info → hishel-0.1.4.dist-info}/WHEEL +0 -0
- {hishel-0.1.3.dist-info → hishel-0.1.4.dist-info}/licenses/LICENSE +0 -0
hishel/__init__.py
CHANGED
|
@@ -8,10 +8,50 @@ from ._serializers import *
|
|
|
8
8
|
from ._sync import *
|
|
9
9
|
from ._lfu_cache import *
|
|
10
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
|
+
)
|
|
11
50
|
|
|
12
51
|
def install_cache() -> None: # pragma: no cover
|
|
13
52
|
httpx.AsyncClient = AsyncCacheClient # type: ignore
|
|
14
53
|
httpx.Client = CacheClient # type: ignore
|
|
15
54
|
|
|
16
55
|
|
|
17
|
-
__version__ = "0.1.
|
|
56
|
+
__version__ = "0.1.4"
|
|
57
|
+
|
hishel/_async/_client.py
CHANGED
hishel/_async/_storages.py
CHANGED
|
@@ -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)
|
hishel/_async/_transports.py
CHANGED
|
@@ -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()
|
hishel/_controller.py
CHANGED
|
@@ -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(
|
hishel/_lmdb_types_.pyi
ADDED
|
@@ -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")
|
hishel/_serializers.py
CHANGED
|
@@ -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
|
|
hishel/_sync/_client.py
CHANGED
hishel/_sync/_storages.py
CHANGED
|
@@ -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)
|
hishel/_sync/_transports.py
CHANGED
|
@@ -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()
|