{"id":1071,"library":"requests-cache","title":"Requests Cache","description":"Requests-cache is a persistent HTTP cache that provides an easy way to get better performance with the popular Python `requests` library. It's particularly useful for web scraping, consuming REST APIs, or dealing with slow or rate-limited sites. It supports various storage backends like SQLite, Redis, MongoDB, DynamoDB, and the filesystem, offering flexible expiration strategies. The current version is 1.3.1, and it maintains an active release cadence.","status":"active","version":"1.3.1","language":"python","source_language":"en","source_url":"https://github.com/requests-cache/requests-cache","tags":["http","cache","requests","performance","web-scraping","api","networking"],"install":[{"cmd":"pip install requests-cache","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for HTTP requests functionality.","package":"requests"},{"reason":"Required for optimized serialization.","package":"cattrs","optional":false},{"reason":"Used for cross-platform user cache directory management.","package":"appdirs","optional":false},{"reason":"Optional but recommended for more secure serialization.","package":"itsdangerous","optional":true}],"imports":[{"note":"Official documentation recommends importing directly from the top-level `requests_cache` package.","wrong":"from requests_cache.core import CachedSession","symbol":"CachedSession","correct":"from requests_cache import CachedSession"},{"symbol":"install_cache","correct":"from requests_cache import install_cache"}],"quickstart":{"code":"import requests_cache\nimport requests\n\n# Use CachedSession as a drop-in replacement for requests.Session\nsession = requests_cache.CachedSession('demo_cache', expire_after=3600)\n\n# Make a request; it will be cached\nresponse1 = session.get('https://httpbin.org/delay/1')\nprint(f\"First request (from cache: {response1.from_cache}): {response1.status_code}\")\n\n# Make the same request again; it will be loaded from cache instantly\nresponse2 = session.get('https://httpbin.org/delay/1')\nprint(f\"Second request (from cache: {response2.from_cache}): {response2.status_code}\")\n\n# Example for global patching (less recommended for complex apps)\n# requests_cache.install_cache('global_cache', expire_after=3600)\n# response3 = requests.get('https://httpbin.org/delay/1')\n# print(f\"Third request (from cache: {getattr(response3, 'from_cache', False)}): {response3.status_code}\")\n\n# Clear the cache\nsession.cache.clear()\nprint(\"Cache cleared.\")","lang":"python","description":"The most common and recommended way to use `requests-cache` is by creating a `CachedSession` instance, which acts as a direct replacement for `requests.Session`. This example demonstrates making a request, observing it being cached, and then retrieving it from the cache instantly on a subsequent call. It also shows how to clear the cache."},"warnings":[{"fix":"Access and modify cache settings exclusively through `session.settings` attribute, e.g., `session.settings.expire_after = 7200`.","message":"Prior to version 1.0, cache settings could sometimes be modified directly on `CachedSession` or `BaseCache`. As of 1.0, settings can only be accessed and modified via `CachedSession.settings` after initialization.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Create a new DynamoDB table when upgrading if you are using the DynamoDB backend. Refer to the DynamoDB backend documentation for details.","message":"For users of the DynamoDB backend, the table structure changed in version 1.0. Upgrading to 1.0 requires creating a new DynamoDB table, as existing tables are incompatible.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"No direct fix needed; old entries will be re-fetched. Users can manually clear the cache (`session.cache.clear()`) or convert old cache formats if supported by specific minor versions (check historical changelogs).","message":"Responses cached with `requests-cache` versions prior to 0.6.0 are invalid due to serialization format changes. These old responses will be treated as expired and automatically re-fetched.","severity":"breaking","affected_versions":"0.6.0 to current"},{"fix":"Prefer `requests_cache.CachedSession` over `requests_cache.install_cache()` for explicit control over caching behavior.","message":"The global patching method (`requests_cache.install_cache()`) has limitations and can lead to unexpected behavior, especially in multi-threaded/multiprocess applications, when used with other `requests`-patching libraries, or in larger applications where cache behavior might not be obvious across modules. Using `CachedSession` is generally recommended.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `requests_cache.CachedSession` or `requests_cache.install_cache()` for HTTP response caching. Explicitly manage cache entries using `session.cache.clear()` or `expire_after` settings for desired data freshness.","message":"The core `requests` library does not natively cache HTTP responses. Users expecting caching behavior without `requests-cache` (or similar) will not get it. When using `requests-cache`, not explicitly calling `clear()` or misconfiguring `expire_after` are common mistakes for managing data freshness.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T23:32:24.621Z","next_check":"2026-06-30T00:00:00.000Z","problems":[{"fix":"pip install requests-cache","cause":"The 'requests-cache' package has not been installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'requests_cache'"},{"fix":"import requests_cache; session = requests_cache.CachedSession('demo_cache'); response = session.get('https://httpbin.org/get')","cause":"The `install_cache()` function was deprecated and removed in requests-cache versions 0.6.0 and later, favoring the `CachedSession` API.","error":"AttributeError: module 'requests_cache' has no attribute 'install_cache'"},{"fix":"pip install msgpack","cause":"The `msgpack` library, an optional dependency required for the `fast_pickle` serializer (used for optimal SQLite caching), is not installed.","error":"ModuleNotFoundError: No module named 'msgpack'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.3.2","cli_name":"","cli_version":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","installed_version":"1.3.2","pypi_latest":"1.3.2","is_stale":false,"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.74,"mem_mb":13.7,"disk_size":"23.8M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.8,"mem_mb":13.6,"disk_size":"23.7M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.5,"import_time_s":0.52,"mem_mb":13.7,"disk_size":"24M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.56,"mem_mb":13.6,"disk_size":"24M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.96,"mem_mb":15,"disk_size":"26.2M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":1.08,"mem_mb":14.8,"disk_size":"26.0M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.5,"import_time_s":0.83,"mem_mb":15,"disk_size":"27M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.83,"mem_mb":14.8,"disk_size":"27M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.81,"mem_mb":14.6,"disk_size":"17.8M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.91,"mem_mb":14.5,"disk_size":"17.7M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.3,"import_time_s":0.84,"mem_mb":14.6,"disk_size":"18M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.88,"mem_mb":14.5,"disk_size":"18M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.8,"mem_mb":15.4,"disk_size":"17.6M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.9,"mem_mb":15.3,"disk_size":"17.4M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.3,"import_time_s":0.81,"mem_mb":15.4,"disk_size":"18M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.89,"mem_mb":15.3,"disk_size":"18M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.69,"mem_mb":13.3,"disk_size":"23.0M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.74,"mem_mb":13.2,"disk_size":"23.0M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.9,"import_time_s":0.61,"mem_mb":13.3,"disk_size":"24M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"requests-cache","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.67,"mem_mb":13.2,"disk_size":"23M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":null,"tag_description":null,"results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}