hishel 0.1.5__tar.gz → 1.0.0__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.5 → hishel-1.0.0}/.gitignore +2 -1
- hishel-1.0.0/CHANGELOG.md +105 -0
- hishel-1.0.0/PKG-INFO +513 -0
- hishel-1.0.0/README.md +367 -0
- hishel-1.0.0/hishel/__init__.py +68 -0
- hishel-1.0.0/hishel/_async_cache.py +213 -0
- hishel-1.0.0/hishel/_async_httpx.py +236 -0
- {hishel-0.1.5/hishel/beta → hishel-1.0.0/hishel}/_core/_headers.py +11 -1
- {hishel-0.1.5/hishel/beta → hishel-1.0.0/hishel}/_core/_spec.py +270 -136
- hishel-1.0.0/hishel/_core/_storages/_async_base.py +71 -0
- hishel-1.0.0/hishel/_core/_storages/_async_sqlite.py +420 -0
- hishel-1.0.0/hishel/_core/_storages/_packing.py +144 -0
- hishel-1.0.0/hishel/_core/_storages/_sync_base.py +71 -0
- hishel-1.0.0/hishel/_core/_storages/_sync_sqlite.py +420 -0
- {hishel-0.1.5/hishel/beta → hishel-1.0.0/hishel}/_core/models.py +100 -37
- hishel-1.0.0/hishel/_policies.py +49 -0
- hishel-1.0.0/hishel/_sync_cache.py +213 -0
- hishel-1.0.0/hishel/_sync_httpx.py +236 -0
- hishel-1.0.0/hishel/_utils.py +129 -0
- hishel-1.0.0/hishel/asgi.py +400 -0
- hishel-1.0.0/hishel/fastapi.py +263 -0
- hishel-1.0.0/hishel/httpx.py +12 -0
- {hishel-0.1.5/hishel/beta → hishel-1.0.0/hishel}/requests.py +31 -25
- {hishel-0.1.5 → hishel-1.0.0}/pyproject.toml +27 -23
- hishel-0.1.5/CHANGELOG.md +0 -64
- hishel-0.1.5/PKG-INFO +0 -258
- hishel-0.1.5/README.md +0 -148
- hishel-0.1.5/hishel/__init__.py +0 -57
- hishel-0.1.5/hishel/_async/__init__.py +0 -5
- hishel-0.1.5/hishel/_async/_client.py +0 -30
- hishel-0.1.5/hishel/_async/_mock.py +0 -43
- hishel-0.1.5/hishel/_async/_pool.py +0 -201
- hishel-0.1.5/hishel/_async/_storages.py +0 -768
- hishel-0.1.5/hishel/_async/_transports.py +0 -282
- hishel-0.1.5/hishel/_controller.py +0 -581
- hishel-0.1.5/hishel/_exceptions.py +0 -10
- hishel-0.1.5/hishel/_files.py +0 -54
- hishel-0.1.5/hishel/_headers.py +0 -215
- hishel-0.1.5/hishel/_lfu_cache.py +0 -71
- hishel-0.1.5/hishel/_lmdb_types_.pyi +0 -53
- hishel-0.1.5/hishel/_s3.py +0 -122
- hishel-0.1.5/hishel/_serializers.py +0 -329
- hishel-0.1.5/hishel/_sync/__init__.py +0 -5
- hishel-0.1.5/hishel/_sync/_client.py +0 -30
- hishel-0.1.5/hishel/_sync/_mock.py +0 -43
- hishel-0.1.5/hishel/_sync/_pool.py +0 -201
- hishel-0.1.5/hishel/_sync/_storages.py +0 -768
- hishel-0.1.5/hishel/_sync/_transports.py +0 -282
- hishel-0.1.5/hishel/_synchronization.py +0 -37
- hishel-0.1.5/hishel/_utils.py +0 -458
- hishel-0.1.5/hishel/beta/__init__.py +0 -59
- hishel-0.1.5/hishel/beta/_async_cache.py +0 -167
- hishel-0.1.5/hishel/beta/_core/__init__.py +0 -0
- hishel-0.1.5/hishel/beta/_core/_async/_storages/_sqlite.py +0 -411
- hishel-0.1.5/hishel/beta/_core/_base/_storages/_base.py +0 -272
- hishel-0.1.5/hishel/beta/_core/_base/_storages/_packing.py +0 -165
- hishel-0.1.5/hishel/beta/_core/_sync/_storages/_sqlite.py +0 -411
- hishel-0.1.5/hishel/beta/_sync_cache.py +0 -167
- hishel-0.1.5/hishel/beta/httpx.py +0 -328
- {hishel-0.1.5 → hishel-1.0.0}/LICENSE +0 -0
- {hishel-0.1.5 → hishel-1.0.0}/hishel/py.typed +0 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## 1.0.0 - 2025-10-28
|
|
6
|
+
### ⚙️ Miscellaneous Tasks
|
|
7
|
+
- Add examples, improve docs
|
|
8
|
+
|
|
9
|
+
## 1.0.0b1 - 2025-10-28
|
|
10
|
+
### ♻️ Refactoring
|
|
11
|
+
- Add policies
|
|
12
|
+
|
|
13
|
+
### ⚙️ Miscellaneous Tasks
|
|
14
|
+
- Improve sans-io diagram colors
|
|
15
|
+
- Add graphql docs
|
|
16
|
+
|
|
17
|
+
### 🐛 Bug Fixes
|
|
18
|
+
- Body-sensitive responses caching
|
|
19
|
+
- Filter out `Transfer-Encoding` header for asgi responses
|
|
20
|
+
|
|
21
|
+
### 🚀 Features
|
|
22
|
+
- Add global `use_body_key` setting
|
|
23
|
+
|
|
24
|
+
## 1.0.0.dev3 - 2025-10-26
|
|
25
|
+
### ♻️ Refactoring
|
|
26
|
+
- Replace pairs with entries, simplify storage API
|
|
27
|
+
- Automatically generate httpx sync integration from async
|
|
28
|
+
|
|
29
|
+
### ⚙️ Miscellaneous Tasks
|
|
30
|
+
- Simplify metadata docs
|
|
31
|
+
- Add custom integrations docs
|
|
32
|
+
- More robust compressed response caching
|
|
33
|
+
|
|
34
|
+
### 🐛 Bug Fixes
|
|
35
|
+
- Add missing permissions into `publish.yml`
|
|
36
|
+
- Raise on consumed httpx streams, which we can't store as is (it's already decoded)
|
|
37
|
+
- Fix compressed data caching for requests
|
|
38
|
+
- Handle httpx iterable usage instead of iterator correctly
|
|
39
|
+
- Add date header for proper age calculation
|
|
40
|
+
|
|
41
|
+
### 🚀 Features
|
|
42
|
+
- Add integrations with fastapi and asgi
|
|
43
|
+
- Add blacksheep integration examples
|
|
44
|
+
- Add logging for asgi
|
|
45
|
+
|
|
46
|
+
## 1.0.0.dev2 - 2025-10-21
|
|
47
|
+
### ⚙️ Miscellaneous Tasks
|
|
48
|
+
- Remove redundant utils and tests
|
|
49
|
+
- Add import without extras check in ci
|
|
50
|
+
- Fix time travel date, explicitly specify the timezone
|
|
51
|
+
|
|
52
|
+
### 🐛 Bug Fixes
|
|
53
|
+
- Fix check for storing auth requests
|
|
54
|
+
- Don't raise an error on 3xx during revalidation
|
|
55
|
+
|
|
56
|
+
### 🚀 Features
|
|
57
|
+
- Add hishel_created_at response metadata
|
|
58
|
+
|
|
59
|
+
## 1.0.0.dev1 - 2025-10-21
|
|
60
|
+
### ⚙️ Miscellaneous Tasks
|
|
61
|
+
- Remove some redundant utils methods
|
|
62
|
+
|
|
63
|
+
### 📦 Dependencies
|
|
64
|
+
- Make httpx and async libs optional dependencies
|
|
65
|
+
- Make `anysqlite` optional dependency
|
|
66
|
+
- Install async extra with httpx
|
|
67
|
+
- Improve git-cliff
|
|
68
|
+
|
|
69
|
+
## 1.0.0.dev0 - 2025-10-19
|
|
70
|
+
### ⚙️ Miscellaneous Tasks
|
|
71
|
+
- Use mike powered versioning
|
|
72
|
+
- Improve docs versioning, deploy dev doc on ci
|
|
73
|
+
|
|
74
|
+
## 0.1.5 - 2025-10-18
|
|
75
|
+
### ⚙️ Miscellaneous Tasks
|
|
76
|
+
- Remove some redundant files from repo
|
|
77
|
+
|
|
78
|
+
### 🐛 Bug Fixes
|
|
79
|
+
- Fix some line breaks
|
|
80
|
+
|
|
81
|
+
### 🚀 Features
|
|
82
|
+
- Set chunk size to 128KB for httpx to reduce SQLite read/writes
|
|
83
|
+
- Better cache-control parsing
|
|
84
|
+
- Add close method to storages API (#384)
|
|
85
|
+
- Increase requests buffer size to 128KB, disable charset detection
|
|
86
|
+
|
|
87
|
+
## 0.1.4 - 2025-10-14
|
|
88
|
+
### ⚙️ Miscellaneous Tasks
|
|
89
|
+
- Improve CI (#369)
|
|
90
|
+
- Remove src folder (#373)
|
|
91
|
+
- Temporary remove python3.14 from CI
|
|
92
|
+
- Add sqlite tests for new storage
|
|
93
|
+
- Move some tests to beta
|
|
94
|
+
|
|
95
|
+
### 🐛 Bug Fixes
|
|
96
|
+
- Create an sqlite file in a cache folder
|
|
97
|
+
- Fix beta imports
|
|
98
|
+
|
|
99
|
+
### 🚀 Features
|
|
100
|
+
- Add support for a sans-IO API (#366)
|
|
101
|
+
- Allow already consumed streams with `CacheTransport` (#377)
|
|
102
|
+
- Add sqlite storage for beta storages
|
|
103
|
+
- Get rid of some locks from sqlite storage
|
|
104
|
+
- Better async implemetation for sqlite storage
|
|
105
|
+
|
hishel-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hishel
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Elegant HTTP Caching for Python
|
|
5
|
+
Project-URL: Homepage, https://hishel.com
|
|
6
|
+
Project-URL: Source, https://github.com/karpetrosyan/hishel
|
|
7
|
+
Author-email: Kar Petrosyan <kar.petrosyanpy@gmail.com>
|
|
8
|
+
License-Expression: BSD-3-Clause
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Environment :: Web Environment
|
|
12
|
+
Classifier: Framework :: AsyncIO
|
|
13
|
+
Classifier: Framework :: Trio
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
26
|
+
Requires-Python: >=3.9
|
|
27
|
+
Requires-Dist: msgpack>=1.1.2
|
|
28
|
+
Requires-Dist: typing-extensions>=4.14.1
|
|
29
|
+
Provides-Extra: async
|
|
30
|
+
Requires-Dist: anyio>=4.9.0; extra == 'async'
|
|
31
|
+
Requires-Dist: anysqlite>=0.0.5; extra == 'async'
|
|
32
|
+
Provides-Extra: fastapi
|
|
33
|
+
Requires-Dist: fastapi>=0.119.1; extra == 'fastapi'
|
|
34
|
+
Provides-Extra: httpx
|
|
35
|
+
Requires-Dist: anyio>=4.9.0; extra == 'httpx'
|
|
36
|
+
Requires-Dist: anysqlite>=0.0.5; extra == 'httpx'
|
|
37
|
+
Requires-Dist: httpx>=0.28.1; extra == 'httpx'
|
|
38
|
+
Provides-Extra: requests
|
|
39
|
+
Requires-Dist: requests>=2.32.5; extra == 'requests'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
<p align="center">
|
|
44
|
+
<img alt="Hishel Logo" width="350" src="https://raw.githubusercontent.com/karpetrosyan/hishel/master/docs/static/Shelkopryad_350x250_yellow.png#gh-dark-mode-only">
|
|
45
|
+
<img alt="Hishel Logo" width="350" src="https://raw.githubusercontent.com/karpetrosyan/hishel/master/docs/static/Shelkopryad_350x250_black.png#gh-light-mode-only">
|
|
46
|
+
</p>
|
|
47
|
+
|
|
48
|
+
<h1 align="center">Hishel</h1>
|
|
49
|
+
|
|
50
|
+
<p align="center">
|
|
51
|
+
<strong>Elegant HTTP Caching for Python</strong>
|
|
52
|
+
</p>
|
|
53
|
+
|
|
54
|
+
<p align="center">
|
|
55
|
+
<a href="https://pypi.org/project/hishel">
|
|
56
|
+
<img src="https://img.shields.io/pypi/v/hishel.svg" alt="PyPI version">
|
|
57
|
+
</a>
|
|
58
|
+
<a href="https://pypi.org/project/hishel">
|
|
59
|
+
<img src="https://img.shields.io/pypi/pyversions/hishel.svg" alt="Python versions">
|
|
60
|
+
</a>
|
|
61
|
+
<a href="https://github.com/karpetrosyan/hishel/blob/master/LICENSE">
|
|
62
|
+
<img src="https://img.shields.io/pypi/l/hishel" alt="License">
|
|
63
|
+
</a>
|
|
64
|
+
<a href="https://coveralls.io/github/karpetrosyan/hishel">
|
|
65
|
+
<img src="https://img.shields.io/coverallsCoverage/github/karpetrosyan/hishel" alt="Coverage">
|
|
66
|
+
</a>
|
|
67
|
+
<a href="https://static.pepy.tech/badge/hishel/month">
|
|
68
|
+
<img src="https://static.pepy.tech/badge/hishel/month" alt="Downloads">
|
|
69
|
+
</a>
|
|
70
|
+
</p>
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
**Hishel** (հիշել, *to remember* in Armenian) is a modern HTTP caching library for Python that implements [RFC 9111](https://www.rfc-editor.org/rfc/rfc9111.html) specifications. It provides seamless caching integration for popular HTTP clients with minimal code changes.
|
|
75
|
+
|
|
76
|
+
## ✨ Features
|
|
77
|
+
|
|
78
|
+
- 🎯 **RFC 9111 Compliant** - Fully compliant with the latest HTTP caching specification
|
|
79
|
+
- 🔌 **Easy Integration** - Drop-in support for HTTPX, Requests, ASGI, FastAPI, and BlackSheep
|
|
80
|
+
- 💾 **Flexible Storage** - SQLite backend with more coming soon
|
|
81
|
+
- ⚡ **High Performance** - Efficient caching with minimal overhead
|
|
82
|
+
- 🔄 **Async & Sync** - Full support for both synchronous and asynchronous workflows
|
|
83
|
+
- 🎨 **Type Safe** - Fully typed with comprehensive type hints
|
|
84
|
+
- 🧪 **Well Tested** - Extensive test coverage and battle-tested
|
|
85
|
+
- 🎛️ **Configurable** - Fine-grained control over caching behavior with flexible policies
|
|
86
|
+
- 💨 **Memory Efficient** - Streaming support prevents loading large payloads into memory
|
|
87
|
+
- 🌐 **Universal** - Works with any ASGI application (Starlette, Litestar, BlackSheep, etc.)
|
|
88
|
+
- 🎯 **GraphQL Support** - Cache GraphQL queries with body-sensitive content caching
|
|
89
|
+
|
|
90
|
+
## 📦 Installation
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pip install hishel
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Optional Dependencies
|
|
97
|
+
|
|
98
|
+
Install with specific integration support:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pip install hishel[httpx] # For HTTPX support
|
|
102
|
+
pip install hishel[requests] # For Requests support
|
|
103
|
+
pip install hishel[fastapi] # For FastAPI support (includes ASGI)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Or install multiple:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
pip install hishel[httpx,requests,fastapi]
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
> [!NOTE]
|
|
113
|
+
> ASGI middleware has no extra dependencies - it's included in the base installation.
|
|
114
|
+
|
|
115
|
+
## 🚀 Quick Start
|
|
116
|
+
|
|
117
|
+
### With HTTPX
|
|
118
|
+
|
|
119
|
+
**Synchronous:**
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from hishel.httpx import SyncCacheClient
|
|
123
|
+
|
|
124
|
+
client = SyncCacheClient()
|
|
125
|
+
|
|
126
|
+
# First request - fetches from origin
|
|
127
|
+
response = client.get("https://api.example.com/data")
|
|
128
|
+
print(response.extensions["hishel_from_cache"]) # False
|
|
129
|
+
|
|
130
|
+
# Second request - served from cache
|
|
131
|
+
response = client.get("https://api.example.com/data")
|
|
132
|
+
print(response.extensions["hishel_from_cache"]) # True
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**Asynchronous:**
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from hishel.httpx import AsyncCacheClient
|
|
139
|
+
|
|
140
|
+
async with AsyncCacheClient() as client:
|
|
141
|
+
# First request - fetches from origin
|
|
142
|
+
response = await client.get("https://api.example.com/data")
|
|
143
|
+
print(response.extensions["hishel_from_cache"]) # False
|
|
144
|
+
|
|
145
|
+
# Second request - served from cache
|
|
146
|
+
response = await client.get("https://api.example.com/data")
|
|
147
|
+
print(response.extensions["hishel_from_cache"]) # True
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### With Requests
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
import requests
|
|
154
|
+
from hishel.requests import CacheAdapter
|
|
155
|
+
|
|
156
|
+
session = requests.Session()
|
|
157
|
+
session.mount("https://", CacheAdapter())
|
|
158
|
+
session.mount("http://", CacheAdapter())
|
|
159
|
+
|
|
160
|
+
# First request - fetches from origin
|
|
161
|
+
response = session.get("https://api.example.com/data")
|
|
162
|
+
|
|
163
|
+
# Second request - served from cache
|
|
164
|
+
response = session.get("https://api.example.com/data")
|
|
165
|
+
print(response.headers.get("X-Hishel-From-Cache")) # "True"
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### With ASGI Applications
|
|
169
|
+
|
|
170
|
+
Add caching middleware to any ASGI application:
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
from hishel.asgi import ASGICacheMiddleware
|
|
174
|
+
|
|
175
|
+
# Wrap your ASGI app
|
|
176
|
+
app = ASGICacheMiddleware(app)
|
|
177
|
+
|
|
178
|
+
# Or configure with options
|
|
179
|
+
from hishel import AsyncSqliteStorage, CacheOptions, SpecificationPolicy
|
|
180
|
+
|
|
181
|
+
app = ASGICacheMiddleware(
|
|
182
|
+
app,
|
|
183
|
+
storage=AsyncSqliteStorage(),
|
|
184
|
+
policy=SpecificationPolicy(
|
|
185
|
+
cache_options=CacheOptions(shared=True)
|
|
186
|
+
),
|
|
187
|
+
)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### With FastAPI
|
|
191
|
+
|
|
192
|
+
Add Cache-Control headers using the `cache()` dependency:
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from fastapi import FastAPI
|
|
196
|
+
from hishel.fastapi import cache
|
|
197
|
+
|
|
198
|
+
app = FastAPI()
|
|
199
|
+
|
|
200
|
+
@app.get("/api/data", dependencies=[cache(max_age=300, public=True)])
|
|
201
|
+
async def get_data():
|
|
202
|
+
# Cache-Control: public, max-age=300
|
|
203
|
+
return {"data": "cached for 5 minutes"}
|
|
204
|
+
|
|
205
|
+
# Optionally wrap with ASGI middleware for local caching according to specified rules
|
|
206
|
+
from hishel.asgi import ASGICacheMiddleware
|
|
207
|
+
from hishel import AsyncSqliteStorage
|
|
208
|
+
|
|
209
|
+
app = ASGICacheMiddleware(app, storage=AsyncSqliteStorage())
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### With BlackSheep
|
|
213
|
+
|
|
214
|
+
Use BlackSheep's native `cache_control` decorator with Hishel's ASGI middleware:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
from blacksheep import Application, get
|
|
218
|
+
from blacksheep.server.headers.cache import cache_control
|
|
219
|
+
|
|
220
|
+
app = Application()
|
|
221
|
+
|
|
222
|
+
@get("/api/data")
|
|
223
|
+
@cache_control(max_age=300, public=True)
|
|
224
|
+
async def get_data():
|
|
225
|
+
# Cache-Control: public, max-age=300
|
|
226
|
+
return {"data": "cached for 5 minutes"}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## 🎛️ Advanced Configuration
|
|
230
|
+
|
|
231
|
+
### Caching Policies
|
|
232
|
+
|
|
233
|
+
Hishel supports two types of caching policies:
|
|
234
|
+
|
|
235
|
+
**SpecificationPolicy** - RFC 9111 compliant HTTP caching (default):
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
from hishel import CacheOptions, SpecificationPolicy
|
|
239
|
+
from hishel.httpx import SyncCacheClient
|
|
240
|
+
|
|
241
|
+
client = SyncCacheClient(
|
|
242
|
+
policy=SpecificationPolicy(
|
|
243
|
+
cache_options=CacheOptions(
|
|
244
|
+
shared=False, # Use as private cache (browser-like)
|
|
245
|
+
supported_methods=["GET", "HEAD", "POST"], # Cache GET, HEAD, and POST
|
|
246
|
+
allow_stale=True # Allow serving stale responses
|
|
247
|
+
)
|
|
248
|
+
)
|
|
249
|
+
)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
**FilterPolicy** - Custom filtering logic for fine-grained control:
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
from hishel import FilterPolicy, BaseFilter, Request
|
|
256
|
+
from hishel.httpx import AsyncCacheClient
|
|
257
|
+
|
|
258
|
+
class CacheOnlyAPIRequests(BaseFilter[Request]):
|
|
259
|
+
def needs_body(self) -> bool:
|
|
260
|
+
return False
|
|
261
|
+
|
|
262
|
+
def apply(self, item: Request, body: bytes | None) -> bool:
|
|
263
|
+
return "/api/" in str(item.url)
|
|
264
|
+
|
|
265
|
+
client = AsyncCacheClient(
|
|
266
|
+
policy=FilterPolicy(
|
|
267
|
+
request_filters=[CacheOnlyAPIRequests()]
|
|
268
|
+
)
|
|
269
|
+
)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
[Learn more about policies →](https://hishel.com/dev/policies/)
|
|
273
|
+
|
|
274
|
+
### Custom Storage Backend
|
|
275
|
+
|
|
276
|
+
```python
|
|
277
|
+
from hishel import SyncSqliteStorage
|
|
278
|
+
from hishel.httpx import SyncCacheClient
|
|
279
|
+
|
|
280
|
+
storage = SyncSqliteStorage(
|
|
281
|
+
database_path="my_cache.db",
|
|
282
|
+
default_ttl=7200.0, # Cache entries expire after 2 hours
|
|
283
|
+
refresh_ttl_on_access=True # Reset TTL when accessing cached entries
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
client = SyncCacheClient(storage=storage)
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### GraphQL and Body-Sensitive Caching
|
|
290
|
+
|
|
291
|
+
Cache GraphQL queries and other POST requests by including the request body in the cache key.
|
|
292
|
+
|
|
293
|
+
**Using per-request header:**
|
|
294
|
+
|
|
295
|
+
```python
|
|
296
|
+
from hishel import FilterPolicy
|
|
297
|
+
from hishel.httpx import SyncCacheClient
|
|
298
|
+
|
|
299
|
+
client = SyncCacheClient(
|
|
300
|
+
policy=FilterPolicy()
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
# Cache GraphQL queries - different queries get different cache entries
|
|
304
|
+
graphql_query = """
|
|
305
|
+
query GetUser($id: ID!) {
|
|
306
|
+
user(id: $id) {
|
|
307
|
+
name
|
|
308
|
+
email
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
"""
|
|
312
|
+
|
|
313
|
+
response = client.post(
|
|
314
|
+
"https://api.example.com/graphql",
|
|
315
|
+
json={"query": graphql_query, "variables": {"id": "123"}},
|
|
316
|
+
headers={"X-Hishel-Body-Key": "true"} # Enable body-based caching
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
# Different query will be cached separately
|
|
320
|
+
response = client.post(
|
|
321
|
+
"https://api.example.com/graphql",
|
|
322
|
+
json={"query": graphql_query, "variables": {"id": "456"}},
|
|
323
|
+
headers={"X-Hishel-Body-Key": "true"}
|
|
324
|
+
)
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
**Using global configuration:**
|
|
328
|
+
|
|
329
|
+
```python
|
|
330
|
+
from hishel.httpx import SyncCacheClient
|
|
331
|
+
from hishel import FilterPolicy
|
|
332
|
+
|
|
333
|
+
# Enable body-based caching for all requests
|
|
334
|
+
client = SyncCacheClient(policy=FilterPolicy(use_body_key=True))
|
|
335
|
+
|
|
336
|
+
# All POST requests automatically include body in cache key
|
|
337
|
+
response = client.post(
|
|
338
|
+
"https://api.example.com/graphql",
|
|
339
|
+
json={"query": graphql_query, "variables": {"id": "123"}}
|
|
340
|
+
)
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## 🏗️ Architecture
|
|
344
|
+
|
|
345
|
+
Hishel uses a **sans-I/O state machine** architecture that separates HTTP caching logic from I/O operations:
|
|
346
|
+
|
|
347
|
+
- ✅ **Correct** - Fully RFC 9111 compliant
|
|
348
|
+
- ✅ **Testable** - Easy to test without network dependencies
|
|
349
|
+
- ✅ **Flexible** - Works with any HTTP client or server
|
|
350
|
+
- ✅ **Type Safe** - Clear state transitions with full type hints
|
|
351
|
+
|
|
352
|
+
## 🔮 Roadmap
|
|
353
|
+
|
|
354
|
+
We're actively working on:
|
|
355
|
+
|
|
356
|
+
- 🎯 Performance optimizations
|
|
357
|
+
- 🎯 More integrations
|
|
358
|
+
- 🎯 Partial responses support
|
|
359
|
+
|
|
360
|
+
## 📚 Documentation
|
|
361
|
+
|
|
362
|
+
Comprehensive documentation is available at [https://hishel.com/dev](https://hishel.com/dev)
|
|
363
|
+
|
|
364
|
+
- [Getting Started](https://hishel.com)
|
|
365
|
+
- [HTTPX Integration](https://hishel.com/dev/integrations/httpx)
|
|
366
|
+
- [Requests Integration](https://hishel.com/dev/integrations/requests)
|
|
367
|
+
- [ASGI Integration](https://hishel.com/dev/asgi)
|
|
368
|
+
- [FastAPI Integration](https://hishel.com/dev/fastapi)
|
|
369
|
+
- [BlackSheep Integration](https://hishel.com/dev/integrations/blacksheep)
|
|
370
|
+
- [GraphQL Integration](https://hishel.com/dev/integrations/graphql)
|
|
371
|
+
- [Storage Backends](https://hishel.com/dev/storages)
|
|
372
|
+
- [Request/Response Metadata](https://hishel.com/dev/metadata)
|
|
373
|
+
- [RFC 9111 Specification](https://hishel.com/dev/specification)
|
|
374
|
+
|
|
375
|
+
## 🤝 Contributing
|
|
376
|
+
|
|
377
|
+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
378
|
+
|
|
379
|
+
See our [Contributing Guide](https://hishel.com/dev/contributing) for more details.
|
|
380
|
+
|
|
381
|
+
## 📄 License
|
|
382
|
+
|
|
383
|
+
This project is licensed under the BSD-3-Clause License - see the [LICENSE](LICENSE) file for details.
|
|
384
|
+
|
|
385
|
+
## 💖 Support
|
|
386
|
+
|
|
387
|
+
If you find Hishel useful, please consider:
|
|
388
|
+
|
|
389
|
+
- ⭐ Starring the repository
|
|
390
|
+
- 🐛 Reporting bugs and issues
|
|
391
|
+
- 💡 Suggesting new features
|
|
392
|
+
- 📖 Improving documentation
|
|
393
|
+
- ☕ [Buying me a coffee](https://buymeacoffee.com/karpetrosyan)
|
|
394
|
+
|
|
395
|
+
## 🙏 Acknowledgments
|
|
396
|
+
|
|
397
|
+
Hishel is inspired by and builds upon the excellent work in the Python HTTP ecosystem, particularly:
|
|
398
|
+
|
|
399
|
+
- [HTTPX](https://github.com/encode/httpx) - A next-generation HTTP client for Python
|
|
400
|
+
- [Requests](https://github.com/psf/requests) - The classic HTTP library for Python
|
|
401
|
+
- [RFC 9111](https://www.rfc-editor.org/rfc/rfc9111.html) - HTTP Caching specification
|
|
402
|
+
|
|
403
|
+
---
|
|
404
|
+
|
|
405
|
+
<p align="center">
|
|
406
|
+
<strong>Made with ❤️ by <a href="https://github.com/karpetrosyan">Kar Petrosyan</a></strong>
|
|
407
|
+
</p>
|
|
408
|
+
|
|
409
|
+
# Changelog
|
|
410
|
+
|
|
411
|
+
All notable changes to this project will be documented in this file.
|
|
412
|
+
|
|
413
|
+
## 1.0.0 - 2025-10-28
|
|
414
|
+
### ⚙️ Miscellaneous Tasks
|
|
415
|
+
- Add examples, improve docs
|
|
416
|
+
|
|
417
|
+
## 1.0.0b1 - 2025-10-28
|
|
418
|
+
### ♻️ Refactoring
|
|
419
|
+
- Add policies
|
|
420
|
+
|
|
421
|
+
### ⚙️ Miscellaneous Tasks
|
|
422
|
+
- Improve sans-io diagram colors
|
|
423
|
+
- Add graphql docs
|
|
424
|
+
|
|
425
|
+
### 🐛 Bug Fixes
|
|
426
|
+
- Body-sensitive responses caching
|
|
427
|
+
- Filter out `Transfer-Encoding` header for asgi responses
|
|
428
|
+
|
|
429
|
+
### 🚀 Features
|
|
430
|
+
- Add global `use_body_key` setting
|
|
431
|
+
|
|
432
|
+
## 1.0.0.dev3 - 2025-10-26
|
|
433
|
+
### ♻️ Refactoring
|
|
434
|
+
- Replace pairs with entries, simplify storage API
|
|
435
|
+
- Automatically generate httpx sync integration from async
|
|
436
|
+
|
|
437
|
+
### ⚙️ Miscellaneous Tasks
|
|
438
|
+
- Simplify metadata docs
|
|
439
|
+
- Add custom integrations docs
|
|
440
|
+
- More robust compressed response caching
|
|
441
|
+
|
|
442
|
+
### 🐛 Bug Fixes
|
|
443
|
+
- Add missing permissions into `publish.yml`
|
|
444
|
+
- Raise on consumed httpx streams, which we can't store as is (it's already decoded)
|
|
445
|
+
- Fix compressed data caching for requests
|
|
446
|
+
- Handle httpx iterable usage instead of iterator correctly
|
|
447
|
+
- Add date header for proper age calculation
|
|
448
|
+
|
|
449
|
+
### 🚀 Features
|
|
450
|
+
- Add integrations with fastapi and asgi
|
|
451
|
+
- Add blacksheep integration examples
|
|
452
|
+
- Add logging for asgi
|
|
453
|
+
|
|
454
|
+
## 1.0.0.dev2 - 2025-10-21
|
|
455
|
+
### ⚙️ Miscellaneous Tasks
|
|
456
|
+
- Remove redundant utils and tests
|
|
457
|
+
- Add import without extras check in ci
|
|
458
|
+
- Fix time travel date, explicitly specify the timezone
|
|
459
|
+
|
|
460
|
+
### 🐛 Bug Fixes
|
|
461
|
+
- Fix check for storing auth requests
|
|
462
|
+
- Don't raise an error on 3xx during revalidation
|
|
463
|
+
|
|
464
|
+
### 🚀 Features
|
|
465
|
+
- Add hishel_created_at response metadata
|
|
466
|
+
|
|
467
|
+
## 1.0.0.dev1 - 2025-10-21
|
|
468
|
+
### ⚙️ Miscellaneous Tasks
|
|
469
|
+
- Remove some redundant utils methods
|
|
470
|
+
|
|
471
|
+
### 📦 Dependencies
|
|
472
|
+
- Make httpx and async libs optional dependencies
|
|
473
|
+
- Make `anysqlite` optional dependency
|
|
474
|
+
- Install async extra with httpx
|
|
475
|
+
- Improve git-cliff
|
|
476
|
+
|
|
477
|
+
## 1.0.0.dev0 - 2025-10-19
|
|
478
|
+
### ⚙️ Miscellaneous Tasks
|
|
479
|
+
- Use mike powered versioning
|
|
480
|
+
- Improve docs versioning, deploy dev doc on ci
|
|
481
|
+
|
|
482
|
+
## 0.1.5 - 2025-10-18
|
|
483
|
+
### ⚙️ Miscellaneous Tasks
|
|
484
|
+
- Remove some redundant files from repo
|
|
485
|
+
|
|
486
|
+
### 🐛 Bug Fixes
|
|
487
|
+
- Fix some line breaks
|
|
488
|
+
|
|
489
|
+
### 🚀 Features
|
|
490
|
+
- Set chunk size to 128KB for httpx to reduce SQLite read/writes
|
|
491
|
+
- Better cache-control parsing
|
|
492
|
+
- Add close method to storages API (#384)
|
|
493
|
+
- Increase requests buffer size to 128KB, disable charset detection
|
|
494
|
+
|
|
495
|
+
## 0.1.4 - 2025-10-14
|
|
496
|
+
### ⚙️ Miscellaneous Tasks
|
|
497
|
+
- Improve CI (#369)
|
|
498
|
+
- Remove src folder (#373)
|
|
499
|
+
- Temporary remove python3.14 from CI
|
|
500
|
+
- Add sqlite tests for new storage
|
|
501
|
+
- Move some tests to beta
|
|
502
|
+
|
|
503
|
+
### 🐛 Bug Fixes
|
|
504
|
+
- Create an sqlite file in a cache folder
|
|
505
|
+
- Fix beta imports
|
|
506
|
+
|
|
507
|
+
### 🚀 Features
|
|
508
|
+
- Add support for a sans-IO API (#366)
|
|
509
|
+
- Allow already consumed streams with `CacheTransport` (#377)
|
|
510
|
+
- Add sqlite storage for beta storages
|
|
511
|
+
- Get rid of some locks from sqlite storage
|
|
512
|
+
- Better async implemetation for sqlite storage
|
|
513
|
+
|