blib/lib/Object/Cache/Sqlite.pm

filter: Statementstmt 97.7% 128 / 131 Branchbran 67.6% 23 / 34 Conditioncond 57.1% 8 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 21 / 21 Podpod 72.7% 8 / 11 totaltotal 86.7% 190 / 219 CC 24 SCAR 11.9
CC
24
Cov
89%
set
6.1
remove_expired_entries
5.8
get
5.0
CRAP 3.3
↑ Summary
1 package Object::Cache::Sqlite;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = 'v0.1.2';
11  
12 3 my $JSON = Cpanel::JSON::XS->new->utf8->allow_nonref;
13  
14 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
15 4 my ($class, %args) = @_;
16  
17 4 ▶ my $db_file = $args{db_file} or croak("db_file is required");
Branch: 1 branch
BranchTrueFalseTotal
my $db_file = $args{"db_file"} or croak("db_file is required") 3 1 4
18 3 ▶ my $silent = $args{silent} // 1;
Condition50%1/2
$args{"silent"} // 1
l!l
03
MC/DC0%0/1
$args{"silent"} // 1
$args{"silent"}
Truth table50%1/2
A
A= $args{"silent"}
Aresult
11
00
19  
20 3 my $self = {
21 db_file => $db_file,
22 dbh => undef,
23 silent => $silent,
24 };
25  
26 3 bless $self, $class;
27 3 $self->_init_db();
28  
29 3 return $self;
30 }
31  
32 3 ▶sub _init_db {
Subroutine _init_db: called CC=3 SCAR=11.1
33 3 my ($self) = @_;
34  
35 3 my $opts = {
36 RaiseError => 1,
37 AutoCommit => 1,
38 };
39  
40 3 my $dbh = DBI->connect("dbi:SQLite:dbname=$self->{db_file}", '', '', $opts);
41  
42 3 $self->{dbh} = $dbh;
43  
44 3 my $sth = $dbh->prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='cache'");
45 3 $sth->execute();
46  
47 3 my $table_exists = $sth->fetchrow_array();
48  
49  
50 3 ▶ if (!$table_exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_exists) 3 0 3
51 3 $dbh->do("CREATE TABLE cache (
52 CreateTime INT,
53 ExpireTime INT,
54 Key VARCHAR(255) PRIMARY KEY UNIQUE,
55 Value BLOB
56 )");
57 3 $dbh->do("CREATE INDEX ExpireTimeIndex ON cache (ExpireTime)");
58  
59 3 ▶ if (!$self->{silent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"silent"}) 0 3 3
60 0 warn "Created cache table in $self->{db_file}\n";
61 }
62 }
63  
64 3 return 1;
65 }
66  
67 11 ▶sub _store_value {
Subroutine _store_value: called CC=1 SCAR=0.0
68 11 my ($self, $value) = @_;
69 11 return $JSON->encode($value);
70 }
71  
72 11 ▶sub _load_value {
Subroutine _load_value: called CC=2 SCAR=7.0
73 11 my ($self, $data) = @_;
74 11 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 11 11
75 11 return eval { $JSON->decode($data) };
76 }
77  
78 15 ▶sub get {
Subroutine get: called CC=5 SCAR=16.1
Pod documented
79 15 my ($self, $key) = @_;
80  
81 15 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 15 15
82  
83 15 my $dbh = $self->{dbh};
84 15 my $now = time();
85  
86 15 my $sth = $dbh->prepare("SELECT Value, ExpireTime FROM cache WHERE Key = ?");
87 15 $sth->execute($key);
88  
89 15 my ($value, $expire_time) = $sth->fetchrow_array();
90  
91  
92 15 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 3 12 15
93 3 return undef;
94 }
95  
96 12 ▶ if (defined $expire_time && $expire_time < $now) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expire_time and $expire_time < $now) 1 11 12
Condition66%2/3
defined $expire_time and $expire_time < $now
!ll&&!rl&&r
0111
MC/DC50%1/2
defined $expire_time and $expire_time < $now
defined $expire_time $expire_time < $now
Truth table66%2/3
A and B
A= defined $expire_timeB= $expire_time < $now
ABresult
0X0
100
111
97 1 $self->delete($key);
98 1 $self->remove_expired_entries(0);
99 1 return undef;
100 }
101  
102 11 return $self->_load_value($value);
103 }
104  
105 12 ▶sub set {
Subroutine set: called CC=6 SCAR=18.0
Pod documented
106 12 my ($self, $key, $value, $expires) = @_;
107  
108 12 ▶ if (!defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 12 12
109 0 return 0;
110 }
111  
112 12 ▶ $expires //= 3600;
Condition100%2/2
$expires //= 3600
l!l
39
MC/DC100%1/1
$expires //= 3600
$expires
Truth table100%2/2
A
A= $expires
Aresult
11
00
113  
114 12 ▶ if ($expires < 100000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires < 100000) 10 2 12
115 10 $expires = time() + $expires;
116 }
117  
118 12 ▶ if ($expires < time()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires < time) 1 11 12
119 1 return 0;
120 }
121  
122 11 my $dbh = $self->{dbh};
123 11 my $now = time();
124  
125 11 my $data = $self->_store_value($value);
126 11 my $sth = $dbh->prepare("REPLACE INTO cache (CreateTime, ExpireTime, Key, Value) VALUES (?, ?, ?, ?)");
127 11 my $result = $sth->execute($now, $expires, $key, $data);
128  
129  
130 11 ▶ return $result ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 11 0 11
131 }
132  
133 2 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.2
Pod documented
134 2 my ($self, $key) = @_;
135  
136 2 ▶ return 0 unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 2 2
137  
138 2 my $dbh = $self->{dbh};
139 2 my $sth = $dbh->prepare("DELETE FROM cache WHERE Key = ?");
140 2 my $result = $sth->execute($key);
141  
142  
143 2 ▶ return $result ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 2 0 2
144 }
145  
146 4 ▶sub cached_item_count {
Subroutine cached_item_count: called CC=2 SCAR=7.0
Pod documented
147 4 my ($self) = @_;
148  
149 4 my $dbh = $self->{dbh};
150 4 my $now = time();
151  
152 4 my $sth = $dbh->prepare("SELECT COUNT(*) FROM cache WHERE ExpireTime >= ? OR ExpireTime IS NULL");
153 4 $sth->execute($now);
154  
155 4 my ($count) = $sth->fetchrow_array();
156  
157  
158 4 ▶ return $count // 0;
Condition50%1/2
$count // 0
l!l
40
MC/DC0%0/1
$count // 0
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
159 }
160  
161 1 ▶sub cached_item_keys {
Subroutine cached_item_keys: called CC=2 SCAR=6.9
Pod documented
162 1 my ($self) = @_;
163  
164 1 my $dbh = $self->{dbh};
165 1 my $now = time();
166  
167 1 my $sth = $dbh->prepare("SELECT Key FROM cache WHERE ExpireTime >= ? OR ExpireTime IS NULL");
168 1 $sth->execute($now);
169  
170 1 my @keys;
171 1 ▶ while (my ($key) = $sth->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($key) = $sth->fetchrow_array) 3 1 4
172 3 push @keys, $key;
173 }
174  
175  
176  
177 1 return \@keys;
178 }
179  
180 1 ▶sub remove_expired_entries {
Subroutine remove_expired_entries: called CC=5 SCAR=17.6
Pod documented
181 1 my ($self, $vacuum) = @_;
182  
183 1 ▶ $vacuum //= 1;
Condition50%1/2
$vacuum //= 1
l!l
10
MC/DC0%0/1
$vacuum //= 1
$vacuum
Truth table50%1/2
A
A= $vacuum
Aresult
11
00
184  
185 1 my $dbh = $self->{dbh};
186 1 my $now = time();
187  
188 1 my $sth = $dbh->prepare("DELETE FROM cache WHERE ExpireTime < ?");
189 1 $sth->execute($now);
190 1 my $deleted = $sth->rows;
191  
192  
193 1 ▶ if ($vacuum && $deleted > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vacuum and $deleted > 0) 0 1 1
Condition33%1/3
$vacuum and $deleted > 0
!ll&&!rl&&r
100
MC/DC0%0/2
$vacuum and $deleted > 0
$vacuum $deleted > 0
Truth table33%1/3
A and B
A= $vacuumB= $deleted > 0
ABresult
0X0
100
111
194 0 $self->vacuum();
195 }
196  
197 1 ▶ return $deleted > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$deleted > 0 ? : 0 1 1
198 }
199  
200 1 ▶sub vacuum {
Subroutine vacuum: called CC=1 SCAR=0.0
Pod undocumented
201 1 my ($self) = @_;
202  
203 1 my $dbh = $self->{dbh};
204 1 $dbh->do("VACUUM");
205  
206 1 return 1;
207 }
208  
209 1 ▶sub empty_cache {
Subroutine empty_cache: called CC=1 SCAR=0.0
Pod documented
210 1 my ($self) = @_;
211  
212 1 my $dbh = $self->{dbh};
213 1 my $sth = $dbh->prepare("DELETE FROM cache");
214 1 $sth->execute();
215  
216 1 my $deleted = $sth->rows;
217  
218  
219 1 $self->vacuum();
220  
221 1 return $deleted;
222 }
223  
224 1 ▶sub init_db {
Subroutine init_db: called CC=1 SCAR=0.0
Pod undocumented
225 1 my ($self) = @_;
226  
227 1 my $dbh = $self->{dbh};
228  
229 1 $dbh->do("DROP TABLE IF EXISTS cache");
230 1 $dbh->do("CREATE TABLE cache (
231 CreateTime INT,
232 ExpireTime INT,
233 Key VARCHAR(255) PRIMARY KEY UNIQUE,
234 Value BLOB
235 )");
236  
237 1 $dbh->do("CREATE INDEX ExpireTimeIndex ON cache (ExpireTime)");
238  
239 1 return 1;
240 }
241  
242 3 ▶sub disconnect {
Subroutine disconnect: called CC=2 SCAR=7.0
Pod undocumented
243 3 my ($self) = @_;
244  
245 3 ▶ if ($self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}) 3 0 3
246 3 $self->{dbh}->disconnect();
247 3 $self->{dbh} = undef;
248 }
249  
250 3 return 1;
251 }
252  
253 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
254 3 my ($self) = @_;
255 3 eval { $self->disconnect() };
256 }
257  
258 3 1;
259  
260 __END__
↑ Summary