Skip to main content

Persistent cache for aiohttp requests

Project description

aiohttp-client-cache

See full documentation at https://aiohttp-client-cache.readthedocs.io

Build status Documentation Status PyPI PyPI - Python Versions PyPI - Format

aiohttp-client-cache is an async persistent cache for aiohttp requests, based on requests-cache.

Not to be confused with aiohttp-cache, which is a cache for the aiohttp web server. This package is, as you might guess, specifically for the aiohttp client.

Development Status

This is an early work in progress and not yet fully functional!

The current state is a mostly working drop-in replacement for aiohttp.ClientSession. However, the cache operations are still synchronous, with minimal testing and likely lots of bugs.

Installation

Requires python 3.7+

Install the latest stable version with pip:

pip install aiohttp-client-cache

To set up for local development:

$ git clone https://github.com/JWCook/aiohttp-client-cache
$ cd aiohttp-client-cache
$ pip install -Ue ".[dev]"
$ # Optional but recommended:
$ pre-commit install --config .github/pre-commit.yml

Usage example

from aiohttp_client_cache import CachedSession
session = CachedSession('demo_cache', backend='sqlite')
response = await session.get('http://httpbin.org/get')

Afterward, all responses with headers and cookies will be transparently cached to a database named demo_cache.sqlite. For example, following code will take only 1-2 seconds instead of 10, and will run instantly on next launch:

for i in range(10):
    await session.get('http://httpbin.org/delay/1')

Cache Backends

Several backends are available. The default backend is sqlite, if installed; otherwise it falls back to memory.

  • sqlite : SQLite database (requires aiosqlite)
  • redis : Stores all data in a redis cache (requires redis-py)
  • mongodb : MongoDB database (requires pymongo)
    • gridfs : MongoDB GridFS enables storage of documents greater than 16MB (requires pymongo)
  • memory : Not persistent, simply stores all data in memory

You can also provide your own backend by subclassing aiohttp_client_cache.backends.BaseCache.

Expiration

If you are using the expire_after parameter , responses are removed from the storage the next time the same request is made. If you want to manually purge all expired items, you can use CachedSession.delete_expired_responses. Example:

session = CachedSession(expire_after=1)
await session.remove_expired_responses()

Credits

Thanks to Roman Haritonov and contributors for the original requests-cache!

This project is licensed under the MIT license, with the exception of portions reused from requests-cache (primarily its storage backends), which is licensed under the BSD license (copy included).

History

0.1.0 (2020-11-14)

  • Initial PyPI release
  • First pass at a general refactor and conversion from requests to aiohttp
  • Basic features are functional, but some backends do not actually operate asynchronously

requests-cache

See requests-cache development history for details on prior changes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-client-cache-0.1.2.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

aiohttp_client_cache-0.1.2-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-client-cache-0.1.2.tar.gz.

File metadata

  • Download URL: aiohttp-client-cache-0.1.2.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6

File hashes

Hashes for aiohttp-client-cache-0.1.2.tar.gz
Algorithm Hash digest
SHA256 688513b8defa2deda441c56df45cecb2177bded78f9384c9b7594f2991681b75
MD5 a09313e31a1d1b4d1f2c70883d8b5dfd
BLAKE2b-256 7df958c3a31077a0330f41ac42717091640604874b6e8699819b5043ba98f3c8

See more details on using hashes here.

File details

Details for the file aiohttp_client_cache-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_client_cache-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6

File hashes

Hashes for aiohttp_client_cache-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d778984f87d670f794dfde9113d1762079828663ad03dc2b0c6792cb9373099e
MD5 704e3b65ee329de8fcebc8cd4a87b609
BLAKE2b-256 4d6cfc1650f77fd0de0997ae76a6dddf82bd1dbc4bef4eafed65ee2cd33ee046

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page