aiohttp-client-cache icon indicating copy to clipboard operation
aiohttp-client-cache copied to clipboard

Feature request: Share cache with requests-cache

Open franz101 opened this issue 2 years ago • 1 comments

Feature description

Use case

Use the same SQLite backend

Workarounds

Have the same key generation

Plan to implement

Maybe

from aiohttp_client_cache import CachedSession, SQLiteBackend
import requests_cache
cache = "drive/MyDrive/xyz.sqlite"
url = 'https://example.com'

requests_cache.install_cache(cache)
requests.get(url)

async def main():
  global url
  async with CachedSession(cache=SQLiteBackend(cache)) as session:  
    r = await session.get(url)
    print(r.from_cache)
await main()

This evaluates to False, but should be True

franz101 avatar Jul 11 '23 16:07 franz101

I agree that this would be nice to have. Unfortunately this would require quite a bit more work than just changing the cache keys. Problems include:

  1. requests-cache uses a different serialization format using the cattrs library
  2. aiohttp.ClientResponse response objects look quite different from requests.Response objects
  3. Objects serialized with pickle are tied to a specific class, and will throw an error if the original serialized class isn't available

A possible solution might look something like:

  • Update this library to use cattrs (to "unstructure" a response object into primitive types before pickling)
  • Make a new response class compatible with both aiohttp and requests
    • And/or: Update both requests-cache and aiohttp-client cache to handle missing attributes unique to each HTTP library
  • Add all (or most) of requests-cache's request matching behavior to this library so cache keys will be the same

There could be a simpler solution, but I can't think of one right now.

JWCook avatar Jul 11 '23 21:07 JWCook