2
要使用MooseX::Storage::IO::CHI存儲以下使用MooseX::Storage:如何使用MooseX :: Storage來存儲一個大的HashRef?
has 'packages' => (is => 'ro', isa => 'HashRef', ...);
它得到存儲對象,但我不能從緩存中檢索。
我的演示代碼如下...
# 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;
上面的代碼轉儲如下:
$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;
所以,它看起來像作爲packages
得到存儲在緩存中。但最新的undef
顯示,比
my $some2 = Some->load('packages', cache => $cache);
不符合我的期望。
任何人都可以幫忙嗎?
OMG,它看起來像我需要RTFM ...謝謝! :) – jm666