| 1 | package DBIx::Class::Cursor::Cached; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Storable (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp::Clan qw/^DBIx::Class/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | $VERSION = '1.001002'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||
| 15 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 4 | my ($storage, $args, $attrs) = @_; | |||||||||||||||||||||||||||
| 17 | 4 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | # This gives us the class the storage object -would- have used | ||||||||||||||||||||||||||||
| 19 | # (since cursor_class is inherited Class::Accessor::Grouped type) | ||||||||||||||||||||||||||||
| 20 | 4 | my $inner_class = (ref $storage)->cursor_class; | |||||||||||||||||||||||||||
| 21 | 4 | my $inner = $inner_class->new(@_); | |||||||||||||||||||||||||||
| 22 | 4 | ▶ | if ($attrs->{cache_for}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | my %args = ( | ||||||||||||||||||||||||||||
| 24 | inner => $inner, | ||||||||||||||||||||||||||||
| 25 | cache_for => delete $attrs->{cache_for}, | ||||||||||||||||||||||||||||
| 26 | cache_object => delete $attrs->{cache_object}, | ||||||||||||||||||||||||||||
| 27 | # this must be here to ensure the deletes have happened | ||||||||||||||||||||||||||||
| 28 | 4 | cache_key => $class->_build_cache_key(@_), | |||||||||||||||||||||||||||
| 29 | pos => 0 | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | 4 | return bless(\%args, $class); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 0 | return $inner; # return object that -would- have been constructed. | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub next { | ||||||||||||||||||||||||||||
| 37 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 6 | ▶ | return @{($self->{data} ||= $self->_fill_data)->[$self->{pos}++]||[]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"data"} ||= $self->_fill_data
MC/DC0%0/2
$self->{"data"} ||= $self->_fill_data
$self->{"data"}
$self->_fill_data
Truth table66%2/3
A ||= B
A= $self->{"data"}B= $self->_fill_data
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub all { | ||||||||||||||||||||||||||||
| 42 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | return @{$self->{data} ||= $self->_fill_data}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"data"} ||= $self->_fill_data
MC/DC0%0/2
$self->{"data"} ||= $self->_fill_data
$self->{"data"}
$self->_fill_data
Truth table33%1/3
A ||= B
A= $self->{"data"}B= $self->_fill_data
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub reset { | ||||||||||||||||||||||||||||
| 47 | 4 | ▶ | shift->{pos} = 0; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub _build_cache_key { | ||||||||||||||||||||||||||||
| 51 | 4 | ▶ | my ($class, $storage, $args, $attrs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_cache_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 52 | # compose the query and bind values, like as_query(), | ||||||||||||||||||||||||||||
| 53 | # so the cache key is only affected by what the database sees | ||||||||||||||||||||||||||||
| 54 | # and not any other cruft in $attrs | ||||||||||||||||||||||||||||
| 55 | 4 | my $ref = $storage->_select_args_to_query(@{$args}[0..2], $attrs); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | my $conn; | |||||||||||||||||||||||||||
| 58 | 4 | ▶ | if (! ($conn = $storage->_dbh) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 3 | my $connect_info = $storage->_dbi_connect_info; | |||||||||||||||||||||||||||
| 60 | 3 | ▶ | if (! ref($connect_info->[0]) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 2 | $conn = { Name => $connect_info->[0], Username => $connect_info->[1] }; | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 1 | carp "Invoking connector coderef $connect_info->[0] in order to obtain cache-lookup information"; | |||||||||||||||||||||||||||
| 64 | 1 | $conn = $connect_info->[0]->(); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | local $Storable::canonical = 1; | |||||||||||||||||||||||||||
| 69 | 4 | ▶ | return Digest::SHA::sha1_hex(Storable::nfreeze( [ $ref, $conn->{Name}, $conn->{Username} || '' ] )); | ||||||||||||||||||||||||||
|
Condition50%1/2
$conn->{'Username'} || ''
MC/DC0%0/1
$conn->{'Username'} || ''
$conn->{'Username'}
Truth table50%1/2
A
A= $conn->{'Username'}
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub _fill_data { | ||||||||||||||||||||||||||||
| 74 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fill_data: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 75 | 6 | my $cache = $self->{cache_object}; | |||||||||||||||||||||||||||
| 76 | 6 | my $key = $self->{cache_key}; | |||||||||||||||||||||||||||
| 77 | 6 | ▶ | return $cache->get($key) || do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$cache->get($key) || do {
my $data = [$self->{'inner'}->all];
$cache->set($key, $data, $self->{'cache_for'});
$data
}
MC/DC0%0/2
$cache->get($key) || do {
my $data = [$self->{'inner'}->all];
$cache->set($key, $data, $self->{'cache_for'});
$data
}
$cache->get($key)
do {
my $data = [$self->{'inner'}->all];
$cache->set($key, $data, $self->{'cache_for'});
$data
}
Truth table66%2/3
A || B
A= $cache->get($key)B= do {
my $data = [$self->{'inner'}->all];
$cache->set($key, $data, $self->{'cache_for'});
$data
}
| |||||||||||||||||||||||||||||
| 78 | 4 | my $data = [ $self->{inner}->all ]; | |||||||||||||||||||||||||||
| 79 | 4 | $cache->set($key, $data, $self->{cache_for}); | |||||||||||||||||||||||||||
| 80 | 4 | $data; | |||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub clear_cache { | ||||||||||||||||||||||||||||
| 85 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 2 | $self->{cache_object}->remove($self->{cache_key}); | |||||||||||||||||||||||||||
| 87 | 2 | delete $self->{data}; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 8 | ▶ | sub cache_key { shift->{cache_key} } | ||||||||||||||||||||||||||
|
Subroutine
cache_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | 1; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 NAME | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | DBIx::Class::Cursor::Cached - cursor class with built-in caching support | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | my $schema = SchemaClass->connect( | ||||||||||||||||||||||||||||
| 101 | $dsn, $user, $pass, { cursor_class => 'DBIx::Class::Cursor::Cached' } | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | $schema->default_resultset_attributes({ | ||||||||||||||||||||||||||||
| 105 | cache_object => Cache::FileCache->new({ namespace => 'SchemaClass' }), | ||||||||||||||||||||||||||||
| 106 | }); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | my $rs = $schema->resultset('CD')->search(undef, { cache_for => 300 }); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my @cds = $rs->all; # fills cache | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $rs = $schema->resultset('CD')->search(undef, { cache_for => 300 }); | ||||||||||||||||||||||||||||
| 113 | # refresh resultset | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | @cds = $rs->all; # uses cache, no SQL run | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | $rs->cursor->clear_cache; # deletes data from cache | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | @cds = $rs->all; # refills cache | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Matt S Trout <mst@shadowcat.co.uk> http://www.shadowcat.co.uk/ | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Initial development sponsored by and (c) Takkle, Inc. 2007 | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | This library is free software under the same license as perl itself | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||