| 1 | package Bio::Protease::Role::WithCache; | ||
| 2 | { | ||
| 3 | $Bio::Protease::Role::WithCache::VERSION = '1.112980'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: A role that adds optional memoization of ProteaseI methods | ||
| 7 | |||
| 8 | use Moose::Role; | ||
| 9 | use MooseX::Types::Moose 'Bool'; | ||
| 10 | use namespace::autoclean; | ||
| 11 | |||
| 12 | has use_cache => ( is => 'ro', isa => Bool, default => 0 ); | ||
| 13 | |||
| 14 | has cache => ( | ||
| 15 | is => 'ro', | ||
| 16 | lazy => 1, | ||
| 17 | does => 'Cache::Ref::Role::API', | ||
| 18 | predicate => '_has_cache', | ||
| 19 | default => | ||
| 20 | sub { require Cache::Ref::LRU; Cache::Ref::LRU->new( size => 5000 ) }, | ||
| 21 | ); | ||
| 22 | |||
| 23 | foreach my $method (qw(digest is_substrate cleavage_sites)) { | ||
| 24 | around $method => sub { | ||
| 25 | my ($orig, $self, $substrate) = @_; | ||
| 26 | |||
| 27 | return $self->$orig($substrate) if ( !$self->use_cache or !$substrate ); | ||
| 28 | |||
| 29 | my $computed = $self->cache->get("$method-$substrate"); | ||
| 30 | |||
| 31 | if ($computed) { | ||
| 32 | return @$computed; | ||
| 33 | } | ||
| 34 | else { | ||
| 35 | my @result = $self->$orig($substrate); | ||
| 36 | $self->cache->set( "$method-$substrate" => \@result ); | ||
| 37 | return @result; | ||
| 38 | } | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | 1; | ||
| 44 | |||
| 45 | __END__ |