By default only autonomous methods are cached (get, head, options). Each method can be setup to be cached using the Session.cache config option.

These methods are accessed through the Session objects Session.cache.[method name].
They can be overridden with the Session.cache.all setting.

For example

fromcache_requestsimportSessionrequests=Session()requests.cache.delete=True# cached, only called once.requests.delete('http://google.com')requests.delete('http://google.com')requests.cache.delete=True# not cached, called twice.requests.delete('http://google.com')requests.delete('http://google.com')# cache ALL methodsrequests.cache.all=True# don't cache any methodsrequests.cache.all=False# Use individual method cache options.requests.cache.all=None

Default settings

Method

Cached

get

True

head

True

options

True

post

False

put

False

patch

False

delete

False

all

None

Function Level Config

Cache Busting

Use keyword bust_cache=True in a memoized function to force reevaluation.

Conditionally Set Cache

Use keyword set_cache to provide a callback. The callback takes the results of function as an argument and must return a bool. Alternatively, True and False can be used.

Use Case Scenarios

Development: 3rd Party APIs

Scenario:

Working on a project that uses a 3rd party API or service.

Things you want:

A cache that persists between sessions and is lightning fast.

Ability to rapidly explore the API and it’s parameters.

Ability to inspect and debug response content.

Ability to focus on progress.

Perfect transition to a production environment.

Things you don’t want:

Dependency on network and server stability for development.

Spamming the API. Especially APIs with limits.

Responses that change in non-meaningful ways.

Burning energy with copypasta or fake data to run piece of your program.

Slow. Responses.

Make a request one time. Cache the results for the rest of your work session.