Skip to content

Commit

Permalink
Fix working with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Sep 19, 2019
1 parent ef67337 commit d4e4c67
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,4 +1,7 @@
Revision history for Class::Simple::Cached

0.02
Fix working with arrays

0.01 Wed 18 Sep 09:23:11 EDT 2019
First Version
34 changes: 23 additions & 11 deletions lib/Class/Simple/Cached.pm
Expand Up @@ -33,7 +33,7 @@ for example by changing its state.
Creates a Class::Simple::Cached object.
It takes one manadatory parameter: cache,
It takes one mandatory parameter: cache,
which is an object which understands get() and set() calls,
such as an L<CHI> object.
Expand Down Expand Up @@ -92,21 +92,33 @@ sub AUTOLOAD {
my $func = $param;
my $object = $self->{'object'};

if($param !~ /^[gs]et_/) {
my $cache = $self->{'cache'};
if($param =~ /^[gs]et_/) {
# $param = "SUPER::$param";
return $object->$func(\@_);
}

if(scalar(@_) == 0) {
if(my $rc = $cache->get($param)) {
return $rc;
my $cache = $self->{'cache'};

if(scalar(@_) == 0) {
# Retrieving a value
if(my $rc = $cache->get($param)) {
if(ref($rc) eq 'ARRAY') {
return @{$rc};
}
return $rc;
}

# $param = "SUPER::$param";
# return $cache->set($param, $self->$param(@_), 'never');
return $cache->set($param, $object->$func(@_), 'never');
}

# $param = "SUPER::$param";
$object->$func(@_);
# return $cache->set($param, $self->$param(@_), 'never');
if($_[1]) {
# Storing an array
my $val = $object->$func(\@_);
$cache->set($param, $val, 'never');
return @{$val};
}
# Storing a scalar
return $cache->set($param, $object->$func($_[0]), 'never');
}

=head1 AUTHOR
Expand Down
7 changes: 6 additions & 1 deletion t/test.t
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::Most tests => 6;
use Test::Most tests => 9;
use Test::NoWarnings;
use CHI;

Expand All @@ -20,6 +20,11 @@ TEST: {
ok($l->fred() eq 'wilma');
ok($l->fred() eq 'wilma');

my @rc = $l->adventure('plugh', 'xyzzy');
ok(scalar(@rc) == 2);
ok($rc[0] eq 'plugh');
ok($rc[1] eq 'xyzzy');

# foreach my $key($cache->get_keys()) {
# diag($key);
# }
Expand Down

0 comments on commit d4e4c67

Please sign in to comment.