| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | DBIx::SQLEngine::Record::Cache - Avoid Repeated Selects | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | B<Setup:> Several ways to create a class. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my $sqldb = DBIx::SQLEngine->new( ... ); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | $class_name = $sqldb->record_class( $table_name, undef, 'Cache' ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | $sqldb->record_class( $table_name, 'My::Record', 'Cache' ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package My::Record; | ||||||||||||||||||||||||||||
| 16 | use DBIx::SQLEngine::Record::Class '-isasubclass', 'Cache'; | ||||||||||||||||||||||||||||
| 17 | My::Record->table( $sqldb->table($table_name) ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | B<Cache:> Uses Cache::Cache interface. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | $class_name->use_cache_style('simple'); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # requires Cache::FastMemoryCache | ||||||||||||||||||||||||||||
| 24 | $class_name->use_cache_style('active'); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use Cache::Cache; | ||||||||||||||||||||||||||||
| 27 | $class_name->cache_cache( $my_cache_cache_object ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | B<Basics:> Layered over superclass. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Fetches from cache if it's been seen before | ||||||||||||||||||||||||||||
| 32 | $record = $class_name->fetch_record( $primary_key ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Fetches from cache if we've run this query before | ||||||||||||||||||||||||||||
| 35 | @records = $class_name->fetch_select(%clauses)->records; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Clears cache so it's seen by next select query | ||||||||||||||||||||||||||||
| 38 | $record->insert_record(); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Clears cache so it's seen by next select query | ||||||||||||||||||||||||||||
| 41 | $record->update_record(); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Clears cache so it's seen by next select query | ||||||||||||||||||||||||||||
| 44 | $record->delete_record(); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | This package provides a caching layer for DBIx::SQLEngine::Record objects. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Don't use this module directly; instead, pass its name as a trait when you create a new record class. This package provides a multiply-composable collection of functionality for Record classes. It is combined with the base class and other traits by DBIx::SQLEngine::Record::Class. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ######################################################################## | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | package DBIx::SQLEngine::Record::Cache; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | use Storable 'freeze'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ######################################################################## | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ######################################################################## | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 CACHE INTERFACE | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | ######################################################################## | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 Cache Configuration | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =over 4 | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item cache_cache() | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | $record_class->cache_cache() : $cache_cache | ||||||||||||||||||||||||||||
| 81 | $record_class->cache_cache( $cache_cache ) | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Gets or sets the cache object associated with this record class. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | If no cache has been set for a given class, no caching is performed. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =back | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | B<Cache Object Requirements:> This package in intended to work with cache object that use the Cache::Cache interface. However, any package which support the limited cache interface used by this package should be sufficient. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Two small classes are included that support this interface; see L<DBIx::SQLEngine::Cache::TrivialCache> and L<DBIx::SQLEngine::Cache::BasicCache>. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | The following methods are used | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =over 4 | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Constructor. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =item get_namespace() | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Used to differentiate one cache object from another. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =item get() | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Fetch a value from the cache, if it is present. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item set() | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Set a value in the cache. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =item clear() | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Clear some or all values in the cache. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =back | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | use Class::MakeMethods ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 1 | 'Template::ClassInherit:scalar' => 'cache_cache', | |||||||||||||||||||||||||||
| 121 | 1 | ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | ######################################################################## | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head2 Cache Operations | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =over 4 | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =item cache_key() | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | $record_class->cache_key( $key ) : $string_value | ||||||||||||||||||||||||||||
| 132 | $record_class->cache_key( \@key ) : $string_value | ||||||||||||||||||||||||||||
| 133 | $record_class->cache_key( \%key ) : $string_value | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | Returns the string value to be used as a cache key. The argument may be an existing string, a reference to a shallow array whose elements will be joined with "\0/\0", or any other reference value which will be stringified by Storable. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =item cache_get() | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | $record_class->cache_get( $key ) : $value | ||||||||||||||||||||||||||||
| 140 | $record_class->cache_get( $key ) : ( $value, $updater_code_ref ) | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Returns the cached value associated with this key, if any. If called in a list context, also returns a reference to a subroutine which will save a new value for that key. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item cache_set() | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | $record_class->cache_set( $key, $value ) | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Caches this value under the provided key. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item cache_get_set() | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | $record_class->cache_get_set( $key, $code_ref, @args ) : $value | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Returns the curent value provided by a cache_get on the provided key, or if it is undefined, invokes the subroutine reference with any additional arguments provided, and saves the subroutine's return value as the cached value. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item cache_clear() | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | $record_class->cache_clear() | ||||||||||||||||||||||||||||
| 159 | $record_class->cache_clear( $key ) | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Clear all values from the cache, or just those associated with the given key. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =back | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 7 | ▶ | sub cache_key { | ||||||||||||||||||||||||||
|
Subroutine
cache_key: called CC=3 SCAR=13.5
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 7 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 169 | 7 | my $type = ref($key); | |||||||||||||||||||||||||||
| 170 | 7 | ▶ | if ( ! $type ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 171 | 0 | $key | |||||||||||||||||||||||||||
| 172 | } elsif ( $type eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 173 | 7 | join("\0/\0", @$key) | |||||||||||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||||||||||
| 175 | 0 | local $Storable::canonical = 1; | |||||||||||||||||||||||||||
| 176 | 0 | freeze($key) | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # $value = $self->cache_get( $key ); | ||||||||||||||||||||||||||||
| 181 | # ( $value, $update ) = $self->cache_get( $key ); | ||||||||||||||||||||||||||||
| 182 | 7 | ▶ | sub cache_get { | ||||||||||||||||||||||||||
|
Subroutine
cache_get: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | 7 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 7 | ▶ | my $cache = $self->cache_cache() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 7 | ▶ | $key = $self->cache_key($key) if ( ref $key ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 7 | my $current = $cache->get( $key ); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 7 | ▶ | if ( ! defined $current ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 4 | $self->cache_log_operation( $cache, 'miss', $key ); | |||||||||||||||||||||||||||
| 192 | } else { | ||||||||||||||||||||||||||||
| 193 | 3 | $self->cache_log_operation( $cache, 'hit', $key ); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 4 | ▶ | ! wantarray ? $current : ( $current, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 197 | 4 | $self->cache_log_operation( $cache, 'update', $key ); | |||||||||||||||||||||||||||
| 198 | 4 | $cache->set( $key, @_ ); | |||||||||||||||||||||||||||
| 199 | 7 | ▶ | } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # $self->cache_set( $key, $value ); | ||||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub cache_set { | ||||||||||||||||||||||||||
|
Subroutine
cache_set: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | 0 | my ( $self, $key, @value ) = @_; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | my $cache = $self->cache_cache() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | $key = $self->cache_key($key) if ( ref $key ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | $self->cache_log_operation( $cache, 'write', $key ); | |||||||||||||||||||||||||||
| 211 | 0 | $cache->set( $key, @value ); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # $value = $self->cache_get_set( $key, \&sub, @args ); | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | sub cache_get_set { | ||||||||||||||||||||||||||
|
Subroutine
cache_get_set: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 0 | my ( $self, $key, $sub, @args ) = @_; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | my ($current, $update) = $self->cache_get($key); | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 0 | ▶ | if ( ! defined $current ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | $current = &$sub( @args ); | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | &$update( defined($current) ? $current : '' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | $current; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # $self->cache_clear(); | ||||||||||||||||||||||||||||
| 229 | # $self->cache_clear( $key ); | ||||||||||||||||||||||||||||
| 230 | 3 | ▶ | sub cache_clear { | ||||||||||||||||||||||||||
|
Subroutine
cache_clear: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | 3 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 3 | ▶ | my $cache = $self->cache_cache() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 3 | ▶ | if ( ! $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 3 | $self->cache_log_operation( $cache, 'clear' ); | |||||||||||||||||||||||||||
| 237 | 3 | $cache->clear(); | |||||||||||||||||||||||||||
| 238 | } else { | ||||||||||||||||||||||||||||
| 239 | 0 | $self->cache_log_operation( $cache, 'clear', $key ); | |||||||||||||||||||||||||||
| 240 | 0 | $cache->set($key, undef); | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | ######################################################################## | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head2 Cache Logging | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =over 4 | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =item CacheLogging() | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | $record_class->CacheLogging() : $level | ||||||||||||||||||||||||||||
| 253 | $record_class->CacheLogging( $level ) | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Sets the logging level associated with a given class. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =item cache_log_operation() | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | $record_class->cache_log_operation( $cache, $operation, $key ) | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Does nothing unless a CacheLogging level is set for this class. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Uses warn() to print a message to the error log, including the key string used, and the operation, which will be one of "hit", "miss", "write", and "clear". | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | If the level is greater than one, the message will also include a history of prior operations on this key. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =back | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =cut | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 1 | ▶ | use Class::MakeMethods ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 272 | 1 | 'Template::ClassInherit:scalar' => 'CacheLogging', | |||||||||||||||||||||||||||
| 273 | 1 | ); | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 1 | ▶ | use vars qw( %CachingHistory ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 14 | ▶ | sub cache_log_operation { | ||||||||||||||||||||||||||
|
Subroutine
cache_log_operation: called CC=4 SCAR=24.6
Pod
documented
| |||||||||||||||||||||||||||||
| 278 | 14 | my ( $self, $cache, $oper, $key ) = @_; | |||||||||||||||||||||||||||
| 279 | 14 | ▶ | my $level = $self->CacheLogging() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 0 | my $namespace = $cache->get_namespace; | |||||||||||||||||||||||||||
| 281 | 0 | ▶ | if ( $level > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | ▶ | my $history = ( $CachingHistory{ $key } ||= [] ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$DBIx::SQLEngine::Record::Cache::CachingHistory{$key} ||= []
MC/DC0%0/1
$DBIx::SQLEngine::Record::Cache::CachingHistory{$key} ||= []
$DBIx::SQLEngine::Record::Cache::CachingHistory{$key}
Truth table0%0/2
A
A= $DBIx::SQLEngine::Record::Cache::CachingHistory{$key}
| |||||||||||||||||||||||||||||
| 283 | 0 | $oper .= " (" . join(' ', @$history ) . ")"; | |||||||||||||||||||||||||||
| 284 | 0 | push @$history, $oper; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | 0 | warn "Cache $namespace: $oper " . DBIx::SQLEngine::printable($key) . "\n"; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | ######################################################################## | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =head2 Cache Styles | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =over 4 | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =item define_cache_styles() | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | DBIx::SQLEngine->define_cache_styles( $name, $code_ref ) | ||||||||||||||||||||||||||||
| 298 | DBIx::SQLEngine->define_cache_styles( %names_and_code_refs ) | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | Define a named caching style. The code ref supplied for each name should create and return an object from the Cache::Cache hierarchy, or another caching class which supports the interface described in the "Cache Object Requirements" section above. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =item cache_styles() | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | DBIx::SQLEngine->cache_styles() : %names_and_info | ||||||||||||||||||||||||||||
| 305 | DBIx::SQLEngine->cache_styles( $name ) : $info | ||||||||||||||||||||||||||||
| 306 | DBIx::SQLEngine->cache_styles( \@names ) : @info | ||||||||||||||||||||||||||||
| 307 | DBIx::SQLEngine->cache_styles( $name, $info, ... ) | ||||||||||||||||||||||||||||
| 308 | DBIx::SQLEngine->cache_styles( \%names_and_info ) | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | Accessor for global hash mapping cache names to initialization subroutines. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =item use_cache_style() | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | $class_name->use_cache_style( $cache_style_name ) | ||||||||||||||||||||||||||||
| 315 | $class_name->use_cache_style( $cache_style_name, @options ) | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | Uses the named caching definition to create a new cache object, and associates it with the given class. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | Use one of the predefined caching styles described in the "Default Caching Styles" section below, or define your own cache styles with define_cache_styles. | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =back | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =cut | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 1 | ▶ | use Class::MakeMethods ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 326 | 1 | 'Standard::Global:hash' => 'cache_styles', | |||||||||||||||||||||||||||
| 327 | 1 | ); | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 1 | ▶ | sub define_cache_styles { | ||||||||||||||||||||||||||
|
Subroutine
define_cache_styles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 330 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 331 | 1 | $self->cache_styles( @_ ); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 1 | ▶ | sub use_cache_style { | ||||||||||||||||||||||||||
|
Subroutine
use_cache_style: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 335 | 1 | my ( $class, $style, %options ) = @_; | |||||||||||||||||||||||||||
| 336 | 1 | my $sub = $class->cache_styles( $style ); | |||||||||||||||||||||||||||
| 337 | 1 | my $cache = $sub->( $class, %options ); | |||||||||||||||||||||||||||
| 338 | 1 | $class->cache_cache( $cache ); | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | ######################################################################## | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =pod | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | B<Default Caching Styles:> The following cache styles are predefined. Except for 'simple', using any of these styles will require installation of the Cache::Cache distribution. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =over 4 | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =item 'simple' | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Uses DBIx::SQLEngine::Cache::TrivialCache. | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =item 'live' | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | Uses Cache::FastMemoryCache with a default expiration time of 1 seconds. | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =item 'active' | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | Uses Cache::FastMemoryCache with a default expiration time of 5 seconds. | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =item 'stable' | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Uses Cache::FastMemoryCache with a default expiration time of 30 seconds. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =item 'file' | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | Uses Cache::FileCache with a default expiration time of 30 seconds. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =back | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | B<Examples:> | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | =over 2 | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =item * | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # requires DBIx::SQLEngine::Cache::TrivialCache | ||||||||||||||||||||||||||||
| 378 | $class_name->use_cache_style('simple'); | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =item * | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # requires Cache::FastMemoryCache from CPAN | ||||||||||||||||||||||||||||
| 383 | $class_name->use_cache_style('active'); | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =back | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =cut | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | __PACKAGE__->define_cache_styles( | ||||||||||||||||||||||||||||
| 390 | 1 | ▶ | 'simple' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 391 | 1 | require DBIx::SQLEngine::Cache::TrivialCache; | |||||||||||||||||||||||||||
| 392 | 1 | DBIx::SQLEngine::Cache::TrivialCache->new(); | |||||||||||||||||||||||||||
| 393 | }, | ||||||||||||||||||||||||||||
| 394 | 0 | ▶ | 'live' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 395 | 0 | require Cache::FastMemoryCache; | |||||||||||||||||||||||||||
| 396 | 0 | Cache::FastMemoryCache->new( { | |||||||||||||||||||||||||||
| 397 | 'namespace' => 'RecordCache:' . (shift), | ||||||||||||||||||||||||||||
| 398 | 'default_expires_in' => 1, | ||||||||||||||||||||||||||||
| 399 | 'auto_purge_interval' => 10, | ||||||||||||||||||||||||||||
| 400 | @_ | ||||||||||||||||||||||||||||
| 401 | } ) | ||||||||||||||||||||||||||||
| 402 | }, | ||||||||||||||||||||||||||||
| 403 | 0 | ▶ | 'active' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 404 | 0 | require Cache::FastMemoryCache; | |||||||||||||||||||||||||||
| 405 | 0 | Cache::FastMemoryCache->new( { | |||||||||||||||||||||||||||
| 406 | 'namespace' => 'RecordCache:' . (shift), | ||||||||||||||||||||||||||||
| 407 | 'default_expires_in' => 5, | ||||||||||||||||||||||||||||
| 408 | 'auto_purge_interval' => 60, | ||||||||||||||||||||||||||||
| 409 | @_ | ||||||||||||||||||||||||||||
| 410 | } ) | ||||||||||||||||||||||||||||
| 411 | }, | ||||||||||||||||||||||||||||
| 412 | 0 | ▶ | 'stable' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 413 | 0 | require Cache::FastMemoryCache; | |||||||||||||||||||||||||||
| 414 | 0 | Cache::FastMemoryCache->new( { | |||||||||||||||||||||||||||
| 415 | 'namespace' => 'RecordCache:' . (shift), | ||||||||||||||||||||||||||||
| 416 | 'default_expires_in' => 30, | ||||||||||||||||||||||||||||
| 417 | 'auto_purge_interval' => 60, | ||||||||||||||||||||||||||||
| 418 | @_ | ||||||||||||||||||||||||||||
| 419 | } ) | ||||||||||||||||||||||||||||
| 420 | }, | ||||||||||||||||||||||||||||
| 421 | 0 | ▶ | 'file' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 422 | 0 | require Cache::FileCache; | |||||||||||||||||||||||||||
| 423 | 0 | Cache::FileCache->new( { | |||||||||||||||||||||||||||
| 424 | 'namespace' => 'RecordCache:' . (shift), | ||||||||||||||||||||||||||||
| 425 | 'default_expires_in' => 30, | ||||||||||||||||||||||||||||
| 426 | 'auto_purge_interval' => 60, | ||||||||||||||||||||||||||||
| 427 | @_ | ||||||||||||||||||||||||||||
| 428 | } ) | ||||||||||||||||||||||||||||
| 429 | }, | ||||||||||||||||||||||||||||
| 430 | 1 | ); | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | ######################################################################## | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | ######################################################################## | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =head1 FETCHING DATA (SQL DQL) | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | Each of these methods provides a cached version of the superclass method. | ||||||||||||||||||||||||||||
| 439 | The results of queries are cached based on the SQL statement and parameters used. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =over 4 | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =item fetch_select() | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | $class_name->fetch_select( %select_clauses ) : $record_set | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | Retrives records from the table using the provided SQL select clauses. | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =item fetch_one_record() | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | $sqldb->fetch_one_record( %select_clauses ) : $record_hash | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Retrives one record from the table using the provided SQL select clauses. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =item select_record() | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | $class_name->select_record ( $primary_key_value ) : $record_obj | ||||||||||||||||||||||||||||
| 458 | $class_name->select_record ( \@compound_primary_key ) : $record_obj | ||||||||||||||||||||||||||||
| 459 | $class_name->select_record ( \%hash_with_primary_key_value ) : $record_obj | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | Fetches a single record by primary key. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =item select_records() | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | $class_name->select_records ( @primary_key_values_or_hashrefs ) : $record_set | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | Fetches a set of one or more records by primary key. | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =item visit_select() | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | $class_name->visit_select ( $sub_ref, %select_clauses ) : @results | ||||||||||||||||||||||||||||
| 472 | $class_name->visit_select ( %select_clauses, $sub_ref ) : @results | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | Calls the provided subroutine on each matching record as it is retrieved. Returns the accumulated results of each subroutine call (in list context). | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | To Do: This could perform caching of the matched records, but currently does not. | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | =back | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | The conversion of select clauses to a SQL statement is performed by the sql_select method: | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =over 4 | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =item sql_select() | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | $class_name->sql_select ( %sql_clauses ) : $sql_stmt, @params | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | Uses the table to call the sql_select method on the current SQLEngine driver. | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | =back | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =cut | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | # $records = $record_class->fetch_select( %select_clauses ); | ||||||||||||||||||||||||||||
| 495 | 7 | ▶ | sub fetch_select { | ||||||||||||||||||||||||||
|
Subroutine
fetch_select: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 496 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 497 | 7 | my %clauses = @_; | |||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 7 | my @sql = $self->sql_select( %clauses ); | |||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | 7 | my ($records, $update) = $self->cache_get( \@sql ); | |||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 7 | ▶ | if ( ! defined $records ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | 4 | $records = $self->NEXT('fetch_select', sql => \@sql ); | |||||||||||||||||||||||||||
| 505 | 4 | ▶ | $update->( $records ) if ( $update and $records ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$update and $records
MC/DC0%0/2
$update and $records
$update
$records
Truth table33%1/3
A and B
A= $updateB= $records
| |||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | 7 | return $records; | |||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 2 | ▶ | sub fetch_one_record { | ||||||||||||||||||||||||||
|
Subroutine
fetch_one_record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 512 | 2 | local $SIG{__DIE__} = \&Carp::confess; | |||||||||||||||||||||||||||
| 513 | 2 | (shift)->fetch_select( @_, 'limit' => 1 )->record( 0 ) | |||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | # @results = $self->visit_select( %select_clauses, $sub ); | ||||||||||||||||||||||||||||
| 517 | 0 | ▶ | sub visit_select { | ||||||||||||||||||||||||||
|
Subroutine
visit_select: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 518 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 519 | 0 | ▶ | my $sub = ( ref($_[0]) ? shift : pop ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | 0 | my %clauses = @_; | |||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | 0 | my @sql = $self->sql_select( %clauses ); | |||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 0 | my ($records, $update) = $self->cache_get( \@sql ); | |||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | 0 | ▶ | if ( $records ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | 0 | return map &$sub( $_ ), @$records; | |||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | 0 | $self->sqlengine_do('visit_select', @_, $sub ) | |||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | ######################################################################## | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 7 | ▶ | sub sql_select { | ||||||||||||||||||||||||||
|
Subroutine
sql_select: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 535 | 7 | (shift)->get_table->sqlengine_do( 'sql_select', @_ ); | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | ######################################################################## | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =head2 Vivifying Records From The Database | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | These methods are called internally by the various select methods and do not need to be called directly. | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =over 4 | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | =item record_from_db_data() | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | $class_name->record_from_db_data( $hash_ref ) | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | Calls SUPER method, then cache_records(). | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | =item record_set_from_db_data() | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | $class_name->record_set_from_db_data( $hash_array_ref ) | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | Calls SUPER method, then cache_records(). | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | =item cache_records() | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | $class_name->cache_records( @records ) | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | Adds records to the cache. | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | =back | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | =cut | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | # $record_class->record_from_db_data( $hash_ref ); | ||||||||||||||||||||||||||||
| 569 | 0 | ▶ | sub record_from_db_data { | ||||||||||||||||||||||||||
|
Subroutine
record_from_db_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 570 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 571 | 0 | my $record = $self->NEXT('record_from_db_data', @_ ); | |||||||||||||||||||||||||||
| 572 | 0 | $self->cache_records( $record ); | |||||||||||||||||||||||||||
| 573 | 0 | $record; | |||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | # $record_class->record_set_from_db_data( $hash_array_ref ); | ||||||||||||||||||||||||||||
| 577 | 4 | ▶ | sub record_set_from_db_data { | ||||||||||||||||||||||||||
|
Subroutine
record_set_from_db_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 578 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 579 | 4 | my $recordset = $self->NEXT('record_set_from_db_data', @_ ); | |||||||||||||||||||||||||||
| 580 | 4 | $self->cache_records( @$recordset ); | |||||||||||||||||||||||||||
| 581 | 4 | $recordset; | |||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 4 | ▶ | sub cache_records { | ||||||||||||||||||||||||||
|
Subroutine
cache_records: called CC=2 SCAR=10.1
Pod
documented
| |||||||||||||||||||||||||||||
| 585 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 586 | 4 | my $id_col = $self->column_primary_name(); | |||||||||||||||||||||||||||
| 587 | 4 | foreach my $record ( @_ ) { | |||||||||||||||||||||||||||
| 588 | 0 | my $tablename = $self->table->name; | |||||||||||||||||||||||||||
| 589 | 0 | my $criteria = { $id_col => $record->{ $id_col } }; | |||||||||||||||||||||||||||
| 590 | 0 | my %index = ( where => { $id_col => $record->{ $id_col } }, limit => 1, table => $self->table->name ); | |||||||||||||||||||||||||||
| 591 | 0 | $self->cache_set( \%index, DBIx::SQLEngine::Record::Set->new($record) ); | |||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | ######################################################################## | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | ######################################################################## | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | =head1 EDITING DATA (SQL DML) | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | =head2 Insert to Add Records | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | After constructing a record with one of the new_*() methods, you may save any changes by calling insert_record. | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =over 4 | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =item insert_record | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | $record_obj->insert_record() : $flag | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | Attempt to insert the record into the database. Calls SUPER method, so implemented using MIXIN. | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | Clears the cache. | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | =back | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | =cut | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | # $record->insert_record() | ||||||||||||||||||||||||||||
| 620 | 1 | ▶ | sub insert_record { | ||||||||||||||||||||||||||
|
Subroutine
insert_record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 621 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 622 | 1 | $self->cache_clear(); | |||||||||||||||||||||||||||
| 623 | 1 | $self->NEXT('insert_record', @_ ); | |||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | ######################################################################## | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | =head2 Update to Change Records | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | After retrieving a record with one of the fetch methods, you may save any changes by calling update_record. | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | =over 4 | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | =item update_record | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | $record_obj->update_record() : $record_count | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | Attempts to update the record using its primary key as a unique identifier. | ||||||||||||||||||||||||||||
| 639 | Calls SUPER method, so implemented using MIXIN. | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | Clears the cache. | ||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | =back | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | =cut | ||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | # $record->update_record() | ||||||||||||||||||||||||||||
| 648 | 1 | ▶ | sub update_record { | ||||||||||||||||||||||||||
|
Subroutine
update_record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 649 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 650 | 1 | $self->cache_clear(); | |||||||||||||||||||||||||||
| 651 | 1 | $self->NEXT('update_record', @_ ); | |||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | ######################################################################## | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | =head2 Delete to Remove Records | ||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||
| 658 | =over 4 | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | =item delete_record() | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | $record_obj->delete_record() : $record_count | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | Delete this existing record based on its primary key. | ||||||||||||||||||||||||||||
| 665 | Calls SUPER method, so implemented using MIXIN. | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | Clears the cache. | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | =back | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | =cut | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | # $record->delete_record() | ||||||||||||||||||||||||||||
| 674 | 1 | ▶ | sub delete_record { | ||||||||||||||||||||||||||
|
Subroutine
delete_record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 675 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 676 | 1 | $self->cache_clear(); | |||||||||||||||||||||||||||
| 677 | 1 | $self->NEXT('delete_record', @_ ); | |||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | ######################################################################## | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | ######################################################################## | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | For more about the Record classes, see L<DBIx::SQLEngine::Record::Class>. | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | See L<DBIx::SQLEngine> for the overall interface and developer documentation. | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | See L<DBIx::SQLEngine::Docs::ReadMe> for general information about | ||||||||||||||||||||||||||||
| 691 | this distribution, including installation and license information. | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | =cut | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | ######################################################################## | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | 1 | 1; | |||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | __END__ | ||||||||||||||||||||||||||||