celogeek

NAME

Jedi::Plugin::Session - Session for Jedi

VERSION

version 0.05

DESCRIPTION

This plugin add to the Jedi::Request two methods : session_get and session_set

A secure UUID is generated on all get/post request, and if the cookie jedi_session is missing, then it will automatically create it to keep the same UUID between each request.

If the cookie with the UUID is copied on a different browser / computer, it will work but the session will not be the same.

SYNOPSIS

The session is very specific to an app, different app in the same Jedi instance, will use different session data.

 package MyJediApp;
 use Jedi::App;
 use Jedi::Plugin::Session;
 use JSON;
 sub jedi_app {
  my ($app) = @_;

  $app->get('/set_session', sub {
    my ($app, $request, $response) = @_;
    my $session = $request->session_get // {};
    $session->{val1} = { this => 'is', a => 'test' };
    $request->session_set($session);
    $response->status(200);
    $response->body('session set !')
  });
 
  $app->get('/get_session', sub {
    my ($app, $request, $response) = @_;
    my $session = $request->session_get;
    $response->status(200);
    $response->body(defined $session ? encode_json($session) : 'session not defined !');
  })
 }
 1;

LIMITATION

The session is keep in memory and serialized. You can't save CODE or unserializable object in the session.

The default expiration is '3 hours'. The cookie with a part of the UUID is keep for 2 years and sent only once.

EXPIRATION

To change the default expiration for your app, you can use the configuration like this :

 MyJediApp: # package name of your app
  session:
    expiration: 3 hours

Check Time::Duration::Parse for the possible value of the expiration.

If you need to set the full expiration time again, you can just set again the session :

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

BACKENDS

Memory

The default backend is memory. It use CHI with the Memory driver.

Redis

You can use Redis as a backend.

 package MyJediApp;
 use Jedi::App;
 use Jedi::Plugin::Session 'Redis';

Everything work the same way.

You can setup Redis access in the configuration like this :

 MyJediApp: # package name of your app
  session:
    expiration: 3 hours
    redis:
      config:
        reconnect: 2
        every: 100
        server: 127.0.0.1:6900
      prefix: my_jedi_app

The redis_prefix will be used to generate the session key. The result will be :

  jedi_prefix_YOUR_PREFIX_UUID

SQLite

You can use DBD::SQLite as a backend.

 package MyJediApp;
 use Jedi::App;
 use Jedi::Plugin::Session 'SQLite';

You can configuration SQLite database file in the configuration :

 MyJediApp: # package name of your app
  session:
    expiration: 3 hours
  sqlite:
    path: /var/lib/jedi_session

The path is the base path for your app. In that case the final file is :

  /var/lib/jedi_session/MyJediApp.db

If your app name is My::Jedi::App, then the final file is :

  /var/lib/jedi_session/My/Jedi/App.db

By default the file is store in the Jedi::Plugin::Session dist_dir. Each app has his own database.

If you start multiple workers, all of them will use the same database and then share the session.

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/celogeek/perl-jedi-plugin-session/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

celogeek <me@celogeek.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by celogeek <me@celogeek.com>.

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