Class-DBI
view release on metacpan or search on metacpan
Now $artist1, $artist2, and $artist3 all point to the same object. If
you update a property on one of them, all of them will reflect the
update.
This is implemented using a simple object lookup index for all live
objects in memory. It is not a traditional cache - when your objects go
out of scope, they will be destroyed normally, and a future retrieve
will instantiate an entirely new object.
The ability to perform this magic for you replies on your perl having
access to the Scalar::Util::weaken function. Although this is part of
the core perl distribution, some vendors do not compile support for it.
To find out if your perl has support for it, you can run this on the
command line:
perl -e 'use Scalar::Util qw(weaken)'
If you get an error message about weak references not being implemented,
Class::DBI will not maintain this lookup index, but give you a separate
instances for each retrieve.
A few new tools are offered for adjusting the behavior of the object
index. These are still somewhat experimental and may change in a future
release.
remove_from_object_index
lib/Class/DBI.pm view on Meta::CPAN
use List::Util;
use Clone ();
use UNIVERSAL::moniker;
use vars qw($Weaken_Is_Available);
BEGIN {
$Weaken_Is_Available = 1;
eval {
require Scalar::Util;
import Scalar::Util qw(weaken);
};
if ($@) {
$Weaken_Is_Available = 0;
}
}
use overload
'""' => sub { shift->stringify_self },
bool => sub { not shift->_undefined_primary },
fallback => 1;
lib/Class/DBI.pm view on Meta::CPAN
return $Live_Objects{$key} || $class->_fresh_init($key => $data);
}
sub _fresh_init {
my ($class, $key, $data) = @_;
my $obj = bless {}, $class;
$obj->_attribute_store(%$data);
# don't store it unless all keys are present
if ($key && $Weaken_Is_Available) {
weaken($Live_Objects{$key} = $obj);
# time to clean up your room?
$class->purge_dead_from_object_index
if ++$Init_Count % $class->purge_object_index_every == 0;
}
return $obj;
}
sub _live_object_key {
my ($me, $data) = @_;
lib/Class/DBI.pm view on Meta::CPAN
Now $artist1, $artist2, and $artist3 all point to the same object. If you
update a property on one of them, all of them will reflect the update.
This is implemented using a simple object lookup index for all live
objects in memory. It is not a traditional cache - when your objects
go out of scope, they will be destroyed normally, and a future retrieve
will instantiate an entirely new object.
The ability to perform this magic for you replies on your perl having
access to the Scalar::Util::weaken function. Although this is part of
the core perl distribution, some vendors do not compile support for it.
To find out if your perl has support for it, you can run this on the
command line:
perl -e 'use Scalar::Util qw(weaken)'
If you get an error message about weak references not being implemented,
Class::DBI will not maintain this lookup index, but give you a separate
instances for each retrieve.
A few new tools are offered for adjusting the behavior of the object
index. These are still somewhat experimental and may change in a
future release.
=head2 remove_from_object_index
t/02-Film.t view on Meta::CPAN
isa_ok $byebye, 'DeletingFilm';
isa_ok $byebye, 'Film';
ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again");
}
my $film;
eval { $film = Film->retrieve('Goodbye Norma Jean') };
ok !$film, "It destroys itself";
}
SKIP: {
skip "Scalar::Util::weaken not available", 3
if !$Class::DBI::Weaken_Is_Available;
# my bad taste is your bad taste
my $btaste = Film->retrieve('Bad Taste');
my $btaste2 = Film->retrieve('Bad Taste');
is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
"Retrieving twice gives ref to same object";
$btaste2->remove_from_object_index;
my $btaste3 = Film->retrieve('Bad Taste');
( run in 0.990 second using v1.01-cache-2.11-cpan-709fd43a63f )