Skip to content

Commit

Permalink
Improved handling of caching functionas that return undef
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Sep 20, 2019
1 parent 564814d commit cba9f7d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
Revision history for Class::Simple::Cached

0.03
Improved handling of caching functionas that return undef

0.02 Thu 19 Sep 19:30:43 EDT 2019
Fix working with arrays

Expand Down
12 changes: 11 additions & 1 deletion lib/Class/Simple/Cached.pm
Expand Up @@ -105,6 +105,9 @@ sub AUTOLOAD {
if(ref($rc) eq 'ARRAY') {
return @{$rc};
}
if($rc eq __PACKAGE__ . ">UNDEF<") {
return;
}
return $rc;
}
if(wantarray) {
Expand All @@ -115,7 +118,12 @@ sub AUTOLOAD {
$cache->set($param, \@rc, 'never');
return @rc;
}
return $cache->set($param, $object->$func(), 'never');
my $rc = $object->$func();
if(!defined($rc)) {
$cache->set($param, __PACKAGE__ . ">UNDEF<", 'never');
return;
}
return $cache->set($param, $rc, 'never');
}

# $param = "SUPER::$param";
Expand All @@ -137,6 +145,8 @@ Nigel Horne, C<< <njh at bandsman.co.uk> >>
=head1 BUGS
Doesn't work with L<Memoize>.
Please report any bugs or feature requests to C<bug-class-simple-cached at rt.cpan.org>,
or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Simple-Cached>.
Expand Down
8 changes: 7 additions & 1 deletion t/class.t
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::Most tests => 17;
use Test::Most tests => 19;
use Test::NoWarnings;
use CHI;

Expand All @@ -29,6 +29,9 @@ CLASS: {
ok(scalar(@a) == 1);
ok($a[0] eq 'a');

ok(!defined($l->empty()));
ok(!defined($l->empty()));

# White box test the cache
ok($cache->get('barney') eq 'betty');
my $a = $cache->get('a');
Expand Down Expand Up @@ -71,6 +74,9 @@ sub a {
return 'a';
}

sub empty {
}

sub calls {
my $self = shift;

Expand Down

0 comments on commit cba9f7d

Please sign in to comment.