blob: d19368086297b9bec7a02e168abcf68116b1bd0d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
(examples)=
# {fas}`laptop-code` Examples
This section contains some complete examples that demonstrate the main features of requests-cache.
## Articles
Some articles and blog posts that discuss requests-cache:
* PyBites: [Module of the Week: requests-cache for repeated API calls](https://pybit.es/articles/requests-cache/)
* Real Python: [Caching External API Requests](https://realpython.com/blog/python/caching-external-api-requests)
* Thomas Gorham: [Faster Backtesting with requests-cache](https://www.mntn.dev/blog/requests-cache)
* Tim O'Hearn: [Pragmatic Usage of requests-cache](https://www.tjohearn.com/2018/02/12/pragmatic-usage-of-requests-cache/)
* Valdir Stumm Jr: [Tips for boosting your Python scripts](https://stummjr.org/post/building-scripts-in-python/)
* Python Web Scraping (2nd Edition): [Exploring requests-cache](https://learning.oreilly.com/library/view/python-web-scraping/9781786462589/3fad0dcc-445b-49a4-8d5e-ba5e1ff8e3bb.xhtml)
* Cui Qingcai: [一个神器,大幅提升爬取效率](https://cuiqingcai.com/36052.html) (A package that greatly improves crawling efficiency)
<!--
Explicit line numbers are added below to include the module docstring in the main doc, and put the
rest of the module contents in a dropdown box.
TODO: It might be nice to have a custom extension to do this automatically.
-->
## Scripts
The following scripts can also be found in the
[examples/](https://github.com/requests-cache/requests-cache/tree/main/examples) folder on GitHub.
### Basic usage (with sessions)
```{include} ../examples/basic_sessions.py
:start-line: 3
:end-line: 4
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[basic_sessions.py](https://github.com/requests-cache/requests-cache/blob/main/examples/basic_sessions.py)
```{literalinclude} ../examples/basic_sessions.py
:lines: 6-
```
:::
### Basic usage (with patching)
```{include} ../examples/basic_patching.py
:start-line: 3
:end-line: 4
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[basic_patching.py](https://github.com/requests-cache/requests-cache/blob/main/examples/basic_patching.py)
```{literalinclude} ../examples/basic_patching.py
:lines: 6-
```
:::
### Cache expiration
```{include} ../examples/expiration.py
:start-line: 2
:end-line: 3
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[expiration.py](https://github.com/requests-cache/requests-cache/blob/main/examples/expiration.py)
```{literalinclude} ../examples/expiration.py
:lines: 5-
```
:::
### URL patterns
```{include} ../examples/url_patterns.py
:start-line: 3
:end-line: 4
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[url_patterns.py](https://github.com/requests-cache/requests-cache/blob/main/examples/url_patterns.py)
```{literalinclude} ../examples/url_patterns.py
:lines: 6-
```
:::
### PyGithub
```{include} ../examples/pygithub.py
:start-line: 2
:end-line: 25
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[pygithub.py](https://github.com/requests-cache/requests-cache/blob/main/examples/pygithub.py)
```{literalinclude} ../examples/pygithub.py
:lines: 27-
```
:::
### Multi-threaded requests
```{include} ../examples/threads.py
:start-line: 2
:end-line: 4
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[threads.py](https://github.com/requests-cache/requests-cache/blob/main/examples/threads.py)
```{literalinclude} ../examples/threads.py
:lines: 6-
```
:::
### Logging requests
```{include} ../examples/log_requests.py
:start-line: 2
:end-line: 3
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[log_requests.py](https://github.com/requests-cache/requests-cache/blob/main/examples/log_requests.py)
```{literalinclude} ../examples/log_requests.py
:lines: 5-
```
:::
### External configuration
```{include} ../examples/external_config.py
:start-line: 2
:end-line: 8
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[external_config.py](https://github.com/requests-cache/requests-cache/blob/main/examples/external_config.py)
```{literalinclude} ../examples/external_config.py
:lines: 10-
```
:::
### Cache speed test
```{include} ../examples/benchmark.py
:start-line: 2
:end-line: 8
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[benchmark.py](https://github.com/requests-cache/requests-cache/blob/main/examples/benchmark.py)
```{literalinclude} ../examples/benchmark.py
:lines: 10-
```
:::
### Requests per second graph
```{include} ../examples/rps_graph.py
:start-line: 2
:end-line: 7
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[benchmark.py](https://github.com/requests-cache/requests-cache/blob/main/examples/rps_graph.py)
```{literalinclude} ../examples/rps_graph.py
:lines: 9-
```
:::
:::{dropdown} Screenshot
:animate: fade-in-slide-down
:color: info
:icon: image
![](../examples/rps_graph.png)
:::
### Using with GitHub Actions
This example shows how to use requests-cache with [GitHub Actions](https://docs.github.com/en/actions).
Key points:
* Create the cache file within the CI project directory
* You can use [actions/cache](https://github.com/actions/cache) to persist the cache file across
workflow runs
* You can use a constant cache key within this action to let requests-cache handle expiration
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[github_actions.yml](https://github.com/requests-cache/requests-cache/blob/main/examples/github_actions.yml)
```{literalinclude} ../examples/github_actions.yml
```
:::
### Converting an old cache
```{include} ../examples/convert_cache.py
:start-line: 2
:end-line: 4
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[convert_cache.py](https://github.com/requests-cache/requests-cache/blob/main/examples/convert_cache.py)
```{literalinclude} ../examples/convert_cache.py
:lines: 6-
```
:::
(custom_keys)=
### Custom request matcher
```{include} ../examples/custom_request_matcher.py
:start-line: 2
:end-line: 15
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[custom_request_matcher.py](https://github.com/requests-cache/requests-cache/blob/main/examples/custom_request_matcher.py)
```{literalinclude} ../examples/custom_request_matcher.py
:lines: 17-
```
:::
### Backtesting with time-machine
```{include} ../examples/time_machine_backtesting.py
:start-line: 2
:end-line: 4
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[time_machine_backtesting.py](https://github.com/requests-cache/requests-cache/blob/main/examples/time_machine_backtesting.py)
```{literalinclude} ../examples/time_machine_backtesting.py
:lines: 6-
```
:::
### VCR Export
```{include} ../examples/vcr.py
:start-line: 2
:end-line: 5
```
:::{dropdown} Example
:animate: fade-in-slide-down
:color: primary
:icon: file-code
[vcr.py](https://github.com/requests-cache/requests-cache/blob/main/examples/vcr.py)
```{literalinclude} ../examples/vcr.py
:lines: 7-
```
:::
|