2016-05-14 20 views
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); 

不符合我的期望。

任何人都可以幫忙嗎?

回答

3

load()需要key_attr的實際值,而不僅僅是它的名稱。

所以我相信這樣的事情會起作用。

# 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; 
+0

OMG,它看起來像我需要RTFM ...謝謝! :) – jm666

相關問題