/MooseX-CacheBacked

Use a cache for low level storage of attribute values.

Primary LanguagePerl

NAME

MooseX::CacheBacked - back class attributes in a cache

SYNOPSIS

package Shirt;
use Moose;
use MooseX::CacheBacked;

has id    => ( is => 1, isa => 'Str', required => 1 ); # must exist

has color => ( is => 'rw', isa => 'Str' );
has size  => ( is => 'ro', isa => 'Int' );

# later...

my $c = Shirt->new(id => '123', color => 'blue', size => 5);

# inside cache:
#     123:id    =>  123
#     123:color =>  blue
#     123:size  =>  5

$c->color('yellow');

# inside cache:
#     123:color => 'yellow'

DESCRIPTION

Usually Moose uses a blessed hash for the low level storage of attribute values. MooseX::CacheBacked causes a cache to be used instead. Each object's attribute/value combinations become key/value pairs in the cache. The cache key is a combination of the objects id and the attribute name. For this reason, MooseX::CacheBacked classes require an attribute called id that is unique in the context that the object is used.

Whenever a MooseX::Cache attribute is set (via init_arg or via the writer method) the corresponding value in the cache is immediately adjusted.

Newly instantiated MooseX::Cache objects automatically have their attributes set to the values of any previously instantiated objects with the same id.

my $x = Some::MooseX::Cache::Class->new(id => 7);

$x->size(14);
$x->color('green');
$x->material('silk');

# This could be in another processes that has access to the cache
my $y = Some::MooseX::Cache::Class->new(id => 7);
$y->size;  # 14
$y->color; # 'green'
$y->material('wool');

$x->material; # wool