Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add install_cache() option to limit caching to current module #616

Open
JWCook opened this issue Apr 24, 2022 · 0 comments
Open

Add install_cache() option to limit caching to current module #616

JWCook opened this issue Apr 24, 2022 · 0 comments

Comments

@JWCook
Copy link
Member

JWCook commented Apr 24, 2022

This is an idea to partly address one of the problems with install_cache(): accidentally caching things you didn't intend to. This could potentially be more convenient than using requests_cache.enabled() for cases where you just want to cache all requests made from a single module.

It would look something like:

# package/module_1.py
from requests import get
from requests_cache import install_cache

install_cache(module_only=True)
response = get('https://url.com') # cached
# package/module_2.py
from requests import get

response = get('https://url.com') # not cached

Using CachedSession directly is still the better option, but this would be a nice safety measure for users who prefer the convenience of install_cache().

This should be doable with inspection. It would work slightly differently than install_cache() currently does, by patching requests.api.request() to use a CachedSession if the calling context is from the same module that patched it, or a regular requests.Session otherwise. This would apply only to the static wrapper functions (requests.get(), etc.), not Session usage.

This could potentially be done from multiple modules at once, by adding and updating an attribute containing a list of allowed modules. uninstall_cache() would also need to take into account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant