[dancer-users] session() auto-flush

David Golden xdg at xdg.me
Fri Feb 14 10:45:56 GMT 2014

On Thu, Feb 13, 2014 at 11:37 PM, Warren Young <warren at etr-usa.com> wrote:
> I finally tracked it down to the fact that session(X, Y) automatically calls
> flush() on the object for you, whereas session() with no parameters returns
> a hashref that doesn't auto-flush itself.

Really, session() with no parameters returns a session *object*, it
happens to be implemented as a hash ref.  There's no magic to it.  To
make objects do things, you have to call methods on them.

Weirdly, D1 doesn't implement get/set methods on the session object.
Instead, Dancer::Session has read/write methods that set values
directly on the session object.

> On one level, I guess live and learn.  But, is it good design for Dancer to
> simply forget all my changes?  Why doesn't the second line in my route
> handler simply continue to modify the in-memory session object, and flush
> all the changes at once?

I assume you're talking Dancer 1, here, right?

You've discovered one of the flaws of the D1 session model -- every
write to the session via the DSL flushes the session to storage.  That
means going to network or disk for *every* session variable change.
Ouch!  What it sounds like you'd really like is to have all changes
flushed when the request is finishing (even on error) and D1 doesn't
do that.

That said, you have two options:

* Have your helper just call Dancer::Session->read() and ->write()
* Save your changes yourself by calling $session->flush

David Golden <xdg at xdg.me>
Take back your inbox! → http://www.bunchmail.com/
Twitter/IRC: @xdg

More information about the dancer-users mailing list