python requests cache to file

Python Requests Cache to File

If you are working with a website that requires a lot of HTTP requests, you may want to consider caching the responses to avoid making the same requests repeatedly. Python Requests library offers a built-in cache system that stores API responses in a file for later use.

Using Requests Cache

To use the Requests cache feature, you need to enable it by adding a cache parameter to your request. Here is an example:

import requests

# Enable cache
s = requests.Session()
s.cache = requests.cache.FileCache('my_cache_folder')

# Make an API request
response = s.get('')

In this example, we created a Session object and set its cache parameter to FileCache with the folder path where we want to store the cache files. Then, we used the Session object to send a GET request to an API endpoint.

Caching Options

Requests cache has several options that you can customize:

  • expire_after: sets the expiration time for cached responses in seconds (default: None, which means no expiration).
  • allowable_methods: list of HTTP methods that are allowed to be cached (default: ['GET', 'OPTIONS', 'HEAD']).
  • allowable_codes: list of HTTP status codes that are allowed to be cached (default: [200, 203, 300, 301, 302, 404, 405, 410, 414]).
  • backend: specifies the cache backend implementation (default: 'sqlite'). Other options are 'memory' and 'redis'.

Here's an example of how to use these options:

import requests

# Enable cache with options
s = requests.Session()
s.cache = requests.cache.FileCache('my_cache_folder', 
                                    allowable_methods=['GET', 'POST'], 
                                    allowable_codes=[200, 401],

# Make an API request
response = s.get('')

In this example, we set the cache expiration time to one hour (3600 seconds), allowed GET and POST methods to be cached, allowed only 200 and 401 status codes to be cached, and used Redis as the cache backend.

Manual Cache Control

If you want more control over the cache, you can use the cache_control parameter in your request to specify the cache behavior. Here are some examples:

# Disable cache for a request
response = s.get('', 
                headers={'cache-control': 'no-cache'})

# Force cache revalidation for a request
response = s.get('', 
                headers={'cache-control': 'max-age=0'})

In the first example, we disabled the cache for a specific request by setting the cache-control header to no-cache. In the second example, we forced the cache to revalidate by setting the max-age value to 0.


The Requests cache feature is a convenient way to improve the performance of your Python scripts that make HTTP requests to APIs. You can customize the cache behavior to suit your needs and avoid making unnecessary requests.