Bio-Protease

 view release on metacpan or  search on metacpan

lib/Bio/Protease/Role/WithCache.pm  view on Meta::CPAN

package Bio::Protease::Role::WithCache;
{
  $Bio::Protease::Role::WithCache::VERSION = '1.112980';
}

# ABSTRACT: A role that adds optional memoization of ProteaseI methods

use Moose::Role;
use MooseX::Types::Moose 'Bool';
use namespace::autoclean;

has use_cache => ( is => 'ro', isa => Bool, default => 0 );

has cache => (
    is        => 'ro',
    lazy      => 1,
    does      => 'Cache::Ref::Role::API',
    predicate => '_has_cache',
    default =>
      sub { require Cache::Ref::LRU; Cache::Ref::LRU->new( size => 5000 ) },
);

foreach my $method (qw(digest is_substrate cleavage_sites)) {
    around $method => sub {
        my ($orig, $self, $substrate) = @_;

        return $self->$orig($substrate) if ( !$self->use_cache or !$substrate );

        my $computed = $self->cache->get("$method-$substrate");

        if ($computed) {
            return @$computed;
        }
        else {
            my @result = $self->$orig($substrate);
            $self->cache->set( "$method-$substrate" => \@result );
            return @result;
        }
    };
}


1;

__END__
=pod

=head1 NAME

Bio::Protease::Role::WithCache - A role that adds optional memoization of ProteaseI methods

=head1 VERSION

version 1.112980

=head1 SYNOPSIS

    package My::Protease;
    use Moose;
    with qw(Bio::ProteaseI Bio::Protease::Role::WithCache);

    sub _cuts { ... }

    # Done, all ProteaseI methods now support optional caching
    # through the 'has_cache' and 'cache' attributes

    1;

=head1 ATTRIBUTES



( run in 1.289 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )