Skip to content

Instantly share code, notes, and snippets.

@ansrivas
Created November 6, 2017 10:55
Show Gist options
  • Select an option

  • Save ansrivas/1db1145528795a223ae8999561511175 to your computer and use it in GitHub Desktop.

Select an option

Save ansrivas/1db1145528795a223ae8999561511175 to your computer and use it in GitHub Desktop.
in memory caching with a ttl using aiohttp and aiocache
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