File Coverage

blib/lib/Dancer2/Session/Sereal.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod n/a
total 45 45 100.0


line stmt bran cond sub pod time code
1 2     2   23993 use 5.008001;
  2         15  
2 2     2   11 use strict;
  2         4  
  2         59  
3 2     2   9 use warnings;
  2         5  
  2         153  
4              
5             package Dancer2::Session::Sereal;
6             our $AUTHORITY = 'cpan:YANICK';
7             # ABSTRACT: Dancer 2 session storage in files with Sereal
8             # VERSION
9             $Dancer2::Session::Sereal::VERSION = '0.003';
10 2     2   12 use Moo;
  2         2  
  2         21  
11 2     2   726 use Dancer2::Core::Types;
  2         4  
  2         1104  
12 2     2   14 use Sereal::Encoder;
  2         2  
  2         73  
13 2     2   8 use Sereal::Decoder;
  2         3  
  2         720  
14              
15             #--------------------------------------------------------------------------#
16             # Attributes
17             #--------------------------------------------------------------------------#
18              
19             has _suffix => (
20             is => 'ro',
21             isa => Str,
22             default => sub { ".srl" },
23             );
24              
25             has encoder_args => (
26             is => 'ro',
27             isa => HashRef,
28             default => sub{
29             +{
30             snappy => 1,
31             croak_on_bless => 1,
32             }
33             }
34             );
35              
36             has _encoder => (
37             is => 'lazy',
38             isa => InstanceOf ['Sereal::Encoder'],
39             handles => { '_freeze' => 'encode' },
40             );
41              
42             sub _build__encoder {
43 2     2   1623 my ($self) = @_;
44 2         79 return Sereal::Encoder->new( $self->encoder_args );
45             }
46              
47             has decoder_args => (
48             is => 'ro',
49             isa => HashRef,
50             default => sub{
51             +{
52             refuse_objects => 1,
53             validate_utf8 => 1,
54             }
55             }
56             );
57              
58             has _decoder => (
59             is => 'lazy',
60             isa => InstanceOf ['Sereal::Decoder'],
61             handles => { '_thaw' => 'decode' },
62             );
63              
64             sub _build__decoder {
65 2     2   1808 my ($self) = @_;
66 2         66 return Sereal::Decoder->new( $self->decoder_args );
67             }
68              
69             #--------------------------------------------------------------------------#
70             # Role composition
71             #--------------------------------------------------------------------------#
72              
73             with 'Dancer2::Core::Role::SessionFactory::File';
74              
75             sub _freeze_to_handle {
76 8     8   198265 my ($self, $fh, $data) = @_;
77 8         37 binmode $fh;
78 8         13 print {$fh} $self->_freeze($data);
  8         151  
79 8         419 return;
80             }
81              
82             sub _thaw_from_handle {
83 7     7   97718 my ($self, $fh) = @_;
84 7         26 binmode($fh);
85 7         12 return $self->_thaw( do { local $/; <$fh> } );
  7         29  
  7         293  
86             }
87              
88             1;
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             Dancer2::Session::Sereal - Dancer 2 session storage in files with Sereal
97              
98             =head1 VERSION
99              
100             version 0.003
101              
102             =head1 DESCRIPTION
103              
104             This module implements Dancer 2 session engine based on L files.
105              
106             This backend can be used in single-machine production environments, but two
107             things should be kept in mind: The content of the session files is not
108             encrypted or protected in anyway and old session files should be purged by a
109             CRON job.
110              
111             =head1 CONFIGURATION
112              
113             The setting B should be set to C in order to use this session
114             engine in a Dancer2 application.
115              
116             Files will be stored to the value of the setting C, whose default
117             value is C.
118              
119             Arguments for the L and L objects can be
120             given via the C and C. If not provided, they default to
121             C<< snappy => 1, croak_on_bless =>1 >> and C<< refuse_objects => 1, validate_utf8 => 1 >>, respectively.
122              
123             Here is an example configuration that use this session engine and stores session
124             files in /tmp/dancer-sessions
125              
126             session: "Sereal"
127              
128             engines:
129             session:
130             Sereal:
131             session_dir: "/tmp/dancer-sessions"
132             encoder_args:
133             snappy: 1
134             croak_on_bless: 1
135             decoder_args:
136             refuse_objects: 1
137             validate_utf8: 1
138              
139             =head1 AUTHOR
140              
141             David Golden
142              
143             =head1 COPYRIGHT AND LICENSE
144              
145             This software is Copyright (c) 2013 by David Golden.
146              
147             This is free software, licensed under:
148              
149             The Apache License, Version 2.0, January 2004
150              
151             =cut
152              
153             __END__