| 1 | package CGI::Ex::Recipes::Cache; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use CGI::Ex::Dump qw(debug dex_warn ctrace dex_trace); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Storable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=20.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | my $class = shift || __PACKAGE__; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || __PACKAGE__
MC/DC0%0/1
shift() || __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | $args->{expires} ||= time + 3600;#one hour by default | ||||||||||||||||||||||||||
|
Condition33%1/3
$args->{"expires"} ||= time + 3600
MC/DC0%0/2
$args->{"expires"} ||= time + 3600
$args->{"expires"}
time + 3600
Truth table33%1/3
A ||= B
A= $args->{"expires"}B= time + 3600
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | $args->{cache_hash} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"cache_hash"} ||= {}
MC/DC0%0/1
$args->{"cache_hash"} ||= {}
$args->{"cache_hash"}
Truth table50%1/2
A
A= $args->{"cache_hash"}
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | $args->{dbh} || croak 'Please provide a database handle with a `cache` table in the database!'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 1 | return bless {%$args}, $class; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #tries first the 'our %CACHE_HASH', then the database. | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 0 | my ($self,$key) = @_; | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | 1 if(exists $self->{cache_hash}{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | 0; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 3 | my ($self,$key) = @_; | |||||||||||||||||||||||||||
| 28 | #dex_trace(); #debug $self; | ||||||||||||||||||||||||||||
| 29 | 3 | ▶ | if(exists $self->{cache_hash}{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | return $self->{cache_hash}{$key}{value}; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | #warn 'getting $key'.$key.' from database'; | ||||||||||||||||||||||||||||
| 33 | 3 | ▶ | my $row = $self->{dbh}->selectrow_hashref('SELECT * FROM cache WHERE id=?',{},$key) | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key) || (return undef)
MC/DC100%1/1
$self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key) || (return undef)
$self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key)
Truth table100%2/2
A
A= $self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key)
| |||||||||||||||||||||||||||||
| 34 | || return undef; | ||||||||||||||||||||||||||||
| 35 | 2 | $self->{cache_hash}{$key} = $row; | |||||||||||||||||||||||||||
| 36 | 2 | ▶ | if($self->{cache_hash}{$key}{expires} < time ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | #warn 'could not $key'.$key.' from database. data expired.'; | ||||||||||||||||||||||||||||
| 38 | 0 | return undef; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 2 | return $self->{cache_hash}{$key}{value}; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | if (!$_[2]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | croak 'Please provide a value to be set!'; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | #NOTE: compatible only with SQLITE and MySQL | ||||||||||||||||||||||||||||
| 48 | $_[0]->{dbh}->prepare( | ||||||||||||||||||||||||||||
| 49 | 'REPLACE INTO `cache` (id, value, tstamp, expires) VALUES ( ?,?,?,? )' | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | )->execute( $_[1], $_[2], time, ($_[3]?time+$_[3]:$_[0]->{expires}) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 1 | $_[0]->{cache_hash} = {}; | |||||||||||||||||||||||||||
| 55 | $_[0]->{dbh}->do('DELETE FROM `cache`') | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | and $_[0]->{dbh}->do('VACUUM'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub freeze { | ||||||||||||||||||||||||||
|
Subroutine
freeze: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | $_[0]->set( | ||||||||||||||||||||||||||||
| 62 | $_[1], | ||||||||||||||||||||||||||||
| 63 | ref $_[2] ? Storable::nfreeze($_[2]) : $_[2], | ||||||||||||||||||||||||||||
| 64 | $_[3] || $_[0]->{expires}, | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[3] || $_[0]{'expires'}
MC/DC0%0/2
$_[3] || $_[0]{'expires'}
$_[3]
$_[0]{'expires'}
Truth table33%1/3
A || B
A= $_[3]B= $_[0]{'expires'}
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | sub thaw { | ||||||||||||||||||||||||||
|
Subroutine
thaw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 1 | Storable::thaw( $_[0]->get($_[1]) ) ; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | 1; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | __END__ | ||||||||||||||||||||||||||||