(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:

Scripts

The following scripts can also be found in the examples/ folder on GitHub.

Basic usage (with sessions)

:start-line: 3
:end-line: 4

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

basic_sessions.py

:lines: 6-

:::

Basic usage (with patching)

:start-line: 3
:end-line: 4

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

basic_patching.py

:lines: 6-

:::

Cache expiration

:start-line: 2
:end-line: 3

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

expiration.py

:lines: 5-

:::

URL patterns

:start-line: 3
:end-line: 4

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

url_patterns.py

:lines: 6-

:::

PyGithub

:start-line: 2
:end-line: 25

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

pygithub.py

:lines: 27-

:::

Multi-threaded requests

:start-line: 2
:end-line: 4

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

threads.py

:lines: 6-

:::

Logging requests

:start-line: 2
:end-line: 3

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

log_requests.py

:lines: 5-

:::

External configuration

:start-line: 2
:end-line: 8

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

external_config.py

:lines: 10-

:::

Cache speed test

:start-line: 2
:end-line: 8

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

benchmark.py

:lines: 10-

:::

Requests per second graph

:start-line: 2
:end-line: 7

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

rps_graph.py

:lines: 9-

:::

:::{dropdown} Screenshot :animate: fade-in-slide-down :color: info :icon: image

:::

Using with GitHub Actions

This example shows how to use requests-cache with GitHub Actions. Key points:

  • Create the cache file within the CI project directory
  • You can use 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

:::

Converting an old cache

:start-line: 2
:end-line: 4

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

convert_cache.py

:lines: 6-

:::

(custom_keys)=

Custom request matcher

:start-line: 2
:end-line: 15

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

custom_request_matcher.py

:lines: 17-

:::

Backtesting with time-machine

:start-line: 2
:end-line: 4

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

time_machine_backtesting.py

:lines: 6-

:::

VCR Export

:start-line: 2
:end-line: 5

:::{dropdown} Example :animate: fade-in-slide-down :color: primary :icon: file-code

vcr.py

:lines: 7-

:::