Created
November 6, 2017 10:55
-
-
Save ansrivas/1db1145528795a223ae8999561511175 to your computer and use it in GitHub Desktop.
in memory caching with a ttl using aiohttp and aiocache
This file contains hidden or 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
| from datetime import datetime | |
| from aiohttp import web | |
| from aiocache import cached | |
| from aiocache.serializers import JsonSerializer | |
| @cached(key="my_custom_key", serializer=JsonSerializer(), ttl=5) | |
| async def time(): | |
| return {"time": datetime.now().isoformat()} | |
| async def handle(request): | |
| return web.json_response(await time()) | |
| if __name__ == "__main__": | |
| app = web.Application() | |
| app.router.add_get('/', handle) | |
| web.run_app(app, port=8081) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment