Skip to content

Commit

Permalink
use Cache::LRU in Session::Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisuke Oishi committed Dec 24, 2010
1 parent 7ba5da9 commit e2887af
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 31 deletions.
1 change: 1 addition & 0 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ recommends:
Log::Any::Adapter::Dispatch: 0
Mouse: 0.42
requires:
Cache::LRU: 0
Capture::Tiny: 0
Carp::Clan: 0
Class::Data::Inheritable: 0
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ requires 'Sub::Exporter';
requires 'Capture::Tiny';
requires 'Data::Dump';
requires 'Module::Find';
requires 'Cache::LRU';

build_requires 'Test::More' => 0.88;
test_requires 'Test::Builder' => 0.33;
Expand Down
5 changes: 2 additions & 3 deletions lib/DBIx/ObjectMapper/Session.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ sub new {
type => OBJECT,
callbacks => {
'ducktype' => sub {
( grep { $_[0]->can($_) } qw(get set remove clear) )
== 4;
}
( grep { $_[0]->can($_) } qw(get set remove) ) == 3;
}
},
default => DBIx::ObjectMapper::Session::Cache->new()
},
Expand Down
25 changes: 1 addition & 24 deletions lib/DBIx/ObjectMapper/Session/Cache.pm
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
package DBIx::ObjectMapper::Session::Cache;
use strict;
use warnings;
use Scalar::Util qw(weaken);
use base 'Cache::LRU';

our $weaken = 1;

sub new { bless +{}, $_[0] }

sub set {
$_[0]->{$_[1]} = $_[2];
weaken($_[0]->{$_[1]}) if $weaken;
$_[2];
}

sub get { $_[0]->{$_[1]} }

sub remove { delete $_[0]->{$_[1]} }

sub clear {
my $self = shift;
$self->{$_} = undef for keys %$self;
}

sub DESTROY {
my $self = shift;
warn "DESTROY $self" if $ENV{MAPPER_DEBUG};
}

1;

Expand Down
7 changes: 3 additions & 4 deletions t/05_engine/011_dbi_query_cache.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use Test::Exception;
use DBIx::ObjectMapper::Engine;
use DBIx::ObjectMapper::Engine::DBI;
use DBIx::ObjectMapper::Session::Cache;
$DBIx::ObjectMapper::Session::Cache::weaken = 0;

ok my $engine = DBIx::ObjectMapper::Engine::DBI->new(
[
Expand Down Expand Up @@ -59,7 +58,7 @@ sub get_it {
get_it('DBIx::ObjectMapper::Engine::DBI::Iterator');
get_it('DBIx::ObjectMapper::Iterator') for 1 .. 3;
is $engine->{sql_cnt}, 13;
$engine->cache->clear;
$engine->{cache} = DBIx::ObjectMapper::Session::Cache->new;
};

{
Expand All @@ -71,7 +70,7 @@ sub get_it {
{
get_it('DBIx::ObjectMapper::Iterator');
is $engine->{sql_cnt}, 14;
$engine->cache->clear;
$engine->{cache} = DBIx::ObjectMapper::Session::Cache->new;
};

{
Expand All @@ -82,7 +81,7 @@ sub get_it {

get_it('DBIx::ObjectMapper::Iterator');
is $engine->{sql_cnt}, 15;
$engine->cache->clear;
$engine->{cache} = DBIx::ObjectMapper::Session::Cache->new;
};

done_testing;

0 comments on commit e2887af

Please sign in to comment.