| 1 | |||||||||||
| 2 | package Class::DBI::Lite::CacheManager; | ||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Carp 'confess'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Digest::MD5 'md5_hex'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | sub new | ||||||||||
| 10 | 1 | ▶ | { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 11 | 1 | my ($class, %args) = @_; | |||||||||
| 12 | |||||||||||
| 13 | 1 | my %defaults = ( | |||||||||
| 14 | __PACKAGE__->defaults, | ||||||||||
| 15 | $class->defaults | ||||||||||
| 16 | ); | ||||||||||
| 17 | 1 | my %params = ( | |||||||||
| 18 | %defaults, | ||||||||||
| 19 | %args, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 1 | foreach my $arg ( keys %defaults ) | |||||||||
| 23 | { | ||||||||||
| 24 | confess "Required param '$arg' was not provided" | ||||||||||
| 25 | 6 | ▶ | unless defined( $params{$arg} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | }# end foreach() | ||||||||||
| 27 | |||||||||||
| 28 | 1 | my $s = bless \%args, $class; | |||||||||
| 29 | 1 | $s->init(); | |||||||||
| 30 | 1 | $s->auto_setup(); | |||||||||
| 31 | 1 | return $s; | |||||||||
| 32 | }# end new() | ||||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | sub init { } | ||||||||
|
Subroutine
init: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 35 | |||||||||||
| 36 | 1 | ▶ | sub defaults {( | ||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 1 | do_auto_setup => 1, | |||||||||
| 38 | do_cache_retrieve => 1, | ||||||||||
| 39 | do_cache_search => 0, | ||||||||||
| 40 | search_options => [ ], | ||||||||||
| 41 | class => undef | ||||||||||
| 42 | )} | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub do_auto_setup { shift->{do_auto_setup} } | ||||||||
|
Subroutine
do_auto_setup: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | sub do_cache_retrieve { shift->{do_cache_retrieve} } | ||||||||
|
Subroutine
do_cache_retrieve: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 47 | |||||||||||
| 48 | 1 | ▶ | sub do_cache_search { shift->{do_cache_search} } | ||||||||
|
Subroutine
do_cache_search: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | |||||||||||
| 50 | 100020 | ▶ | sub search_options { @{ shift->{search_options} } } | ||||||||
|
Subroutine
search_options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | |||||||||||
| 52 | sub cache_searches_containing | ||||||||||
| 53 | 1 | ▶ | { | ||||||||
|
Subroutine
cache_searches_containing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 54 | 1 | my ($s, @cols) = @_; | |||||||||
| 55 | |||||||||||
| 56 | 1 | my $sig = md5_hex( join ':', sort @cols ); | |||||||||
| 57 | 1 | push @{$s->{search_options}}, $sig; | |||||||||
| 58 | }# end cache_searches_containing() | ||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub class { shift->{class} } | ||||||||
|
Subroutine
class: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 61 | |||||||||||
| 62 | sub set; | ||||||||||
| 63 | |||||||||||
| 64 | sub get; | ||||||||||
| 65 | |||||||||||
| 66 | sub delete; | ||||||||||
| 67 | |||||||||||
| 68 | sub clear; | ||||||||||
| 69 | |||||||||||
| 70 | sub auto_setup | ||||||||||
| 71 | 1 | ▶ | { | ||||||||
|
Subroutine
auto_setup: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||
| 72 | 1 | my $s = shift; | |||||||||
| 73 | |||||||||||
| 74 | 1 | my $class = $s->class; | |||||||||
| 75 | |||||||||||
| 76 | 1 | ▶ | if( $s->do_cache_retrieve ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | { | ||||||||||
| 78 | 0 | ▶ | $class->add_trigger( before_retrieve => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 79 | 0 | my ($s, $id) = @_; | |||||||||
| 80 | 0 | my $key = $s->get_cache_key( $id ); | |||||||||
| 81 | 0 | $class->cache->get( $key ); | |||||||||
| 82 | 0 | }); | |||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | $class->add_trigger( after_retrieve => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 85 | 0 | my $s = shift; | |||||||||
| 86 | 0 | $class->cache->set( $s->get_cache_key => $s->as_hashref ); | |||||||||
| 87 | 0 | }); | |||||||||
| 88 | }# end if() | ||||||||||
| 89 | |||||||||||
| 90 | 1 | ▶ | if( $s->do_cache_search ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | { | ||||||||||
| 92 | 100000 | ▶ | $class->add_trigger( before_search => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 93 | 100000 | my ($s, $params) = @_; | |||||||||
| 94 | |||||||||||
| 95 | 100000 | my $sig = md5_hex(join ':', sort keys %$params); | |||||||||
| 96 | 100000 | ▶ | return unless grep { $_ eq $sig } ( $s->cache->search_options ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | 100000 | my $id = md5_hex( join ':', map { "$_=$params->{$_}" } sort keys %$params ); | |||||||||
| 99 | 100000 | my $key = $s->get_cache_key( $id ); | |||||||||
| 100 | |||||||||||
| 101 | 100000 | ▶ | my $cached = $class->cache->get( $key ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | or return; | ||||||||||
| 103 | |||||||||||
| 104 | 99980 | my @res = grep { $_ } @{ $cached->{data} }; | |||||||||
| 105 | 99980 | ▶ | return unless @res; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 99980 | @res; | |||||||||
| 107 | 1 | }); | |||||||||
| 108 | |||||||||||
| 109 | 20 | ▶ | $class->add_trigger( after_search => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 110 | 20 | my ($s, $params, $result_array) = @_; | |||||||||
| 111 | |||||||||||
| 112 | 20 | my $sig = md5_hex(join ':', sort keys %$params); | |||||||||
| 113 | 20 | ▶ | return unless grep { $_ eq $sig } ( $s->cache->search_options ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | |||||||||||
| 115 | 20 | my $id = md5_hex( join ':', map { "$_=$params->{$_}" } sort keys %$params ); | |||||||||
| 116 | 20 | my $key = $s->get_cache_key( $id ); | |||||||||
| 117 | |||||||||||
| 118 | 20 | my @objects = map { $_->as_hashref } @$result_array; | |||||||||
| 119 | 20 | $class->cache->set( $key => { data => \@objects } ); | |||||||||
| 120 | 1 | }); | |||||||||
| 121 | }# end if() | ||||||||||
| 122 | |||||||||||
| 123 | 10 | ▶ | $class->add_trigger( after_create => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 10 | my $s = shift; | |||||||||
| 125 | 10 | $class->cache->clear(); | |||||||||
| 126 | 1 | }); | |||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | $class->add_trigger( after_update => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 129 | 0 | my $s = shift; | |||||||||
| 130 | 0 | $class->cache->clear(); | |||||||||
| 131 | 1 | }); | |||||||||
| 132 | |||||||||||
| 133 | 11 | ▶ | $class->add_trigger( after_delete => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 134 | 11 | my $s = shift; | |||||||||
| 135 | 11 | $class->cache->clear(); | |||||||||
| 136 | 1 | }); | |||||||||
| 137 | }# end auto_setup() | ||||||||||
| 138 | |||||||||||
| 139 | 2 | 1;# return true: | |||||||||
| 140 | |||||||||||
| 141 | =pod | ||||||||||
| 142 | |||||||||||
| 143 | =head1 NAME | ||||||||||
| 144 | |||||||||||
| 145 | Class::DBI::Lite::CacheManager - Base class for NoSQL cache managers. | ||||||||||
| 146 | |||||||||||
| 147 | =head1 SYNOPSIS | ||||||||||
| 148 | |||||||||||
| 149 | You should not use this class directly - use L<Class::DBI::Lite::CacheManager::Memcached> | ||||||||||
| 150 | or L<Class::DBI::Lite::CacheManager::InMemory>. | ||||||||||
| 151 | |||||||||||
| 152 | B<NOTE:> "NoSQL" is "Not Only SQL" - not "No SQL". | ||||||||||
| 153 | |||||||||||
| 154 | =head1 DESCRIPTION | ||||||||||
| 155 | |||||||||||
| 156 | Many - but not all - database queries can be avoided by using a simple cache system. | ||||||||||
| 157 | |||||||||||
| 158 | The CacheManager extentions for L<Class::DBI::Lite> offer the following features: | ||||||||||
| 159 | |||||||||||
| 160 | =over 4 | ||||||||||
| 161 | |||||||||||
| 162 | =item * B<Up to 10x increase in speed.> | ||||||||||
| 163 | |||||||||||
| 164 | =item * Per-class caching options - specify different cache parameters on a per-class basis. | ||||||||||
| 165 | |||||||||||
| 166 | =item * Reduced load on the database. | ||||||||||
| 167 | |||||||||||
| 168 | =item * Reduced network traffic. | ||||||||||
| 169 | |||||||||||
| 170 | =back | ||||||||||
| 171 | |||||||||||
| 172 | =head1 SEE ALSO | ||||||||||
| 173 | |||||||||||
| 174 | L<Class::DBI::Lite::CacheManager::Memcached> and L<Class::DBI::Lite::CacheManager::InMemory> for | ||||||||||
| 175 | implementation-specific details. | ||||||||||
| 176 | |||||||||||
| 177 | =head1 AUTHOR | ||||||||||
| 178 | |||||||||||
| 179 | Copyright John Drago <jdrago_999@yahoo.com>. All rights reserved. | ||||||||||
| 180 | |||||||||||
| 181 | =head1 LICENSE | ||||||||||
| 182 | |||||||||||
| 183 | This software is B<Free> software and may be used and redistributed under the | ||||||||||
| 184 | same terms as perl itself. | ||||||||||
| 185 | |||||||||||
| 186 | =cut | ||||||||||
| 187 | |||||||||||