dream icon indicating copy to clipboard operation
dream copied to clipboard

Redis-likes for Session Storage?

Open jaidetree opened this issue 1 year ago • 9 comments

This framework looks very impressive! I noticed in the docs the session backends include memory, cookies, or sql. Was curious if you'd be open to a PR that extends a redis (or redis-like) session store?

jaidetree avatar Dec 09 '24 21:12 jaidetree

Could it be written as a library, which we could link to from the Dream repo/sites? The reason I am asking is because I am concerned about picking up a dependency, or else an awkward API with extra callbacks to bind it to a dependency dynamically. If there is any obstacle to writing a redis session backend as a library, I think we should fix that and I would be very open to a PR to that end.

aantron avatar Dec 10 '24 11:12 aantron

I see, the memory, cookie, and db backends reuse resources already included in the framework whereas this would require at least a redis client library which may not get used by anything else. That said, it can be useful for async worker queues but understandably not a high priority.

A separate library with a link in the docs works for me as a staring point.

I'll start drafting that library. My plan was to match the other storage module APIs and use a popular\common redis client library so not expecting much chaos there, but will see how it goes.

If I do run into any barriers from the framework design I'll create issues to make sure it's something that needs to be addressed and to ensure we're on the same page for how to solve it.

jaidetree avatar Dec 10 '24 19:12 jaidetree

Great, looking forward to that!

aantron avatar Dec 11 '24 19:12 aantron

If there is any obstacle to writing a redis session backend as a library

Correct me if I'm wrong, but I don't see any way to write a new session middleware in such a way that it can be used by the session accessor functions like session_field, set_session_field etc. Of course it's possible to just make new accessor functions that talk to Redis for example, but it would be less flexible because we wouldn't be able to just swap out session backends and keep using the same accessors...

yawaramin avatar Aug 09 '25 19:08 yawaramin

On the contrary they are written in such a way as to let you plug in another back end, like Redis.

aantron avatar Aug 09 '25 22:08 aantron

Sorry I meant as a user, without having to add the new backend in Dream.

yawaramin avatar Aug 09 '25 22:08 yawaramin

Not at the moment. An early version of Dream, when I was still developing it, before publishing, had the hooks for users to bind their own backends exposed, but they ended up being pretty messy, and I wasn't confident enough in the design to leave them in the API.

aantron avatar Aug 10 '25 09:08 aantron

This should actually be simplified by transitioning to effects, as one of the issues was that I wanted to have as few of the operations as possible be promise-valued, but depending on both the nature of the back end and the exact behavior of the binding, I/O might have to be done in almost every place.

aantron avatar Aug 10 '25 10:08 aantron

Right, currently if an API was exposed for this it would probably look something like:

val sessions_backend :
  get:(string -> string promise) ->
  set:(id:string -> expires_at:float -> string -> unit promise) ->
  delete:(string -> unit promise) ->
  ?lifetime:float ->
  middleware

Kinda awkward...

yawaramin avatar Aug 10 '25 18:08 yawaramin