blib/lib/CHI/Driver/BerkeleyDB.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 #
2 # This file is part of CHI-Driver-BerkeleyDB
3 #
4 # This software is copyright (c) 2009 by Jonathan Swartz.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9  
10 package CHI::Driver::BerkeleyDB;
11 $CHI::Driver::BerkeleyDB::VERSION = '0.04';
12 # ABSTRACT: BerkeleyDB Cache Driver for CHI
13  
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use BerkeleyDB 0.30;
Subroutine BEGIN: called CC=1 SCAR=2.6
17 use CHI::Util 0.25 qw(read_dir);
18 use File::Path qw(mkpath);
19 use Moose;
20  
21 extends 'CHI::Driver';
22  
23 has db => (is => 'ro', lazy_build => 1);
24  
25 has db_class => (is => 'ro', default => 'BerkeleyDB::Hash');
26  
27 has dir_create_mode => (is => 'ro', isa => 'Int', default => oct(775));
28  
29 has env => (is => 'ro', lazy_build => 1);
30  
31 has filename => (is => 'ro', init_arg => undef, lazy_build => 1);
32  
33 has root_dir => (is => 'ro');
34  
35 sub _build_filename {
36 my $self = shift;
37 return $self->escape_for_filename( $self->namespace ) . ".db";
38 }
39  
40 sub _build_env {
41 my $self = shift;
42  
43 my $root_dir = $self->root_dir;
44  
45 unless (defined $root_dir) {
46 die "must specify one of env or root_dir";
47 }
48  
49 unless (-d $root_dir) {
50 mkpath( $root_dir, 0, $self->dir_create_mode );
51 }
52  
53 my $env = BerkeleyDB::Env->new(
54 '-Home' => $self->root_dir,
55 '-Config' => {},
56 '-Flags' => DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
57 or die sprintf( "cannot open Berkeley DB environment in '%s': %s",
58 $root_dir, $BerkeleyDB::Error );
59  
60 return $env;
61 }
62  
63 sub _build_db {
64 my $self = shift;
65  
66 my $filename = $self->filename;
67 my $db = $self->db_class->new(
68 '-Filename' => $filename,
69 '-Flags' => DB_CREATE,
70 '-Env' => $self->env)
71 or die
72 sprintf( "cannot open Berkeley DB file '%s' in environment '%s': %s",
73 $filename, $self->root_dir, $BerkeleyDB::Error );
74  
75 return $db;
76 }
77  
78 sub fetch {
79 my ($self, $key) = @_;
80  
81 my $data;
82  
83 return ( $self->db->db_get( $key, $data ) == 0 ) ? $data : undef;
84 }
85  
86 sub store {
87 my ($self, $key, $data) = @_;
88  
89 $self->db->db_put( $key, $data ) == 0
90 or die $BerkeleyDB::Error;
91 }
92  
93 sub remove {
94 my ($self, $key) = @_;
95  
96 $self->db->db_del($key) == 0
97 or die $BerkeleyDB::Error;
98 }
99  
100 sub clear {
101 my $self = shift;
102  
103 my $count = 0;
104 $self->db->truncate($count) == 0
105 or die $BerkeleyDB::Error;
106 }
107  
108 sub get_keys {
109 my $self = shift;
110  
111 my @keys;
112 my $cursor = $self->db->db_cursor();
113 my ($key, $value) = ('', '');
114  
115 while ( $cursor->c_get( $key, $value, BerkeleyDB::DB_NEXT() ) == 0 ) {
116 push( @keys, $key );
117 }
118  
119 return @keys;
120 }
121  
122 sub get_namespaces {
123 my $self = shift;
124  
125 my @contents = read_dir( $self->root_dir );
126  
127 my @namespaces =
128 map { $self->unescape_for_filename( substr( $_, 0, -3 ) ) }
129 grep { /\.db$/ } @contents;
130  
131 return @namespaces;
132 }
133  
134 __PACKAGE__->meta->make_immutable;
135  
136 __END__
137  
138 =pod
139  
140 =encoding UTF-8
141  
142 =head1 NAME
143  
144 CHI::Driver::BerkeleyDB - BerkeleyDB Cache Driver for CHI
145  
146 =head1 VERSION
147  
148 version 0.04
149  
150 =head1 SYNOPSIS
151  
152 use CHI;
153  
154 my $cache = CHI->new(
155 driver => 'BerkeleyDB',
156 root_dir => '/path/to/cache/root'
157 );
158  
159 =head1 DESCRIPTION
160  
161 This cache driver uses L<Berkeley DB|BerkeleyDB> files to store data. Each
162 namespace is stored in its own db file.
163  
164 By default, the driver configures the Berkeley DB environment to use the
165 Concurrent Data Store (CDS), making it safe for multiple processes to read and
166 write the cache without explicit locking.
167  
168 =for Pod::Coverage clear fetch get_keys get_namespaces remove store
169  
170 =head1 CONSTRUCTOR OPTIONS
171  
172 =over 4
173  
174 =item *
175  
176 root_dir
177  
178 Path to the directory that will contain the Berkeley DB environment, also known as the "Home".
179  
180 =item *
181  
182 db_class
183  
184 BerkeleyDB class, defaults to BerkeleyDB::Hash.
185  
186 =item *
187  
188 env
189  
190 Use this Berkeley DB environment instead of creating one.
191  
192 =item *
193  
194 db
195  
196 Use this Berkeley DB object instead of creating one.
197  
198 =back
199  
200 =head1 HISTORY
201  
202 Originally created by Jonathan Swartz. Version 0.04 and later maintained by
203 Michael Schout.
204  
205 =head1 SEE ALSO
206  
207 =over 4
208  
209 =item *
210  
211 L<CHI>
212  
213 =item *
214  
215 L<BerkeleyDB>
216  
217 =back
218  
219 =head1 SOURCE
220  
221 The development version is on github at L<http://https://github.com/mschout/perl-chi-driver-bdb>
222 and may be cloned from L<git://https://github.com/mschout/perl-chi-driver-bdb.git>
223  
224 =head1 BUGS
225  
226 Please report any bugs or feature requests on the bugtracker website
227 L<https://github.com/mschout/perl-chi-driver-bdb/issues>
228  
229 When submitting a bug or request, please include a test-file or a
230 patch to an existing test-file that illustrates the bug or desired
231 feature.
232  
233 =head1 AUTHOR
234  
235 Michael Schout <mschout@cpan.org>
236  
237 =head1 COPYRIGHT AND LICENSE
238  
239 This software is copyright (c) 2009 by Jonathan Swartz.
240  
241 This is free software; you can redistribute it and/or modify it under
242 the same terms as the Perl 5 programming language system itself.
243  
244 =cut
↑ Summary