Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include example scripts on readthedocs, and move some more details fr…
…om Readme to readthedocs
- Loading branch information
Showing
11 changed files
with
167 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* Taken from: https://github.com/plone/training/blob/master/_static/custom.css */ | ||
|
||
.toggle { | ||
background: none repeat scroll 0 0 #e7f2fa; | ||
padding: 12px; | ||
line-height: 24px; | ||
margin-bottom: 24px; | ||
} | ||
|
||
.toggle .admonition-title { | ||
display: block; | ||
clear: both; | ||
cursor: pointer; | ||
} | ||
|
||
.toggle .admonition-title:after { | ||
content: " ▶"; | ||
} | ||
|
||
.toggle .admonition-title.open:after { | ||
content: " ▼"; | ||
} | ||
|
||
.toggle p:last-child { | ||
margin-bottom: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{# Taken from: https://github.com/plone/training/blob/master/_templates/page.html #} | ||
{% extends "!page.html" %} | ||
|
||
{% block footer %} | ||
<script type="text/javascript"> | ||
$(document).ready(function() { | ||
$(".toggle > *").hide(); | ||
$(".toggle .admonition-title").show(); | ||
$(".toggle .admonition-title").click(function() { | ||
$(this).parent().children().not(".admonition-title").toggle(400); | ||
$(this).parent().children(".admonition-title").toggleClass("open"); | ||
}) | ||
}); | ||
</script> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Examples | ||
-------- | ||
Following are some complete examples to demonstrate some of the features of aiohttp-client-cache. | ||
These can also be found in the | ||
`examples/ <https://github.com/JWCook/aiohttp-client-cache/tree/main/examples>`_ folder on GitHub. | ||
|
||
Expiration based on URL patterns | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
.. include:: ../examples/url_patterns.py | ||
:start-line: 3 | ||
:end-line: 4 | ||
|
||
.. admonition:: Example code | ||
:class: toggle | ||
|
||
.. literalinclude:: ../examples/url_patterns.py | ||
:lines: 1,5- | ||
|
||
Precaching site links | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
.. include:: ../examples/precache.py | ||
:start-line: 2 | ||
:end-line: 18 | ||
|
||
.. admonition:: Example code | ||
:class: toggle | ||
|
||
.. literalinclude:: ../examples/precache.py | ||
:lines: 1,20- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Related Projects | ||
---------------- | ||
Other python cache projects you may want to check out: | ||
|
||
* `aiohttp-cache <https://github.com/cr0hn/aiohttp-cache>`_: A server-side async HTTP cache for the | ||
``aiohttp`` web server | ||
* `diskcache <https://github.com/grantjenks/python-diskcache>`_: A general-purpose (not HTTP-specific) | ||
file-based cache built on SQLite | ||
* `aiocache <https://github.com/aio-libs/aiocache>`_: General-purpose (not HTTP-specific) async cache | ||
backends | ||
* `requests-cache <https://github.com/reclosedev/requests-cache`_: An HTTP cache for the ``requests`` library | ||
* `CacheControl <https://github.com/ionrock/cachecontrol>`_: An HTTP cache for ``requests`` that caches | ||
according to uses HTTP headers and status codes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python3 | ||
# fmt: off | ||
""" | ||
An example of setting expiration based on :ref:`user_guide:url patterns` | ||
""" | ||
import asyncio | ||
from datetime import timedelta | ||
|
||
from aiohttp_client_cache import CachedSession, SQLiteBackend | ||
|
||
default_expire_after = 60 * 60 # By default, cached responses expire in an hour | ||
urls_expire_after = { | ||
'httpbin.org/image': timedelta(days=7), # Requests for this base URL will expire in a week | ||
'*.fillmurray.com': -1, # Requests matching this pattern will never expire | ||
} | ||
urls = [ | ||
'https://httpbin.org/get', # Will expire in an hour | ||
'https://httpbin.org/image/jpeg', # Will expire in a week | ||
'http://www.fillmurray.com/460/300', # Will never expire | ||
] | ||
|
||
|
||
async def main(): | ||
cache = SQLiteBackend( | ||
cache_name='~/.cache/aiohttp-requests.db', | ||
expire_after=default_expire_after, | ||
urls_expire_after=urls_expire_after, | ||
) | ||
|
||
async with CachedSession(cache=cache) as session: | ||
tasks = [asyncio.create_task(session.get(url)) for url in urls] | ||
return await asyncio.gather(*tasks) | ||
|
||
|
||
if __name__ == "__main__": | ||
original_responses = asyncio.run(main()) | ||
cached_responses = asyncio.run(main()) | ||
for response in cached_responses: | ||
expires = response.expires.isoformat() if response.expires else 'Never' | ||
print(f'{response.url}: {expires}') |