File Coverage

blib/lib/Bio/Protease/Role/WithCache.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Bio::Protease::Role::WithCache;
2             {
3             $Bio::Protease::Role::WithCache::VERSION = '1.112900'; # TRIAL
4             }
5              
6             # ABSTRACT: A role that adds optional memoization of ProteaseI methods
7              
8 4     4   1630 use Moose::Role;
  4         4  
  4         17  
9 4     4   12789 use MooseX::Types::Moose 'Bool';
  4         7  
  4         24  
10 4     4   12722 use namespace::autoclean;
  4         5  
  4         26  
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__
46             =pod
47              
48             =head1 NAME
49              
50             Bio::Protease::Role::WithCache - A role that adds optional memoization of ProteaseI methods
51              
52             =head1 VERSION
53              
54             version 1.112900
55              
56             =head1 SYNOPSIS
57              
58             package My::Protease;
59             use Moose;
60             with qw(Bio::ProteaseI Bio::Protease::Role::WithCache);
61              
62             sub _cuts { ... }
63              
64             # Done, all ProteaseI methods now support optional caching
65             # through the 'has_cache' and 'cache' attributes
66              
67             1;
68              
69             =head1 ATTRIBUTES
70              
71             =head2 use_cache
72              
73             Turn caching on, trading memory for speed. Defaults to 0 (no caching).
74             Useful when any method is being called several times with the same
75             argument.
76              
77             my $p = Bio::Protease->new( specificity => 'trypsin', use_cache => 0 );
78             my $c = Bio::Protease->new( specificity => 'trypsin', use_cache => 1 );
79              
80             my $substrate = 'MAAEELRKVIKPR' x 10;
81              
82             $p->digest( $substrate ) for (1..1000); # time: 5.11s
83             $c->digest( $substrate ) for (1..1000); # time: 0.12s
84              
85             =head2 cache
86              
87             The cache object, which has to do the L<Cache::Ref::Role::API> role.
88             Uses L<Cache::Ref::LRU> by default with a cache size of 5000, but you
89             can set this to your liking at construction time:
90              
91             my $p = Bio::Protease->new(
92             use_cache => 1,
93             cache => Cache::Ref::Random->new( size => 50 ),
94             specificity => 'trypsin'
95             );
96              
97             =head1 AUTHOR
98              
99             Bruno Vecchi <vecchi.b gmail.com>
100              
101             =head1 COPYRIGHT AND LICENSE
102              
103             This software is copyright (c) 2011 by Bruno Vecchi.
104              
105             This is free software; you can redistribute it and/or modify it under
106             the same terms as the Perl 5 programming language system itself.
107              
108             =cut
109