File: memcached_storage.py

package info (click to toggle)
python-aiohttp-session 2.12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 496 kB
  • sloc: python: 2,534; makefile: 197
file content (29 lines) | stat: -rw-r--r-- 771 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import asyncio
import time

import aiomcache
from aiohttp import web

from aiohttp_session import get_session, setup
from aiohttp_session.memcached_storage import MemcachedStorage


async def handler(request: web.Request) -> web.Response:
    session = await get_session(request)
    last_visit = session["last_visit"] if "last_visit" in session else None
    session["last_visit"] = time.time()
    text = f"Last visited: {last_visit}"
    return web.Response(text=text)


async def make_app() -> web.Application:
    app = web.Application()
    mc = aiomcache.Client("127.0.0.1", 11211)
    setup(app, MemcachedStorage(mc))
    app.router.add_get("/", handler)
    return app


loop = asyncio.get_event_loop()
app = loop.run_until_complete(make_app())
web.run_app(app)