DBIx-QuickORM

 view release on metacpan or  search on metacpan

lib/DBIx/QuickORM/Manual/Caching.pm  view on Meta::CPAN

The cache lives on the connection (L<DBIx::QuickORM::Connection>), held by its
row manager (L<DBIx::QuickORM::RowManager>). Two separate connections each have
their own cache, so the same database row loaded through two connections yields
two distinct objects. This is intentional: each connection has its own
transaction state and its own view of the data. See
L<DBIx::QuickORM::Manual::Connections> for the connection lifecycle.

=head1 WEAK REFERENCES: NO LEAKS

Cached rows are held by B<weak> reference. The cache lets you reuse a row while
it is still alive somewhere in your program, but it does not keep rows alive on
its own. Once nothing else references a row, it is garbage collected and its
cache entry disappears. Loading many rows and then dropping them does not grow
the cache without bound.

A consequence: identity is only guaranteed for as long as you hold a reference.
If you drop every reference to a row and fetch it again, you may get a fresh
object - but it will still be the only one, so the one-copy guarantee holds.

=head1 INTERACTION WITH SELECT / INSERT / UPDATE / DELETE

t/AI/cache_fixes.t  view on Meta::CPAN

};

subtest dead_weak_entries_are_purged => sub {
    my $source = $con->source('solo');
    my $sh     = $con->handle('solo');

    my @rows = map { $sh->insert({name => "purge_$_"}) } 1 .. 5;
    my @pks  = map { $_->field('solo_id') } @rows;

    my $bucket = $manager->{cache}{$source->source_orm_name};
    ok((grep { defined $bucket->{$_} } keys %$bucket) >= 5, "all five rows are cached and alive");

    @rows = ();    # Drop the only strong references; weak cache entries go undef.

    my $dead = grep { !defined $bucket->{$_} } keys %$bucket;
    ok($dead >= 5, "dead entries linger in the bucket before a purge");

    # A lookup that hits a dead entry triggers the purge.
    my $miss = $manager->do_cache_lookup($source, undef, [$pks[0]], undef);
    ok(!$miss, "lookup of a garbage-collected row misses");



( run in 1.229 second using v1.01-cache-2.11-cpan-df04353d9ac )