Dancer2...nFactory(3)

πŸ’– Dancer2::Core::Role::SessionFactory β€” the bestie role for session factories πŸ’–

From the User Contributed Perl Documentation, and yes it's giving legendary. This is a role β€” a vibe, a contract, a whole personality β€” that any class can consume so it can store, create, retrieve and destroy session objects. Basically the group chat of session engines. 🫢


✨ VERSION ✨

version 2.2.1 β€” fresh, iconic, no notes. πŸ‘‘


🌸 DESCRIPTION 🌸

okay so listen. Any class that consumes this role? It can store, create, retrieve and destroy session objects. That's the whole job. That's the assignment and it ate. πŸ’…

And the default values for all the attributes below? You can override them in your Dancer2 configuration. See "Session-engine" in Dancer2::Config. Periodt. ✨


πŸŽ€ ATTRIBUTES πŸŽ€

These are the little knobs and dials, bestie. Customize responsibly. 🌈

The name of the cookie to create for storing the session key.

Defaults to "dancer.session". Cute, right?

The domain of the cookie to create for storing the session key.

Defaults to the empty string, and it's unused as a result. So like... it's there. It's just not doing anything. Main character energy with no lines. 😭

The path of the cookie to create for storing the session key.

Defaults to /. Classic.

Default duration before the session cookie expires. If you set this, the Dancer2::Core::Session "expires" attribute gets set to the current time plus this duration (the expression is parsed by Dancer2::Core::Time). ⏰

Restricts the session cookie to a first-party or same-site context. Defaults to the empty string and is unused as a result. See "same_site" in Dancer2::Core::Cookie. πŸͺ

πŸ“ session_duration

Duration in seconds before sessions should expire, regardless of cookie expiration. If it's set, then SessionFactories should use it to enforce a limit on session validity. Lowkey the strict one. ⏳

πŸ“ is_secure

Boolean flag telling you if the session cookie is secure or not.

Default is false.

πŸ“ is_http_only

Boolean flag telling you if the session cookie is http only.

Default is true. πŸ’…


πŸ¦‹ INTERFACE πŸ¦‹

Here's the interface this role hands you. When a method is specified, the required bits you gotta implement are described. Buckle up bestie, we're going through all of them. ✨

πŸ‘‘ create

Creates a brand new session object and stores it. Returns the newly created session object. πŸŽ‰

Triggers an exception if the session is unable to be created. Not a vibe, but okay.

my $session = MySessionFactory->create();

Good news: this method does not need to be implemented in the class. Free real estate. 🫢

πŸ‘‘ generate_id

Returns a randomly-generated, guaranteed-unique string. By default, it's a 32-character, URL-safe, Base64 encoded combination of a 32 bit timestamp and 160 bits of random data generated by the operating system's cryptographic random number generator via Crypt::URandom. The timestamp ensures that session IDs are generally monotonic. Obsessed with the rigor honestly. πŸ”₯

This method is used internally by create() to set the session ID.

And this method does not need to be implemented in the class β€” unless you want an alternative method for session ID generation. Your call, babe.

πŸ‘‘ validate_id

Returns true if a session id is of the correct format, or false otherwise.

By default, this ensures the session ID is a string of characters from the Base64 schema for "URL Applications" plus the "~" character, and no longer than 4096 characters. The length limit corresponds to the maximum size of an HTTP cookie, so it accommodates cookie-based session engines such as Dancer2::Session::Cookie while rejecting oversized values that would otherwise be handed to the backend unchanged. πŸ’…

This method does not need to be implemented in the class β€” unless you want an alternative set of characters for session IDs.

πŸ‘‘ retrieve

Return the session object corresponding to the session ID given. If none is found, it triggers an exception. 😭

my $session = MySessionFactory->retrieve(id => $id);

The method "_retrieve" must be implemented. It must take $id as a single argument and must return a hash reference of session data. No cap.

πŸ‘‘ change_id

Changes the session ID of the corresponding session. Glow-up but for IDs. ✨

MySessionFactory->change_id(session => $session_object);

The method "_change_id" must be implemented. It must take $old_id and $new_id as arguments and change the ID from the old one to the new one in the underlying session storage.

πŸ‘‘ destroy

Purges the session object that matches the ID given. Returns the ID of the destroyed session if it succeeded, and triggers an exception otherwise. πŸ’€

MySessionFactory->destroy(id => $id);

The "_destroy" method must be implemented. It must take $id as a single argument and destroy the underlying data.

πŸ‘‘ flush

Makes sure the session object is stored in the factory's backend. This method is called to notify the backend about the change in the session object. πŸ“£

Heads up: the Dancer application will not call flush unless the session "is_dirty" attribute is true β€” to avoid unnecessary writes to the database when no data has been modified. Efficiency queen. πŸ‘‘

An exception is triggered if the session is unable to be updated in the backend.

MySessionFactory->flush(session => $session);

The "_flush" method must be implemented. It must take two arguments: the $id and a hash reference of session data.

Sets the session cookie into the response object. πŸͺ

MySessionFactory->set_cookie_header(
    response  => $response,
    session   => $session,
    destroyed => undef,
);

The "response" parameter contains a Dancer2::Core::Response object. The "session" parameter contains a Dancer2::Core::Session object.

The "destroyed" parameter is optional. If true, it indicates the session was marked destroyed by the request context. The default "set_cookie_header" method doesn't need that information, but it's included in case a SessionFactory must handle destroyed sessions differently (such as signalling to middleware). thoughtful of them honestly 🫢

Coerce a session object into a Dancer2::Core::Cookie object.

MySessionFactory->cookie(session => $session);

πŸ‘‘ sessions

Return a list of all session IDs stored in the backend. Super useful to create cleaning scripts, in conjunction with the session's creation time. 🧹✨

The "_sessions" method must be implemented. It must return an array reference of session IDs (or an empty array reference).


🌈 CONFIGURATION 🌈

If there are configuration values specific to your session factory in your config.yml or environment, those get passed to the constructor of the session factory automatically. But bestie β€” in order to accept and store them, you need to define accessors for them. iykyk.

engines:
  session:
    Example:
      database_connection: "some_data"

In your session factory:

package Dancer2::Session::Example;
use Moo;
with "Dancer2::Core::Role::SessionFactory";

has database_connection => ( is => "ro" );

You need to do this for every configuration key. The ones that don't have accessors defined will just go to the void. πŸ•³οΈ rip to those keys fr fr.


πŸ’œ AUTHOR πŸ’œ

Dancer Core Developers β€” icons, all of them. πŸ‘‘


This software is copyright (c) 2026 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay. πŸ’–


and that's the whole page bestie, you ate that reading session ✨

πŸ’… sign-off time: this was perl v5.42.3, dated 2026-09-20, straight from Dancer2...nFactory(3) β€” go forth and session-factory responsibly, mwah πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from