| 1 |
import tornado.web |
| 2 |
import tornado.ioloop |
| 3 |
from datetime import datetime |
| 4 |
from aiocache import cached |
| 5 |
from aiocache.serializers import JsonSerializer |
| 6 |
|
| 7 |
|
| 8 |
class MainHandler(tornado.web.RequestHandler): |
| 9 |
|
| 10 |
# Due some incompatibilities between tornado and asyncio, caches can't use the "timeout" feature |
| 11 |
# in order to make it work, you will have to specify it always to 0 |
| 12 |
@cached(key="my_custom_key", serializer=JsonSerializer(), timeout=0) |
| 13 |
async def time(self): |
| 14 |
return {"time": datetime.now().isoformat()} |
| 15 |
|
| 16 |
async def get(self): |
| 17 |
self.write(await self.time()) |
| 18 |
|
| 19 |
|
| 20 |
if __name__ == "__main__": |
| 21 |
tornado.ioloop.IOLoop.configure('tornado.platform.asyncio.AsyncIOLoop') |
| 22 |
app = tornado.web.Application([(r"/", MainHandler)]) |
| 23 |
app.listen(8888) |
| 24 |
tornado.ioloop.IOLoop.current().start() |