Programming:Perl:Jedi:Plugin Session

From Knowledge base
Jump to: navigation, search

Jedi::Plugin::Session is an extension for Perl Jedi to give your the possibility to store session for your user.

The plugin will automatically generate and save a UUID for the visiting user, and extend the Jedi::Request to add the methods: session_get and session_set.

You have no limit (except your memory) on how many data you store for each user. You can only save a serializable object.

The plugin session has for now 3 engines :

I may add a file engine soon.

On the Redis and SQLite engines allow you to share the same app launching in multiple workers the session.

Each app has his session data.

The UUID store in a cookie in the browser of the user is only a part of the final UUID. If someone steals that cookie, he will have a different session and also if you change the browser.

Let show an example of how to use it :

package myApps;
use Jedi::App;
use Jedi::Plugin::Session;

sub jedi_app {
  my ($app) = @_;

  $app->get('/set', sub {
    my ($app, $request, $response) = @_;
    my $session = $request->session_get // {};
    my $k = $request->params->{'k'};
    my $v = $request->params->{'v'};


    if (defined $k) {
      $session->{$k} = $v;
    } else {

  $app->get('/get', sub {
    my ($app, $request, $response) = @_;
    my $session = $request->session_get // {};
    my $k = $request->params->{'k'};
    my $session_val;
    if (defined $k) {
      $session_val = $session->{$k};
    $response->body(defined $session_val ? $session_val : "undef");



The session is not necessary a HASH. But it is often simpler to save one this way.

If the user is a new one, or his session has expired, then the session data will be undefined.

If you want to reset the expiration time you can do :

$request->session_set($request->session_get // {})

To use another engine, it is simple :

use Jedi::Plugin::Session 'SQLite';
use Jedi::Plugin::Session 'Redis';

Each engine can be configured with the Jedi configuration.

You can take a look at the possibility with the BACKENDS doc.

Each engine should work without any configuration. SQLite will store the data in the dist_dir of the Jedi::Plugin::Session, and create a session file per app. The Redis will use the default port to store the session.

Share your opinion