File Coverage

blib/lib/Ado/Sessions/File.pm
Criterion Covered Total %
statement 31 31 100.0
branch 3 4 75.0
condition 4 5 80.0
subroutine 8 9 88.8
pod 5 5 100.0
total 51 54 94.4


line stmt bran cond sub pod time code
1             package Ado::Sessions::File;
2              
3 3     3   663 use Mojo::Base 'Ado::Sessions';
  3         4  
  3         15  
4 3     3   396 use Mojo::Util qw(b64_decode b64_encode);
  3         5  
  3         142  
5 3     3   233 use File::Spec::Functions qw(tmpdir catfile);
  3         567  
  3         151  
6 3     3   248 use Mojo::File 'path';
  3         15291  
  3         1018  
7              
8             has dstdir => sub {tmpdir};
9              
10             sub dstfile {
11 10     10 1 109 my ($self, $id) = @_;
12 10         60 return 'ado.' . $id;
13             }
14              
15             sub absfile {
16 10     10 1 24 my ($self, $id) = @_;
17 10         25 return catfile $self->dstdir, $self->dstfile($id);
18             }
19              
20             sub load {
21 5     5 1 121 my ($self, $c) = @_;
22              
23 5         10 my $session = {};
24 5   100     17 my $id = $self->session_id($c) || '';
25 5         428 my $cookie_file = $self->absfile($id);
26              
27 5 100 66     64 if ($id and -e $cookie_file) {
28 3         11 my $sessiondata = path($cookie_file)->slurp();
29             return
30 3 50       223 unless $session = Mojo::JSON::j(b64_decode($sessiondata));
31             }
32              
33 5         591 return $self->prepare_load($c, $session);
34             }
35              
36             sub store {
37 5     5 1 2620 my ($self, $c) = @_;
38              
39 5         25 my ($id, $session) = $self->prepare_store($c);
40 5         24 my $value = b64_encode(Mojo::JSON::encode_json($session), '');
41 5         435 my $file = $self->absfile($id);
42 5         19 path($file)->spurt($value);
43 5         1119 chmod(oct('0600'), $file);
44 5         20 return;
45             }
46              
47             # TODO
48       0 1   sub cleanup {
49              
50             # go to session dir
51             # find all ado.* files
52             # filter against file age where is older than session timeout
53             # unlink all old files
54              
55             # Warning!
56             # This action will slow down the application performance, so considering
57             # any other GC, like cronjob or watchdog would be a better solution.
58             }
59              
60             1;
61              
62             =pod
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             Ado::Sessions::File - manage sessions stored in files
69              
70             =head1 DESCRIPTION
71              
72             L manages sessions for
73             L. All data gets serialized with L and stored
74             C encoded in a file. A cookie or a request parameter can be used to
75             share the session id between the server and the user agents.
76              
77             =head1 ATTRIBUTES
78              
79             L inherits all attributes from
80             L and implements the following new ones.
81              
82             =head2 dstdir
83              
84             Path where to store session data files.
85              
86              
87             =head1 METHODS
88              
89             =head2 absfile
90              
91             Compose absolute path to session data file.
92              
93             =head2 cleanup
94              
95             This method is a garbage collector. Cleans up expired session files.
96              
97              
98             =head2 dstfile
99              
100             File name of the session data file.
101              
102             =head2 load
103              
104             Load session data from file.
105              
106             =head2 store
107              
108             Store session data in file.
109              
110              
111             =head1 SEE ALSO
112              
113             L, L
114              
115             =cut
116              
117