| 1 | # Copyright 2008, 2009, 2010 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Memoize::ConstSecond; | ||
| 18 | use 5.006; | ||
| 19 | ## no critic (RequireUseStrict RequireUseWarnings) | ||
| 20 | no warnings; | ||
| 21 | |||
| 22 | sub import { | ||
| 23 | my @names = @_; | ||
| 24 | my $package = caller; | ||
| 25 | foreach my $name (@names) { | ||
| 26 | if ($name !~ /::/) { | ||
| 27 | $name = "${package}::$name"; | ||
| 28 | } | ||
| 29 | my $old = \&$name; | ||
| 30 | my $last_time = time() - 1; | ||
| 31 | my $last_value; | ||
| 32 | *$name = sub { | ||
| 33 | my $t = time(); | ||
| 34 | if ($t == $last_time) { | ||
| 35 | return $last_value; | ||
| 36 | } else { | ||
| 37 | $last_time = $t; | ||
| 38 | return ($last_value = $old->(@_)); | ||
| 39 | } | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | 1; | ||
| 45 | __END__ | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | App::Chart::Memoize::ConstSecond -- memoize functions to cache for 1 second | ||
| 50 | |||
| 51 | =head1 SYNOPSIS | ||
| 52 | |||
| 53 | sub foo { some_code() }; | ||
| 54 | use App::Chart::Memoize::ConstSecond 'foo'; | ||
| 55 | |||
| 56 | sub bar { some_code() }; # or a set of functions at once | ||
| 57 | sub quux { some_code() }; | ||
| 58 | use App::Chart::Memoize::ConstSecond 'bar','quux'; | ||
| 59 | |||
| 60 | use App::Chart::Memoize::ConstSecond 'Some::Other::func'; | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | C<App::Chart::Memoize::ConstSecond> modifies given functions so that the return value | ||
| 65 | from the original is cached for 1 second (until C<time()> ticks over). This | ||
| 66 | is meant to save work if the original func has to do something slow like a | ||
| 67 | disk lookup or similar, yet still re-run that later on to allow for changes. | ||
| 68 | |||
| 69 | =head1 FUNCTIONS | ||
| 70 | |||
| 71 | There are no functions as such, everything is accomplished through the | ||
| 72 | C<use> import. | ||
| 73 | |||
| 74 | =over 4 | ||
| 75 | |||
| 76 | =item C<< use App::Chart::Memoize::ConstSecond 'func' >> | ||
| 77 | |||
| 78 | =item C<< use App::Chart::Memoize::ConstSecond 'Some::Package::func' >> | ||
| 79 | |||
| 80 | =back | ||
| 81 | |||
| 82 | =head1 OTHER NOTES | ||
| 83 | |||
| 84 | C<Memoize::Expire> adds a similar time-based expiry to the C<Memoize> cache. | ||
| 85 | C<ConstSecond> is a lot smaller. | ||
| 86 | |||
| 87 | =head1 SEE ALSO | ||
| 88 | |||
| 89 | L<Memoize::Expire> | ||
| 90 | |||
| 91 | =cut |