NAME

Lab::Moose::Instrument::Cache - Device caching functionality in Moose::Instrument drivers

VERSION

version 3.860

SYNOPSIS

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 => ...);
    $self->cached_foobar($value);
}

DESCRIPTION

This package exports a new Moose keyword: cache.

Calling cache key => (getter => $getter, isa => $type) generates the following functions:

cached_key (accessor)

Calling $instr->cached_key() will return the last stored value from the cache. If the cache entry is empty, use the $getter method.

To update the cache entry, call $instr->cached_key($value).

has_cached_key (predicate)

Return true if the cache entry holds a value (which is not undef).

clear_cached_key (clearer)

Clear the value of the cache entry.

cached_key_builder (builder)

Called by cached_key if the entry is cleared. This will call the $getter method. Can be overriden by 'around' method modifier if the $getter needs special extra arguments.

The isa argument is optional.

Array cache

Some methods take an additional parameter (e.g. channel number). For this case you can give the index_arg argument to the cache keyword:

cache foobar => (isa => 'Num', getter => 'get_foobar', index_arg => 'channel');

# Get value from cache.
my $value = $instr->cached_foobar(channel => 1);

# Store value.
$instr->cached_foobar(channel => 2, value => 1.234);

# Clear single entry.
$instr->clear_cached_foobar(channel => 3);

# Clear them all.
$instr->clear_cached_foobar();

# Check for cache value
if ($instr->has_cached_foobar(channel => 1)) {...}

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by the Lab::Measurement team; in detail:

Copyright 2016       Simon Reinhardt
          2017       Andreas K. Huettel, Simon Reinhardt
          2018       Simon Reinhardt
          2020       Andreas K. Huettel

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.