A cache provider for CacheControl using Django's caching mechanism.
pip install cachecontrol-django
import requests
from cachecontrol import CacheControl
from cachecontrol_django import DjangoCache
session = CacheControl(requests.session(), cache=DjangoCache())
session.get("https://www.glassesdirect.co.uk/")
If you see errors about key length (such as "MemcachedKeyLengthError: Key length is > 250"), you can ask DjangoCache to hash the keys it uses by specifying a key_hash_algorithm
:
long_url = 'https://www.glassesdirect.co.uk/?q=' + ('x' * 250)
session = CacheControl(
requests.session(),
cache=DjangoCache(key_hash_algorithm='sha512')
)
session.get(long_url)
Note that when hashing these keys, there is a very small chance of a hash collision causing a request for one URL to return the content of another.