Bogdanp / molten

A minimal, extensible, fast and productive framework for building HTTP APIs with Python 3.6 and later.

Home Page:https://moltenframework.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docs for session storage

cmclaughlin opened this issue · comments

Awesome work... just getting started with this framework and I'm very impressed.

I see the dump method in the SessionStore class, but it's not obvious to me how to use it. Would appreciate some documentation or examples on this, i.e. how to persist sessions to Redis, Postgres, etc.

Thank you

Thanks! I'll add some docs around that later this week. In the mean time, here's a basic (untested!) Redis session store implementation (it assumes you have some sort of Redis component in the system):

class RedisSessionStore:
    def load(self, cookies: Cookies, redis: Redis) -> Session:
        session_id = cookies.get("session-id")
        if not session_id:
            return Session.empty()

        session_data = redis.hgetall(session_id)
        if not session_data:
            return Session.empty()

        return Session(**session_data)

    def dump(self, session: Session, redis: Redis) -> Cookie:
        redis.hmset(session.id, session)
        return Cookie("session-id", session.id)

Basically, load and dump can each request components via DI and that allows them to store and retrieve session data from anywhere.