Lab::Moose::Instrument::Cache - Role for device cache functionality in Moose::Instrument drivers.


in your driver:

 use Lab::Moose::Instrument::Cache;

 cache 'foobar' => (getter => 'get_foobar');

 sub get_foobar {
     my $self = shift;
     return $self->cached_foobar(
         $self->query(command => ...));

 sub set_foobar {
     my ($self, $value) = @_;
     $self->write(command => ...);


This package exports a new Moose keyword: cache.

Calling cache key => (getter => $getter, isa => $type) will generate a Moose attribute 'cached_key' with the following properties:

 is => 'rw',
 isa => $type,
 predicate => 'has_cached_key',
 clearer => 'clear_cached_key',
 builder => 'cached_key_builder',
 lazy => 1,
 init_arg => undef

The isa argument is optional.

The builder method comes into play if a cache entry is in the cleared state. If the getter is called in this situation, the builder method will be used to generate the value. The default builder method just calls the configured $getter method.

If you need to call the getter with specific arguments, override the builder method. For example, the format_data_query of the Lab::Moose::Instrument::RS_ZVM needs a timeout of 3s. This is done by putting the following into the driver:

 sub cached_format_data_builder {
     my $self = shift;
     return $self->format_data_query( timeout => 3 );