diff options
| author | Jordan Cook <jordan.cook@pioneer.com> | 2021-08-29 17:55:33 -0500 |
|---|---|---|
| committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-08-29 20:29:16 -0500 |
| commit | f707ddeeee5c29881c0de96092a01e78a905a401 (patch) | |
| tree | d323297b41cef9a895b27c3999ae70d69c5d6df7 /examples/basic_sessions.py | |
| parent | 465f94d72dbb9e3ef557c5a526accd433e3ebc42 (diff) | |
| download | requests-cache-f707ddeeee5c29881c0de96092a01e78a905a401.tar.gz | |
Some formatting for examples
Diffstat (limited to 'examples/basic_sessions.py')
| -rwxr-xr-x | examples/basic_sessions.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/basic_sessions.py b/examples/basic_sessions.py new file mode 100755 index 0000000..42a6dd2 --- /dev/null +++ b/examples/basic_sessions.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# flake8: noqa: F841 +""" +A simple example using requests-cache with [httpbin](https://httpbin.org) +""" +import time + +from requests_cache import CachedSession + + +def main(): + session = CachedSession('example_cache', backend='sqlite') + + # The real request will only be made once; afterward, the cached response is used + for i in range(5): + response = session.get('http://httpbin.org/get') + + # This is more obvious when calling a slow endpoint + for i in range(5): + response = session.get('http://httpbin.org/delay/2') + + # Caching can be disabled if we want to get a fresh page and not cache it + with session.cache_disabled(): + print(session.get('http://httpbin.org/ip').text) + + # Get some debugging info about the cache + print(session.cache) + print('Cached URLS:', list(session.cache.urls)) + + +if __name__ == "__main__": + t = time.time() + main() + print('Elapsed: %.3f seconds' % (time.time() - t)) |
