File Coverage

blib/lib/WebDAO/Store/MLDBM.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package WebDAO::Store::MLDBM;
2              
3             #$Id: MLDBM.pm 509 2009-03-03 19:45:02Z zag $
4              
5             =head1 NAME
6              
7             WebDAO::Store::MLDBM - Implement session store using MLDBM
8              
9             =head1 SYNOPSIS
10              
11            
12             ...
13             SetEnv wdSession WebDAO::Sessionco
14             #for use external storage
15             SetEnv wdStore WebDAO::Store::MLDBM
16             SetEnv wdStorePar path=/tmp
17             ....
18              
19            
20              
21             =head1 DESCRIPTION
22              
23             WebDAO::Store::MLDBM - Implement session store using MLDBM
24              
25             =cut
26              
27 2     2   132215 use File::Path;
  2         4  
  2         103  
28 2     2   10 use Fcntl ":flock";
  2         2  
  2         248  
29 2     2   1636 use IO::File;
  2         1805  
  2         243  
30 2     2   3885 use MLDBM qw (DB_File Data::Dumper);
  2         6677  
  2         10  
31 2     2   1795 use WebDAO::Store::Storable;
  0            
  0            
32             use Data::Dumper;
33             use strict 'vars';
34             use base 'WebDAO::Store::Storable';
35              
36             our $VERSION = '1.01';
37              
38             sub load {
39             my $self = shift;
40             my $id = shift || return {};
41             my %hash;
42             my $db_file = $self->_dir() . "sess_$id.db";
43             my $db = tie %hash, "MLDBM", $db_file, O_CREAT | O_RDWR, 0644 or die "$!";
44             my $fd = $db->fd();
45             undef $db;
46             local *DBM;
47             open DBM, "+<&=$fd" or die "$!";
48             flock DBM, LOCK_SH;
49             my $tmp_hash = $hash{$id};
50             untie %hash;
51             flock DBM, LOCK_UN;
52             close DBM;
53             return $tmp_hash;
54             }
55              
56             sub store {
57             my $self = shift;
58             my $id = shift || return {};
59             my $ref_tree = shift;
60             return unless $ref_tree && ref($ref_tree);
61             my %hash;
62             my $db_file = $self->_dir() . "sess_$id.db";
63             my $db = tie %hash, "MLDBM", $db_file, O_CREAT | O_RDWR, 0644 or die "$!";
64             my $fd = $db->fd();
65             undef $db;
66             local *DBM;
67             open DBM, "+<&=$fd" or die "$!";
68             flock DBM, LOCK_EX;
69             $hash{$id} = $ref_tree;
70             untie %hash;
71             flock DBM, LOCK_UN;
72             close DBM;
73             return $id;
74              
75             }
76             1;