jm666 jm666 - 6 months ago 11
Perl Question

How to use MooseX::Storage for storing one big HashRef?

Want to use MooseX::Storage using the MooseX::Storage::IO::CHI for storing the following:

has 'packages' => (is => 'ro', isa => 'HashRef', ...);


It got the object stored, but I can't retrieve it from the cache.

My demo code is the following...

# the Entry objects are the values in the Some->packages HasrRef
package Entry {
use Moose;
use warnings;
use MooseX::Storage;
with Storage(io => [ 'CHI' => { key_attr => 'id', key_prefix => 'id-' }]);
has 'id' => (is => 'ro', isa => 'Int', default => 0);
has 'names' => (is => 'ro', isa => 'ArrayRef[Str]', default => sub{ [] } );
}

# want store THIS object into the cache,
# because the computation of the '_build_packages' is expensive
package Some {
use Moose;
use warnings;
use MooseX::Storage;
with Storage(io => [ 'CHI' => { key_attr => 'packages', key_prefix => 'package-' }]);

has 'packages' => (is => 'ro', isa => 'HashRef', builder => '_build_packages');
sub _build_packages {
my $hr;
# building the demo 2-element hash here - otherwise very time-expensive code...
# each value in the hash an object ISA: Entry
$hr->{$_} = Entry->new(id => $_, names => ["some", "names $_"]) for (1..2);
return $hr;
}
}

use 5.014;
use warnings;
use Data::Dumper;
use CHI;

my $cachedir = './testcache';
my $cache = CHI->new(driver => 'File', root_dir => $cachedir);

my $some = Some->new(); # OK
say Dumper $some;
my $cached = $some->store(cache => $cache); # also OK
say Dumper $cached;

# doesn't loads the 'packages'...
my $some2 = Some->load('packages', cache => $cache); #returns undef :(
say Dumper $some2;


the above code dumps the following:

$VAR1 = bless( {
'packages' => {
'1' => bless( {
'names' => [
'some',
'names 1'
],
'id' => 1
}, 'Entry' ),
'2' => bless( {
'id' => 2,
'names' => [
'some',
'names 2'
]
}, 'Entry' )
}
}, 'Some' );

$VAR1 = {
'__CLASS__' => 'Some',
'packages' => {
'2' => {
'__CLASS__' => 'Entry',
'names' => [
'some',
'names 2'
],
'id' => 2
},
'1' => {
'id' => 1,
'__CLASS__' => 'Entry',
'names' => [
'some',
'names 1'
]
}
}
};

$VAR1 = undef;


So,it looks like as the
packages
got stored in the cache. But the latest
undef
shows, than the

my $some2 = Some->load('packages', cache => $cache);


doesn't works as I expecting.

Could anyone help?

Answer

load() requires the actual value of the key_attr and not just the name of it.

So I believe something like this would work.

# the Entry objects are the values in the Some->packages HasrRef
package Entry {
    use Moose;
    use warnings;
    use MooseX::Storage;
    with Storage(io => [ 'CHI' => { key_attr => 'id', key_prefix => 'id-' }]);
    has 'id' => (is => 'ro', isa => 'Int', default => 0);
    has 'names' => (is => 'ro', isa => 'ArrayRef[Str]', default => sub{ [] } );
}

# want store THIS object into the cache,
# because the computation of the '_build_packages' is expensive
package Some {
    use Moose;
    use warnings;
    use MooseX::Storage;
    with Storage(io => [ 'CHI' => { key_attr => 'some_id', key_prefix => 'some_id-' }]);

    has 'some_id' => (is => 'ro', isa => 'Int');
    has 'packages' => (is => 'ro', isa => 'HashRef', builder => '_build_packages');
    sub _build_packages {
        my $hr;
        # building the demo 2-element hash here - otherwise very time-expensive code...
        # each value in the hash an object ISA: Entry 
        $hr->{$_} = Entry->new(id => $_, names => ["some", "names $_"]) for (1..2);
        return $hr;
    }
}

use 5.014;
use warnings;
use Data::Dumper;
use CHI;

my $cachedir = './testcache';
my $cache = CHI->new(driver => 'File', root_dir => $cachedir);

my $some = Some->new(some_id => 100);             # OK
say Dumper $some;
my $cached = $some->store(cache => $cache); # also OK
say Dumper $cached;

my $some2 = Some->load(100, cache => $cache);
say Dumper $some2->packages;