RejectedSoftware Forums

Sign up

Is it possible to store current session information globally? (dlang)

lets say I didnt want to pass the ServerResponse throughout my program into every function. For example, what if the res.session was storing the id of the current user. This is used often, so I wouldnt want this passed through each function. How do I store this session info globally or at least in a way I can access it easily without passing it into every function that requires it? Assuming there's multiple users using the site.

Re: Is it possible to store current session information globally? (dlang)

On 2013-12-09 18:01, carboncomputed wrote:

lets say I didnt want to pass the ServerResponse throughout my program into every function. For example, what if the res.session was storing the id of the current user. This is used often, so I wouldnt want this passed through each function. How do I store this session info globally or at least in a way I can access it easily without passing it into every function that requires it? Assuming there's multiple users using the site.

Just wondering, did you get this idea from the issue I posted today on
github?

One solution is to have something global that looks like
TaskLocal!Session m_session or TaskLocal!T and set it every time a
request comes through.

The other solution is something I'm working on since a couple days, I
have a fork up at https://www.github.com/etcimon/vibe.d that does this
if you compile with (dub) "Versions": ["WithRESThack"]

See here:
https://github.com/rejectedsoftware/vibe.d/issues/432

Basically, if you look at vibe/http/server.d:1205, the request and
response vars are (private) global which allows me to set some global
properties for servers. I made one called _session that returns
req.session, and this allows me to use SessionLocal!(T,KEY) variables.
(Experimental!) which is automatically set at any point in the program
for any user request.

I did this today, didn't test it in a REST session-enabled setting but
that's what it's intended for. I'm working on other session-specific
features for type-safety, memory cache and cache database, you'll find
it in vibe/http/session/