File Coverage

blib/lib/Memoize/HashKey/Ignore.pm
Criterion Covered Total %
statement 41 44 93.1
branch 8 10 80.0
condition 6 8 75.0
subroutine 12 12 100.0
pod n/a
total 67 74 90.5


line stmt bran cond sub pod time code
1             package Memoize::HashKey::Ignore;
2              
3 3     3   86011 use 5.006;
  3         13  
  3         138  
4 3     3   16 use strict;
  3         7  
  3         112  
5 3     3   15 use warnings FATAL => 'all';
  3         9  
  3         120  
6              
7 3     3   2290 use Try::Tiny;
  3         4668  
  3         148  
8 3     3   1893 use Memoize;
  3         5095  
  3         1507  
9              
10             =head1 NAME
11              
12             Memoize::HashKey::Ignore - allow certain keys not to be memoized.
13              
14             =head1 VERSION
15              
16             Version 0.02
17              
18             =cut
19              
20             our $VERSION = '0.02';
21              
22             =head1 SYNOPSIS
23              
24             use Memoize;
25              
26             tie my %scalar_cache = 'Memoize::HashKey::Ignore', IGNORE => sub { my $key = shift, return ($key eq 'BROKENKEY') ? 1 : 0; };
27             tie my %list_cache = 'Memoize::HashKey::Ignore', IGNORE => sub { my $key = shift, return ($key eq 'BROKENKEY') ? 1 : 0; };
28              
29             memoize('function', SCALAR_CACHE => [ HASH => \%scalar_cache ], LIST_CACHE => [ HASH => \%list_cache ]);
30              
31             =head1 EXPORT
32              
33             Sometimes you don't want to store certain keys. You know what the values looks likes, but you can't easily write memoize function which culls them itself.
34              
35             Memoize::HashKey::Ignore allows you to supply a code reference which describes, which keys should not be stored in Memoization Cache.
36              
37             This module will allow you to memoize the entire function with splitting it into cached and uncached pieces.
38              
39             =cut
40              
41             sub TIEHASH {
42 6     6   2146 my ( $package, %args ) = @_;
43 6   100     42 my $cache = $args{HASH} || {};
44              
45 6 100 100     40 if ( $args{IGNORE} and not ref $args{IGNORE} eq 'CODE' ) {
46 1         17 die 'Memoize::HashKey::Ignore: IGNORE argument must be a code ref.';
47             }
48 5 100       19 if ( $args{TIE} ) {
49 1         2 my ( $module, @opts ) = @{ $args{TIE} };
  1         5  
50 1         3 my $modulefile = $module . '.pm';
51 1         3 $modulefile =~ s{::}{/}g;
52 1     1   622 try { require $modulefile }
53             catch {
54 1     1   28 die 'Memoize::HashKey::Ignore: Could not load hash tie module "'
55             . $module . '": '
56             . $_;
57 1         13 };
58 0         0 my $rc = (
59             tie %$cache => $module,
60             @opts
61             );
62 0 0       0 if ( not $rc ) {
63 0         0 die 'Memoize::HashKey::Ignore Could not tie hash to "'
64             . $module . '": '
65             . $@;
66             }
67             }
68              
69 4         10 $args{CACHE} = $cache;
70 4         26 return bless \%args => $package;
71             }
72              
73             sub EXISTS {
74 63     63   12167 my ( $self, $key ) = @_;
75 63 100       262 return ( exists $self->{CACHE}->{$key} ) ? 1 : 0;
76             }
77              
78             sub FETCH {
79 36     36   226 my ( $self, $key ) = @_;
80 36         183 return $self->{CACHE}->{$key};
81             }
82              
83             sub CLEAR {
84 2     2   741 my ( $self ) = @_;
85 2         5 $self->{CACHE} = {};
86 2         11 return $self->{CACHE};
87             }
88              
89             sub STORE {
90 27     27   381 my ( $self, $key, $value ) = @_;
91              
92 27 100 33     93 if ( not defined $self->{IGNORE} or not &{ $self->{IGNORE} }($key) ) {
  27         79  
93 21         154 $self->{CACHE}->{$key} = $value;
94             }
95              
96 27         104 return;
97             }
98              
99             =head1 AUTHOR
100              
101             binary.com, C<< >>
102              
103             =head1 BUGS
104              
105             Please report any bugs or feature requests to C, or through
106             the web interface at L. I will be notified, and then you'll
107             automatically be notified of progress on your bug as I make changes.
108              
109              
110              
111              
112             =head1 SUPPORT
113              
114             You can find documentation for this module with the perldoc command.
115              
116             perldoc Memoize::HashKey::Ignore
117              
118              
119             You can also look for information at:
120              
121             =over 4
122              
123             =item * RT: CPAN's request tracker (report bugs here)
124              
125             L
126              
127             =item * AnnoCPAN: Annotated CPAN documentation
128              
129             L
130              
131             =item * CPAN Ratings
132              
133             L
134              
135             =item * Search CPAN
136              
137             L
138              
139             =back
140              
141              
142             =head1 ACKNOWLEDGEMENTS
143              
144              
145             =head1 LICENSE AND COPYRIGHT
146              
147             Copyright 2014 binary.com.
148              
149             This program is free software; you can redistribute it and/or modify it
150             under the terms of the the Artistic License (2.0). You may obtain a
151             copy of the full license at:
152              
153             L
154              
155             Any use, modification, and distribution of the Standard or Modified
156             Versions is governed by this Artistic License. By using, modifying or
157             distributing the Package, you accept this license. Do not use, modify,
158             or distribute the Package, if you do not accept this license.
159              
160             If your Modified Version has been derived from a Modified Version made
161             by someone other than you, you are nevertheless required to ensure that
162             your Modified Version complies with the requirements of this license.
163              
164             This license does not grant you the right to use any trademark, service
165             mark, tradename, or logo of the Copyright Holder.
166              
167             This license includes the non-exclusive, worldwide, free-of-charge
168             patent license to make, have made, use, offer to sell, sell, import and
169             otherwise transfer the Package with respect to any patent claims
170             licensable by the Copyright Holder that are necessarily infringed by the
171             Package. If you institute patent litigation (including a cross-claim or
172             counterclaim) against any party alleging that the Package constitutes
173             direct or contributory patent infringement, then this Artistic License
174             to you shall terminate on the date that such litigation is filed.
175              
176             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
177             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
178             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
179             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
180             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
181             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
182             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
183             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
184              
185              
186             =cut
187              
188             1; # End of Memoize::HashKey::Ignore