Skip to content

Commit

Permalink
Add cache_hits method
Browse files Browse the repository at this point in the history
  • Loading branch information
nwellnhof committed Aug 24, 2012
1 parent 4da3df5 commit 23e95ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
24 changes: 22 additions & 2 deletions lib/XML/LibXML/Cache/Base.pm
Expand Up @@ -19,17 +19,29 @@ my $deps_found;
sub new { sub new {
my $class = shift; my $class = shift;


my $self = { cache => {} }; my $self = {
cache => {},
hits => 0,
};


return bless($self, $class); return bless($self, $class);
} }


sub cache_hits {
my $self = shift;

return $self->{hits};
}

sub _cache_lookup { sub _cache_lookup {
my ($self, $filename, $get_item) = @_; my ($self, $filename, $get_item) = @_;


my $item = $self->_cache_read($filename); my $item = $self->_cache_read($filename);


return $item if $item; if ($item) {
++$self->{hits};
return $item;
}


$deps_found = {}; $deps_found = {};


Expand Down Expand Up @@ -120,5 +132,13 @@ Base class for the document and style sheet caches.
=head2 new =head2 new
Only used by subclasses.
=head2 cache_hits
my $hits = $cache->cache_hits;
Return the number of cache hits.
=cut =cut
11 changes: 10 additions & 1 deletion t/xml.t
Expand Up @@ -2,7 +2,7 @@
use strict; use strict;


use File::Touch; use File::Touch;
use Test::More tests => 11; use Test::More tests => 14;
use Test::Deep; use Test::Deep;


$ENV{XML_CATALOG_FILES} = ''; $ENV{XML_CATALOG_FILES} = '';
Expand All @@ -26,6 +26,9 @@ isa_ok($cached_rec, 'ARRAY');
my ($cached_doc, $deps) = @$cached_rec; my ($cached_doc, $deps) = @$cached_rec;
is($cached_doc, $doc, 'cached doc'); is($cached_doc, $doc, 'cached doc');


my $hits = $cache->cache_hits;
is($hits, 0, 'cache hits before');

my $number = re(qr/^\d+\z/); my $number = re(qr/^\d+\z/);
my $numbers = [ $number, $number ]; my $numbers = [ $number, $number ];


Expand All @@ -50,9 +53,15 @@ cmp_deeply(
$cached_doc = $cache->parse_file($filename); $cached_doc = $cache->parse_file($filename);
is(int($cached_doc), int($doc), 'cached doc'); is(int($cached_doc), int($doc), 'cached doc');


$hits = $cache->cache_hits;
is($hits, 1, 'cache hits after');

$ref = File::Touch->new(mtime => $time, no_create => 1); $ref = File::Touch->new(mtime => $time, no_create => 1);
$ref->touch($entity_filename); $ref->touch($entity_filename);


my $new_doc = $cache->parse_file($filename); my $new_doc = $cache->parse_file($filename);
isnt(int($new_doc), int($doc), 'new doc'); isnt(int($new_doc), int($doc), 'new doc');


$hits = $cache->cache_hits;
is($hits, 1, 'cache hits after');

0 comments on commit 23e95ca

Please sign in to comment.