Cache-Ref-0.04000755001750001750 011446730246 13172 5ustar00brunovbrunov000000000000README000644001750001750 45211446730246 14113 0ustar00brunovbrunov000000000000Cache-Ref-0.04 This archive contains the distribution Cache-Ref, version 0.04: Memory only cache of live references This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Changes000644001750001750 44411446730246 14527 0ustar00brunovbrunov000000000000Cache-Ref-0.040.04 - Replace deprecated use of 'for qw(...)' with 'for (qw(...))' 0.03 - Make sure that Cache::Ref::Random::expire removes the desired number of items 0.02 - Added LIFO, Null and Random implementations - Added 'expire' method - Documentation improvements 0.01 - Initial version t000755001750001750 011446730246 13356 5ustar00brunovbrunov000000000000Cache-Ref-0.04lru.t000644001750001750 655111446730246 14513 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use ok 'Cache::Ref::LRU'; foreach my $lru ( map { "Cache::Ref::Util::LRU::$_" } qw(Array List) ) { use_ok($lru); { my $c = Cache::Ref::LRU->new( size => 3, lru_class => $lru ); isa_ok( $c, "Cache::Ref" ); $c->set( foo => "blah" ); is( $c->get("foo"), "blah", "foo in cache" ); $c->set( bar => "lala" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar in cache" ); $c->set( baz => "blob" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "blob", "baz in cache" ); { my $ran = 0; $c->compute( baz => sub { $ran++ } ); ok( !$ran, "did not compute" ); } { my $ran = 0; $c->compute( zot => sub { $ran++; "quxx" } ); ok( $ran, "did compute" ); } is( $c->get("foo"), undef, "foo no longer in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "blob", "baz still in cache" ); is( $c->get("zot"), "quxx", "zot in cache" ); $c->hit("bar"); is( $c->_lru->mru, "bar", "mru" ); is( $c->_lru->lru, "baz", "lru" ); $c->set( oi => "vey" ); is( $c->get("foo"), undef, "foo no longer in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), undef, "baz no longer in cache" ); is( $c->get("zot"), "quxx", "zot still in cache" ); is( $c->get("oi"), "vey", "oi in cache" ); $c->set( foo => "brrr" ); $c->set( foo => "bar" ); $c->set( bar => "baz" ); is( $c->get("foo"), "bar", "foo in cache" ); is( $c->get("bar"), "baz", "bar still in cache, new value" ); is( $c->get("baz"), undef, "baz no longer in cache" ); is( $c->get("zot"), undef, "zot no longer in cache" ); is( $c->get("oi"), "vey", "oi still in cache" ); is_deeply( [ $c->get(qw(foo bar nothere)) ], [ qw(bar baz), undef ], "mget" ); $c->remove("oi"); is( $c->get("oi"), undef, "oi removed from cache" ); is( $c->_index_size, 2, "two elements in cache" ); $c->expire(1); is( $c->_index_size, 1, "expired one entry" ); $c->clear; is( $c->_index_size, 0, "cache is empty" ); } { my $c = Cache::Ref::LRU->new( size => 5, lru_class => $lru ); my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 2000 ) { my $key = 1 + int rand 8; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } cmp_ok( $hit, '>=', $miss, "more cache hits than misses during random access of small sigma ($hit >= $miss)" ); ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { foreach my $key ( 1 .. 10 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '<=', $c->size * 3, "no significant hits during linear scans ($hit)" ); } } done_testing; # ex: set sw=4 et: car.t000644001750001750 2642211446730246 14475 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use List::Util qw(shuffle); my $inv; sub invariants { my $self = shift; $inv++; # bugs fail("mru size undeflow") if $self->_mru_size < 0; fail("mfu size underflow") if $self->_mfu_size < 0; fail("mfu history size underflow") if $self->_mfu_history_size < 0; fail("mfu history count and list disagree") if $self->_mfu_history_size xor $self->_mfu_history_head; fail("mru history size underflow") if $self->_mru_history_size < 0; fail("mru history count and list disagree") if $self->_mru_history_size xor $self->_mru_history_head; # I1 0 ≤ |T1| + |T2| ≤ c. fail("mru + mfu size <= cache size") if $self->_mfu_size + $self->_mru_size > $self->size; if ( $self->isa("Cache::Ref::CART") ) { # I2’ 0 ≤ |T2|+|B2| ≤ c. if ( 0 and $self->_mfu_size + $self->_mfu_history_size > $self->size ) { fail("mfu + mfu history size <= cache size "); diag sprintf "%d + %d > %d", $self->_mfu_size, $self->_mfu_history_size, $self->size; } # I3’ 0 ≤ |T1|+|B1| ≤ 2c. fail("mru + mru history size <= cache size * 2 ") if $self->_mru_size + $self->_mru_history_size > $self->size * 2; } else { # I2 0 ≤ |T1| + |B1| ≤ c. fail("mru + mru history size <= cache size ") if $self->_mru_size + $self->_mru_history_size > $self->size; # I3 0 ≤ |T2| + |B2| ≤ 2c. fail("mfu + mfu history size <= cache size * 2 ") if $self->_mfu_size + $self->_mfu_history_size > $self->size * 2; } my $sum = $self->_mfu_size + $self->_mfu_history_size + $self->_mru_size + $self->_mru_history_size; if ( $sum > $self->size * 2 ) { # I4 0 ≤ |T1| + |T2| + |B1| + |B2| ≤ 2c. fail("sum of all sizes <= cache size * 2 "); diag sprintf("mfu=%d + mfuh=%d + mru=%d + mruh=%d > %d * 2", $self->_mfu_size, $self->_mfu_history_size, $self->_mru_size, $self->_mru_history_size, $self->size); } fail("index size > cache size * 2") if $self->_index_size > $self->size * 2; fail("size sums != index size") if $self->_index_size != $sum; # FIXME these invariants are broken on remove # I5 If |T1|+|T2|_mru_size + $self->_mfu_size < $self->size and $self->_mru_history_size || $self->_mfu_history_size; # I6 If |T1|+|B1|+|T2|+|B2| ≥ c, then |T1| + |T2| = c. #fail("clocks aren't full index size is bigger than cache size") # if $self->_mru_size + $self->_mfu_size != $self->size # and $self->_mfu_size + $self->_mfu_history_size + $self->_mru_size + $self->_mru_history_size >= $self->size; #fail("clocks aren't full index size is bigger than cache size") # if $self->_mru_size + $self->_mfu_size != $self->size and $self->_index_size >= $self->size; # I7 Due to demand paging, once the cache is full, it remains full from then on. } foreach my $class (qw(Cache::Ref::CAR Cache::Ref::CART)) { use_ok($class); my $meta = $class->meta; $meta->make_mutable; foreach my $method ( grep { /^[a-z]/ && !/^(?:size|meta)$/ } $meta->get_method_list ) { $meta->add_before_method_modifier($method, sub { ::invariants($_[0]) }); $meta->add_after_method_modifier ($method, sub { ::invariants($_[0]) }); } $meta->make_immutable; $inv = 0; { my $c = $class->new( size => 3 ); isa_ok( $c, "Cache::Ref" ); $c->set( foo => "blah" ); is( $c->peek("foo"), "blah", "foo in cache" ); $c->set( bar => "lala" ); is( $c->peek("foo"), "blah", "foo still in cache" ); is( $c->peek("bar"), "lala", "bar in cache" ); $c->set( baz => "blob" ); is( $c->peek("foo"), "blah", "foo still in cache" ); is( $c->peek("bar"), "lala", "bar still in cache" ); is( $c->peek("baz"), "blob", "baz in cache" ); $c->set( zot => "quxx" ); is( $c->peek("foo"), undef, "foo no longer in cache" ); is( $c->peek("bar"), "lala", "bar still in cache" ); is( $c->peek("baz"), "blob", "baz still in cache" ); is( $c->peek("zot"), "quxx", "zot in cache" ); $c->hit("bar"); $c->get("baz"); $c->set( oi => "vey" ); is( $c->peek("foo"), undef, "foo no longer in cache" ); is( $c->peek("bar"), "lala", "bar still in cache" ); is( $c->peek("baz"), "blob", "baz still in cache" ); is( $c->peek("zot"), undef, "zot no longer in cache" ); is( $c->peek("oi"), "vey", "oi in cache" ); $c->set( foo => "bar" ); $c->set( bar => "baz" ); is( $c->peek("foo"), "bar", "foo in cache" ); is( $c->peek("bar"), "baz", "bar still in cache, new value" ); is( $c->peek("baz"), "blob", "baz no longer in cache" ); is( $c->peek("zot"), undef, "zot no longer in cache" ); is( $c->peek("oi"), undef, "oi still in cache" ); is_deeply( [ $c->peek(qw(foo bar baz nothere)) ], [ qw(bar baz blob), undef ], "mget" ); $c->remove("foo"); is( $c->peek("foo"), undef, "foo removed" ); $c->expire(2); is_deeply( [ $c->peek(qw(foo bar baz nothere)) ], [ undef, undef, undef, undef ], "mget" ); } { my $c = $class->new( size => 5 ); { my ( $hit, $miss ) = ( 0, 0 ); foreach my $offset ( 1 .. 100 ) { for ( 1 .. 100 ) { # high locality of reference, should adjust to lru my $key = $offset + int rand 4; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss * 10, "hit rate during random access of small sigma ($hit >= $miss * 3)" ); cmp_ok( $miss, '<=', 400, "miss rate during random access of small sigma ($miss <= max offset * 4)" ); } { my ( $hit, $miss ) = ( 0, 0 ); foreach my $offset ( 1 .. 100 ) { for ( 1 .. 30 ) { # medium locality of reference, my $key = $offset + int rand 8; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss, "hit rate during random access of medium sigma ($hit >= $miss)" ); } { my ( $hit, $miss ) = ( 0, 0 ); foreach my $offset ( 1 .. 100 ) { for ( 1 .. 30 ) { my $key = $offset + int rand 40; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss / 10, "hit rate during random access of large sigma ($hit >= $miss/10)" ); } { my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { # biased locality of reference, like a linear scan, but with weighting foreach my $key ( 1 .. 3, 1 .. 3, 1 .. 12 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss / 2, "hit rate during small linear scans ($hit >= $miss/2)" ); } { my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { # biased locality of reference, like a linear scan, but with weighting foreach my $key ( 1 .. 3, 1 .. 20 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } if ( $c->isa("Cache::Ref::CART") ) { # favours LRU due to the fact that access isn't random cmp_ok( $hit, '>=', $miss / 10, "hit rate during medium linear scan ($hit >= $miss/10)" ); } else { cmp_ok( $hit, '>=', $miss / 5, "hit rate during medium linear scan ($hit >= $miss/5)" ); } } { my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { # biased locality of reference, like a linear scan, but with weighting foreach my $key ( 1 .. 3, 1 .. 45 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } if ( $c->isa("Cache::Ref::CART") ) { # this test favours LRU, but the cache size is too small for LFU to matter over this sigma cmp_ok( $hit, '>=', $miss / 20, "hit rate during medium linear scan ($hit >= $miss/20)" ); } else { cmp_ok( $hit, '>=', $miss / 10, "hit rate during medium linear scan ($hit >= $miss/10)" ); } } { my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { # should favour LFU foreach my $key ( shuffle( 1 .. 2, 1 .. 3, 1 .. 10 ) ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss / 3, "hit rate during small sigma weighted random access ($hit >= $miss/3)" ); } { my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { # good for LFU with a larger history size foreach my $key ( shuffle(1 .. 3), shuffle(1 .. 45) ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss / 20, "hit rate during alternating small/large random access ($hit >= $miss/20)" ); } { my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 200 ) { # good for LFU with a large history size foreach my $key ( shuffle( 1 .. 2, 1 .. 3, 1 .. 45 ) ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss / 10, "hit rate during large weighted sigma, random access ($hit >= $miss/10)" ); } } cmp_ok( $inv, '>=', 1000, "invariants ran at least a few times" ); } done_testing; # ex: set sw=4 et: LICENSE000644001750001750 4351411446730246 14306 0ustar00brunovbrunov000000000000Cache-Ref-0.04This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2010 by Yuval Kogman. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2010 by Yuval Kogman. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End dist.ini000755001750001750 32411446730246 14700 0ustar00brunovbrunov000000000000Cache-Ref-0.04name = Cache-Ref version = 0.04 author = Yuval Kogman license = Perl_5 copyright_holder = Yuval Kogman [@Filter] -bundle = @NUFFIN dist = Cache-Ref repository_at = github [UploadToCPAN] [ConfirmRelease] null.t000644001750001750 45311446730246 14636 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use ok 'Cache::Ref::Null'; my $cache = Cache::Ref::Null->new; is( $cache->get("foo"), undef, "no value for get" ); $cache->set("foo" => 42); is( $cache->get("foo"), undef, "no value for after set" ); done_testing; # ex: set sw=4 et: fifo.t000644001750001750 525111446730246 14630 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use ok 'Cache::Ref::FIFO'; { my $c = Cache::Ref::FIFO->new( size => 3 ); isa_ok( $c, "Cache::Ref" ); $c->set( foo => "blah" ); is( $c->get("foo"), "blah", "foo in cache" ); $c->set( bar => "lala" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar in cache" ); $c->set( baz => "blob" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "blob", "baz in cache" ); $c->set( zot => "quxx" ); is( $c->get("foo"), undef, "foo no longer in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "blob", "baz still in cache" ); is( $c->get("zot"), "quxx", "zot in cache" ); $c->set( baz => "jsd" ); $c->set( quxx => "dancing" ); is( $c->get("foo"), undef, "foo no longer in cache" ); is( $c->get("bar"), undef, "bar no longer in cache" ); is( $c->get("baz"), "jsd", "baz still in cache" ); is( $c->get("zot"), "quxx", "zot still in cache" ); is( $c->get("quxx"), "dancing", "quxx in cache" ); $c->remove("quxx"); is( $c->get("foo"), undef, "foo no longer in cache" ); is( $c->get("bar"), undef, "bar no longer in cache" ); is( $c->get("baz"), "jsd", "baz still in cache" ); is( $c->get("zot"), "quxx", "zot still in cache" ); is( $c->get("quxx"), undef, "quxx removed from cache" ); is_deeply( [ $c->get(qw(baz zot nothere)) ], [ qw(jsd quxx), undef ], "mget" ); is( $c->_index_size, 2, "two elements in cache" ); $c->remove("bar"); $c->set( $_ => $_ ) for 1 .. 3; is( $c->get($_), $_, "get $_" ) for 1 .. 3; is( $c->_index_size, 3, "refilled" ); $c->expire(2); is( $c->_index_size, 1, "expired" ); $c->clear; is( $c->_index_size, 0, "no elements in cache" ); } { my $c = Cache::Ref::FIFO->new( size => 5 ); my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 2000 ) { my $key = 1 + int rand 8; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } cmp_ok( $hit, '>=', $miss, "more cache hits than misses during random access of small sigma ($hit >= $miss)" ); ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { foreach my $key ( 1 .. 10 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '<=', $c->size * 3, "no significant hits during linear scans ($hit)" ); } done_testing; # ex: set sw=4 et: lifo.t000644001750001750 613311446730246 14636 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use ok 'Cache::Ref::LIFO'; { my $c = Cache::Ref::LIFO->new( size => 3 ); isa_ok( $c, "Cache::Ref" ); $c->set( foo => "blah" ); is( $c->get("foo"), "blah", "foo in cache" ); $c->set( bar => "lala" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar in cache" ); $c->set( baz => "blob" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "blob", "baz in cache" ); $c->set( zot => "quxx" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "blob", "baz still in cache" ); is( $c->get("zot"), undef, "zot in not in cache" ); $c->set( baz => "jsd" ); $c->set( quxx => "dancing" ); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "jsd", "baz still in cache" ); is( $c->get("zot"), undef, "zot in not in cache" ); is( $c->get("quxx"), undef, "quxx not in cache" ); $c->remove("quxx"); is( $c->get("foo"), "blah", "foo still in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "jsd", "baz still in cache" ); is( $c->get("zot"), undef, "zot in not in cache" ); is( $c->get("quxx"), undef, "quxx not in cache" ); $c->remove("foo"); is( $c->get("foo"), undef, "foo no longer in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "jsd", "baz still in cache" ); is( $c->get("zot"), undef, "zot in not in cache" ); is( $c->get("quxx"), undef, "quxx not in cache" ); $c->set( quxx => "dancing" ); is( $c->get("foo"), undef, "foo no longer in cache" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), "jsd", "baz still in cache" ); is( $c->get("zot"), undef, "zot in not in cache" ); is( $c->get("quxx"), "dancing", "quxx in cache" ); is_deeply( [ $c->get(qw(bar baz nothere)) ], [ qw(lala jsd), undef ], "mget" ); is( $c->_index_size, 3, "refilled" ); $c->expire(2); is( $c->_index_size, 1, "expired" ); $c->clear; is( $c->_index_size, 0, "no elements in cache" ); } { my $c = Cache::Ref::LIFO->new( size => 5 ); my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 2000 ) { my $key = 1 + int rand 8; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } cmp_ok( $hit, '>=', $miss, "more cache hits than misses during random access of small sigma ($hit >= $miss)" ); ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { foreach my $key ( 1 .. 10 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '==', $miss, "hit rate during linear scan ($hit == $miss)" ); } done_testing; # ex: set sw=4 et: META.yml000644001750001750 1036011446730246 14543 0ustar00brunovbrunov000000000000Cache-Ref-0.04--- abstract: 'Memory only cache of live references' author: - 'Yuval Kogman' build_requires: Test::Moose: 0 Test::More: 0 ok: 0 configure_requires: ExtUtils::MakeMaker: 6.31 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.102341, CPAN::Meta::Converter version 2.102400' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Cache-Ref requires: Carp: 0 Hash::Util::FieldHash::Compat: 0 List::Util: 0 Moose: 0 Moose::Role: 0 MooseX::Role::Parameterized: 0 Scalar::Util: 0 namespace::autoclean: 0 resources: bugtracker: http://rt.cpan.org/Public/Dist/Display.html?Name=Cache-Ref homepage: http://search.cpan.org/dist/Cache-Ref repository: git://github.com/nothingmuch/cache-ref.git version: 0.04 x_Dist_Zilla: plugins: - class: Dist::Zilla::Plugin::GatherDir name: '@Filter/@Basic/GatherDir' version: 4.102341 - class: Dist::Zilla::Plugin::PruneCruft name: '@Filter/@Basic/PruneCruft' version: 4.102341 - class: Dist::Zilla::Plugin::ManifestSkip name: '@Filter/@Basic/ManifestSkip' version: 4.102341 - class: Dist::Zilla::Plugin::MetaYAML name: '@Filter/@Basic/MetaYAML' version: 4.102341 - class: Dist::Zilla::Plugin::License name: '@Filter/@Basic/License' version: 4.102341 - class: Dist::Zilla::Plugin::Readme name: '@Filter/@Basic/Readme' version: 4.102341 - class: Dist::Zilla::Plugin::ExtraTests name: '@Filter/@Basic/ExtraTests' version: 4.102341 - class: Dist::Zilla::Plugin::ExecDir name: '@Filter/@Basic/ExecDir' version: 4.102341 - class: Dist::Zilla::Plugin::ShareDir name: '@Filter/@Basic/ShareDir' version: 4.102341 - class: Dist::Zilla::Plugin::MakeMaker name: '@Filter/@Basic/MakeMaker' version: 4.102341 - class: Dist::Zilla::Plugin::Manifest name: '@Filter/@Basic/Manifest' version: 4.102341 - class: Dist::Zilla::Plugin::TestRelease name: '@Filter/@Basic/TestRelease' version: 4.102341 - class: Dist::Zilla::Plugin::ConfirmRelease name: '@Filter/@Basic/ConfirmRelease' version: 4.102341 - class: Dist::Zilla::Plugin::UploadToCPAN name: '@Filter/@Basic/UploadToCPAN' version: 4.102341 - class: Dist::Zilla::Plugin::MetaConfig name: '@Filter/MetaConfig' version: 4.102341 - class: Dist::Zilla::Plugin::MetaJSON name: '@Filter/MetaJSON' version: 4.102341 - class: Dist::Zilla::Plugin::PkgVersion name: '@Filter/PkgVersion' version: 4.102341 - class: Dist::Zilla::Plugin::PodSyntaxTests name: '@Filter/PodSyntaxTests' version: 4.102341 - class: Dist::Zilla::Plugin::NoTabsTests name: '@Filter/NoTabsTests' version: 0.01 - class: Dist::Zilla::Plugin::PodCoverageTests name: '@Filter/PodCoverageTests' version: 4.102341 - class: Dist::Zilla::Plugin::MetaResources name: '@Filter/MetaResources' version: 4.102341 - class: Dist::Zilla::Plugin::Authority name: '@Filter/Authority' version: 1.000 - class: Dist::Zilla::Plugin::EOLTests name: '@Filter/EOLTests' version: 0.02 - class: Dist::Zilla::Plugin::PodWeaver name: '@Filter/PodWeaver' version: 3.101640 - class: Dist::Zilla::Plugin::AutoPrereq name: '@Filter/AutoPrereq' version: 4.102341 - class: Dist::Zilla::Plugin::UploadToCPAN name: UploadToCPAN version: 4.102341 - class: Dist::Zilla::Plugin::ConfirmRelease name: ConfirmRelease version: 4.102341 - class: Dist::Zilla::Plugin::FinderCode name: ':InstallModules' version: 4.102341 - class: Dist::Zilla::Plugin::FinderCode name: ':TestFiles' version: 4.102341 - class: Dist::Zilla::Plugin::FinderCode name: ':ExecFiles' version: 4.102341 - class: Dist::Zilla::Plugin::FinderCode name: ':ShareFiles' version: 4.102341 zilla: class: Dist::Zilla::Dist::Builder config: is_trial: 0 version: 4.102341 x_authority: cpan:NUFFIN MANIFEST000644001750001750 126711446730246 14411 0ustar00brunovbrunov000000000000Cache-Ref-0.04Changes LICENSE MANIFEST META.json META.yml Makefile.PL README dist.ini lib/Cache/Ref.pm lib/Cache/Ref/CAR.pm lib/Cache/Ref/CAR/Base.pm lib/Cache/Ref/CART.pm lib/Cache/Ref/CLOCK.pm lib/Cache/Ref/CLOCK/Base.pm lib/Cache/Ref/FIFO.pm lib/Cache/Ref/GCLOCK.pm lib/Cache/Ref/LIFO.pm lib/Cache/Ref/LRU.pm lib/Cache/Ref/Null.pm lib/Cache/Ref/Random.pm lib/Cache/Ref/Role/API.pm lib/Cache/Ref/Role/Index.pm lib/Cache/Ref/Role/WithDoublyLinkedList.pm lib/Cache/Ref/Util/LRU/API.pm lib/Cache/Ref/Util/LRU/Array.pm lib/Cache/Ref/Util/LRU/List.pm t/car.t t/clock.t t/fifo.t t/lifo.t t/lru.t t/null.t t/random.t t/release-eol.t t/release-no-tabs.t t/release-pod-coverage.t t/release-pod-syntax.t t/util_lru.t clock.t000644001750001750 713211446730246 15000 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use ok 'Cache::Ref::CLOCK'; use ok 'Cache::Ref::GCLOCK'; foreach my $impl (qw(Cache::Ref::CLOCK Cache::Ref::GCLOCK)) { { my $c = $impl->new( size => 3 ); isa_ok( $c, "Cache::Ref" ); $c->set( foo => "blah" ); is( $c->get("foo"), "blah", "foo" ); $c->set( bar => "lala" ); is( $c->get("bar"), "lala", "bar" ); $c->set( baz => "blob" ); is( $c->get("baz"), "blob", "baz" ); $c->set( zot => "quxx" ); is( $c->get("zot"), "quxx", "zot" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("foo"), undef, "foo no longer in cache" ); $c->set( quxx => "tmp" ); $c->set( quxx => "dancing" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), undef, "baz no longer in cache" ); is( $c->get("zot"), "quxx", "zot still in cache" ); is( $c->get("quxx"), "dancing", "quxx in cache" ); $c->remove("quxx"); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), undef, "baz no longer in cache" ); is( $c->get("zot"), "quxx", "zot still in cache" ); is( $c->get("quxx"), undef, "quxx removed from cache" ); is( $c->_index_size, 2, "two elements in cache" ); $c->set( quxx => "blah" ); is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), undef, "baz no longer in cache" ); is( $c->get("zot"), "quxx", "zot still in cache" ); is( $c->get("quxx"), "blah", "quxx in cache" ); if ( $c->isa("Cache::Ref::CLOCK") ) { $c->set( new => "element" ); # overwrites 'zot' due to current value of _hand $c->hit("bar"); $c->set( another => "member" ); } else { $c->hit("bar") for 1 .. 3; $c->set("new" => "element"); # expires 'quxx' $c->hit("new"); # otherwise it's less frequently used than 'zot' $c->set("another" => "member"); } is( $c->get("bar"), "lala", "bar still in cache" ); is( $c->get("baz"), undef, "baz no longer in cache" ); is( $c->get("zot"), undef,, "zot no longer in cache" ); is( $c->get("quxx"), undef, "quxx no longer in cache" ); is( $c->get("new"), "element", "new still in cache" ); is( $c->get("another"), "member", "another still in cache" ); is_deeply( [ $c->get(qw(bar new nothere)) ], [ qw(lala element), undef ], "mget" ); is( $c->_index_size, 3, "cache size" ); $c->expire(2); is( $c->_index_size, 1, "expired" ); $c->clear; is( $c->_index_size, 0, "no elements in cache" ); } { my $c = $impl->new( size => 5 ); my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 2000 ) { my $key = 1 + int rand 8; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } cmp_ok( $hit, '>=', $miss, "more cache hits than misses during random access of small sigma ($hit >= $miss)" ); ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { foreach my $key ( 1 .. 10 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '<=', $c->size * 3, "no significant hits during linear scans ($hit)" ); } } done_testing; # ex: set sw=4 et: META.json000644001750001750 1505211446730246 14716 0ustar00brunovbrunov000000000000Cache-Ref-0.04{ "abstract" : "Memory only cache of live references", "author" : [ "Yuval Kogman" ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 4.102341, CPAN::Meta::Converter version 2.102400", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Cache-Ref", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.31" } }, "runtime" : { "requires" : { "Carp" : 0, "Hash::Util::FieldHash::Compat" : 0, "List::Util" : 0, "Moose" : 0, "Moose::Role" : 0, "MooseX::Role::Parameterized" : 0, "Scalar::Util" : 0, "namespace::autoclean" : 0 } }, "test" : { "requires" : { "Test::Moose" : 0, "Test::More" : 0, "ok" : 0 } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "mailto" : "bug-Cache-Ref@rt.cpan.org", "web" : "http://rt.cpan.org/Public/Dist/Display.html?Name=Cache-Ref" }, "homepage" : "http://search.cpan.org/dist/Cache-Ref", "repository" : { "type" : "git", "url" : "git://github.com/nothingmuch/cache-ref.git", "web" : "http://github.com/nothingmuch/cache-ref" } }, "version" : "0.04", "x_Dist_Zilla" : { "plugins" : [ { "class" : "Dist::Zilla::Plugin::GatherDir", "name" : "@Filter/@Basic/GatherDir", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::PruneCruft", "name" : "@Filter/@Basic/PruneCruft", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::ManifestSkip", "name" : "@Filter/@Basic/ManifestSkip", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::MetaYAML", "name" : "@Filter/@Basic/MetaYAML", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::License", "name" : "@Filter/@Basic/License", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::Readme", "name" : "@Filter/@Basic/Readme", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::ExtraTests", "name" : "@Filter/@Basic/ExtraTests", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::ExecDir", "name" : "@Filter/@Basic/ExecDir", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::ShareDir", "name" : "@Filter/@Basic/ShareDir", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::MakeMaker", "name" : "@Filter/@Basic/MakeMaker", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::Manifest", "name" : "@Filter/@Basic/Manifest", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::TestRelease", "name" : "@Filter/@Basic/TestRelease", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::ConfirmRelease", "name" : "@Filter/@Basic/ConfirmRelease", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::UploadToCPAN", "name" : "@Filter/@Basic/UploadToCPAN", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::MetaConfig", "name" : "@Filter/MetaConfig", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::MetaJSON", "name" : "@Filter/MetaJSON", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::PkgVersion", "name" : "@Filter/PkgVersion", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::PodSyntaxTests", "name" : "@Filter/PodSyntaxTests", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::NoTabsTests", "name" : "@Filter/NoTabsTests", "version" : "0.01" }, { "class" : "Dist::Zilla::Plugin::PodCoverageTests", "name" : "@Filter/PodCoverageTests", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::MetaResources", "name" : "@Filter/MetaResources", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::Authority", "name" : "@Filter/Authority", "version" : "1.000" }, { "class" : "Dist::Zilla::Plugin::EOLTests", "name" : "@Filter/EOLTests", "version" : "0.02" }, { "class" : "Dist::Zilla::Plugin::PodWeaver", "name" : "@Filter/PodWeaver", "version" : "3.101640" }, { "class" : "Dist::Zilla::Plugin::AutoPrereq", "name" : "@Filter/AutoPrereq", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::UploadToCPAN", "name" : "UploadToCPAN", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::ConfirmRelease", "name" : "ConfirmRelease", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::FinderCode", "name" : ":InstallModules", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::FinderCode", "name" : ":TestFiles", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::FinderCode", "name" : ":ExecFiles", "version" : "4.102341" }, { "class" : "Dist::Zilla::Plugin::FinderCode", "name" : ":ShareFiles", "version" : "4.102341" } ], "zilla" : { "class" : "Dist::Zilla::Dist::Builder", "config" : { "is_trial" : 0 }, "version" : "4.102341" } }, "x_authority" : "cpan:NUFFIN" } random.t000644001750001750 226611446730246 15170 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use ok 'Cache::Ref::Random'; my $c = Cache::Ref::Random->new( size => 5 ); my ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 2000 ) { my $key = 1 + int rand 8; if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } cmp_ok( $hit, '>=', $miss, "more cache hits than misses during random access of small sigma ($hit >= $miss)" ); ( $hit, $miss ) = ( 0, 0 ); for ( 1 .. 100 ) { foreach my $key ( 1 .. 8 ) { if ( $c->get($key) ) { $hit++; } else { $miss++; $c->set($key => $key); } } } cmp_ok( $hit, '>=', $miss / 3, "hit rate in linear scans($hit >= $miss / 3)" ); { # Tests shouldn't use a private method, but right now I can't think # of other way to test that the expire method is actually removing # the desired number of elements my $c = Cache::Ref::Random->new( size => 100 ); for (1..10) { $c->set( $_ => $_ ) for (1..100); is $c->_index_size, 100; $c->expire(50); is $c->_index_size, 50; $c->clear; } } done_testing; # ex: set sw=4 et: Makefile.PL000644001750001750 234011446730246 15223 0ustar00brunovbrunov000000000000Cache-Ref-0.04 use strict; use warnings; use ExtUtils::MakeMaker 6.31; my %WriteMakefileArgs = ( 'ABSTRACT' => 'Memory only cache of live references', 'AUTHOR' => 'Yuval Kogman', 'BUILD_REQUIRES' => { 'Test::Moose' => '0', 'Test::More' => '0', 'ok' => '0' }, 'CONFIGURE_REQUIRES' => { 'ExtUtils::MakeMaker' => '6.31' }, 'DISTNAME' => 'Cache-Ref', 'EXE_FILES' => [], 'LICENSE' => 'perl', 'NAME' => 'Cache::Ref', 'PREREQ_PM' => { 'Carp' => '0', 'Hash::Util::FieldHash::Compat' => '0', 'List::Util' => '0', 'Moose' => '0', 'Moose::Role' => '0', 'MooseX::Role::Parameterized' => '0', 'Scalar::Util' => '0', 'namespace::autoclean' => '0' }, 'VERSION' => '0.04', 'test' => { 'TESTS' => 't/*.t' } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); util_lru.t000644001750001750 625311446730246 15547 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!/usr/bin/perl use strict; use warnings; use Test::More; use Test::Moose; #foreach my $impl qw(Cache::Ref::Util::LRU::Array Cache::Ref::Util::LRU::List) { foreach my $impl (qw(Cache::Ref::Util::LRU::List)) { use_ok($impl); isa_ok( my $l = $impl->new, $impl ); does_ok( $l, "Cache::Ref::Util::LRU::API" ); is( $l->lru, undef, "no mru" ); is( $l->mru, undef, "no lru" ); my ( $foo, $bar ) = $l->insert(qw(foo bar)); is( $l->mru, "foo", "get mru" ); is( $l->lru, "bar", "get lru" ); $l->hit($bar); is( $l->mru, "bar", "get mru" ); is( $l->lru, "foo", "get lru" ); my $baz = $l->insert("baz"); is( $l->mru, "baz", "get mru" ); is( $l->lru, "foo", "get lru" ); $l->hit($bar); is( $l->mru, "bar", "get mru" ); is( $l->lru, "foo", "get lru" ); $l->hit($foo); is( $l->mru, "foo", "get mru" ); is( $l->lru, "baz", "get lru" ); is( $l->remove_lru, "baz", "remove lru" ); is( $l->remove_lru, "bar", "remove lru" ); is( $l->remove_lru, "foo", "remove lru" ); is( $l->remove_lru, undef, "remove lru" ); foreach my $remove ( qw(remove_lru remove_mru) ) { $l->insert("foo"); is( $l->lru, "foo", "lru" ); is( $l->mru, "foo", "mru" ); is( $l->$remove, "foo", "remove" ); is( $l->remove_lru, undef, "nothing to remove" ); is( $l->remove_mru, undef, "nothing to remove" ); } $foo = $l->insert("foo"); $bar = $l->insert("bar"); $baz = $l->insert("baz"); $l->hit($baz); is( $l->mru, "baz", "get mru" ); is( $l->lru, "foo", "get lru" ); $l->hit($bar); is( $l->mru, "bar", "get mru" ); is( $l->lru, "foo", "get lru" ); $l->hit($foo); is( $l->mru, "foo", "get mru" ); is( $l->lru, "baz", "get lru" ); $l->hit($baz); is( $l->mru, "baz", "get mru" ); is( $l->lru, "bar", "get lru" ); $l->hit($foo, $bar); is( $l->mru, "foo", "get mru" ); is( $l->lru, "baz", "get lru" ); $l->hit($bar, $baz); is( $l->mru, "bar", "get mru" ); is( $l->lru, "foo", "get lru" ); $l->hit($baz, $foo, $bar); is( $l->remove_lru, "bar", "remove lru" ); is( $l->lru, "foo", "get lru" ); $l->hit($foo); is( $l->mru, "foo", "get mru" ); is( $l->lru, "baz", "get lru" ); is( $l->remove_mru, "foo", "remove mru" ); is( $l->mru, "baz", "get mru" ); is( $l->lru, "baz", "get lru" ); is( $l->remove_mru, "baz" ); is( $l->lru, undef, "no lru" ); is( $l->mru, undef, "no mru" ); ( $foo, $bar, $baz ) = $l->insert(qw(foo bar baz)); $l->remove($bar); is( $l->mru, "foo", "get mru" ); is( $l->lru, "baz", "get lru" ); $l->remove($foo); is( $l->mru, "baz", "get lru" ); is( $l->lru, "baz", "get lru" ); $l->remove($baz); is( $l->lru, undef, "no lru" ); is( $l->mru, undef, "no mru" ); $l->insert(qw(foo bar)); $l->hit; $l->insert; is( $l->mru, "foo", "get mru" ); is( $l->lru, "bar", "get lru" ); $l->clear; is( $l->lru, undef, "no lru" ); is( $l->mru, undef, "no mru" ); $l->clear; is( $l->lru, undef, "no lru" ); is( $l->mru, undef, "no mru" ); } done_testing; # ex: set sw=4 et: release-eol.t000644001750001750 47611446730246 16066 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use strict; use warnings; use Test::More; eval 'use Test::EOL'; plan skip_all => 'Test::EOL required' if $@; all_perl_files_ok({ trailing_whitespace => 1 }); Cache000755001750001750 011446730246 14664 5ustar00brunovbrunov000000000000Cache-Ref-0.04/libRef.pm000644001750001750 1361711446730246 16125 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cachepackage Cache::Ref; BEGIN { $Cache::Ref::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::VERSION = '0.04'; } # ABSTRACT: Memory only cache of live references use Moose; __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref - Memory only cache of live references =head1 SYNOPSIS # this class is just a base class and a documentation start point # just use the various algorithms directly use Cache::Ref::CART; my $cache = Cache::Ref::CART->new( size => 1024 ); # add a cache value or set an existing key to a new value $cache->set(foo => $some_object); # get a value $cache->get("foo"); # also takes a list of keys # remove a key before it has normally expired $cache->remove("foo"); # remove all cached data $cache->clear; # 'hit' is like 'get' without the overhead of obtaining the value # it's useful for keeping values from expiring when you already have # the values $cache->hit("foo"); # also takes a list of keys =head1 DESCRIPTION Unlike L which attempts to address the problem of caching things persistently, this module implements in memory caching, designed primarily for B in memory. This collection of classes implements a number of semi related algorithms. =head1 METHODS =over 4 =item get @keys Fetch entries from the cache. =item hit @keys Promote C<@keys> in the cache. Same effect as C except it doesn't actually return anything. =item set $key => $value Adds an entry to the cache. =item compute $key, sub { ...; return $value } Calls C with C<$key>. If there's a hit the value is returned. Otherwise the code block is executed to compute the value, and the result is stored in the cache using C. =item remove @keys Remove specific entries from the cache. =item expire $x Remove C<$x> many entries from the cache. Hopefully the entries removed are the most useless ones. C<$x> defaults to 1. =item clear Empty the cache. =back =head1 ALGORITHMS =head2 FIFO This is a simple FIFO queue where a C places the element on the head of a queue, and if the size is too big an element will be discarded from the tail of the queue. L provides similar behavior, but flushing happens periodically and in bigger numbers. Therefore, performance will be better on very high cache usage, when hits don't matter that much. This implementation has the lowest memory overhead, due to the simplicity of its data structures (just a hash and an array). Its expiry policy is appropriate for when the data set has a high locality of reference, and random access is generally confined to neighbors, as a part of some larger scan. For truly random access cache hit rates will suffer. Long term utility of cache entries is not considered at all, so scans will poison the cache. This is the only algorithm for which C (and C) has no side effects. =head2 LRU This implementation uses an LRU list of entries (two implementations are provided for trading off memory for speed). Long term utility of cache entries is not considered at all, so scans will poison the cache. =head3 Cache::Ref::Util::LRU::List Uses a doubly linked list to perform MRU propagation. Faster than Array. Cache hits and LRU removal is O(1). =head3 Cache::Ref::Util::LRU::Array Generally slower for a cache size bigger than about 10 elements, but uses less memory due to the compact layout. Cache hits are O(cache size). LRU removal is O(1). =head2 CLOCK This is an implementation of second chance FIFO, using a circular buffer. Second chance FIFO is a very simple approximation of LRU. The CLOCK algorithm has its origins in Multics' virtual memory paging implementation. It's slightly more general purpose than FIFO when dealing with random access. Long term utility of cache entries is not considered at all, so scans will poison the cache. Using values of C bigger than 1 (the default), more accurate approximations of LRU can be made, at the cost of more complicated expiry. =head2 GCLOCK Tries to approximate LFU instead of LRU. Cache hits increment a counter by one, instead of resetting it to the constant C. Cache replacement decays existing counters just like CLOCK. =head2 CAR CLOCK with Adaptive Removal. A self tuning cache that varies between approximations of LRU and LFU expiry. Has the highest memory overhead of all the implementations due to the extent of the metadata it maintains. However, this overhead is still small for when sizeable objects are involved. Resistent to cache poisoning when scanning. =head2 CART CAR with temporal filtering. Like CAR but does not promote a cache entry to the long term usefulness set due to frequent successive access. This is probably the most general purpose algorithm. =head1 SEE ALSO =over 4 =item L Appropriate for persistent caching of data with complex expiry. =item L Can be used to layer L over other caches (e.g. L). =item L A simpler implementation with similar goals (memory only caching), designed for when cache misses are not very high cost, so cache hits have an extremely low overhead and the policy is very simplistic. =item L Caches shared references for as long as there is some other reference to those objects. =item L Designed to help choose an appropriate cache layer. =item Algorithm information L L L =back =head1 VERSION CONTROL L =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut release-no-tabs.t000644001750001750 45011446730246 16642 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use strict; use warnings; use Test::More; eval 'use Test::NoTabs'; plan skip_all => 'Test::NoTabs required' if $@; all_perl_files_ok(); Ref000755001750001750 011446730246 15400 5ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/CacheLRU.pm000644001750001750 504511446730246 16543 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::LRU; BEGIN { $Cache::Ref::LRU::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::LRU::VERSION = '0.04'; } # ABSTRACT: Least recently used expiry policy use Moose; use Cache::Ref::Util::LRU::List; use namespace::autoclean; extends qw(Cache::Ref); with qw( Cache::Ref::Role::API Cache::Ref::Role::Index ); has size => ( isa => "Int", is => "ro", required => 1, ); has lru_class => ( isa => "ClassName", is => "ro", default => "Cache::Ref::Util::LRU::List", ); has _lru => ( does => "Cache::Ref::Util::LRU::API", is => "ro", lazy_build => 1, ); sub _build__lru { shift->lru_class->new } sub get { my ( $self, @keys ) = @_; my @e = $self->_index_get(@keys); $self->_lru->hit(map { $_->[1] } grep { defined } @e); return ( @keys == 1 ? $e[0][0] : map { $_ && $_->[0] } @e ); } sub hit { my ( $self, @keys ) = @_; $self->_lru->hit( map { $_->[1] } $self->_index_get(@keys) ); return; } sub expire { my ( $self, $how_many ) = @_; my $l = $self->_lru; $self->_index_delete( $l->remove_lru ) for 1 .. ($how_many || 1); return; } sub set { my ( $self, $key, $value ) = @_; my $l = $self->_lru; if ( my $e = $self->_index_get($key) ) { $l->hit($e->[1]); $e->[0] = $value; } else { if ( $self->_index_size == $self->size ) { $self->expire(1); } $self->_index_set( $key => [ $value, $l->insert($key) ] ); } return $value; } sub clear { my $self = shift; $self->_lru->clear; $self->_index_clear; return; } sub remove { my ( $self, @keys ) = @_; $self->_lru->remove(map { $_->[1] } $self->_index_delete(@keys)); return; } __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::LRU - Least recently used expiry policy =head1 SYNOPSIS my $c = Cache::Ref::LRU->new( size => $n, ); =head1 DESCRIPTION This is an implementation of the least recently used expiry policy. It provides both an array and a doubly linked list based implementation. See L for a discussion. =head1 ATTRIBUTES =over 4 =item size The size of the live entries. =item lru_class The class of the LRU list implementation. =back =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut CAR.pm000644001750001750 1164711446730246 16533 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::CAR; BEGIN { $Cache::Ref::CAR::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::CAR::VERSION = '0.04'; } # ABSTRACT: CLOCK with Adaptive Replacement use Moose; use List::Util qw(max min); use Cache::Ref::CAR::Base (); use namespace::autoclean; extends qw(Cache::Ref); with qw(Cache::Ref::CAR::Base); sub _mru_history_too_big { my $self = shift; $self->_mru_history_size and $self->_mru_history_size + $self->_mru_size == $self->size; } sub _mfu_history_too_big { my $self = shift; $self->_index_size == $self->size * 2; } sub _decrease_mru_target_size { my $self = shift; if ( $self->_mru_target_size > 0 ) { my $adjustment = int( $self->_mru_history_size / $self->_mfu_history_size ); $self->_set_mru_target_size( max( 0, $self->_mru_target_size - max(1, $adjustment) ) ); } } sub _increase_mru_target_size { my $self = shift; my $adjustment = int( $self->_mfu_history_size / $self->_mru_history_size ); $self->_set_mru_target_size( min( $self->size, $self->_mru_target_size + max(1, $adjustment) ) ); } sub _restore_from_mfu_history { my ( $self, $e ) = @_; $self->_mfu_push($e); } sub _restore_from_mru_history { my ( $self, $e ) = @_; $self->_mfu_push($e); } sub expire { my ( $self, $how_many ) = @_; $how_many ||= 1; if ( my $mru = $self->_mru ) { my $cur = $self->_next($mru); # mru pool is too big while ( $cur and $self->_mru_size >= max(1,$self->_mru_target_size) ) { my $next = $self->_next($cur); $self->_circular_splice($cur); if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) { $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; # turn off reference bit # move to t2 (mfu) $self->_mfu_push($cur); $cur = $next; } else { # reference bit is off, which means this entry is freeable delete $cur->[2]; # delete the value # move to history # MFU_BIT not set if ( $self->_mru_history_head ) { $self->_set_next($cur, $self->_mru_history_head); $self->_set_prev($self->_mru_history_head, $cur); } else { $self->_set_next($cur, undef); } $self->_mru_history_head($cur); $self->_mru_history_tail($cur) unless $self->_mru_history_tail; $self->_inc_mru_history_size; return; } } } for ( 1 .. $how_many ) { my $tail = $self->_mfu || last; my $cur = $self->_next($tail) || last; loop: { if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) { $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; $tail = $cur; $cur = $self->_next($cur); redo loop; } else { # reference bit is off, which means this entry is freeable $self->_mfu($tail); $self->_circular_splice($cur); delete $cur->[2]; # delete the value # move to history $cur->[0] |= Cache::Ref::CAR::Base::MFU_BIT; if ( $self->_mfu_history_head ) { $self->_set_prev($self->_mfu_history_head, $cur); $self->_set_next($cur, $self->_mfu_history_head); } else { $self->_set_next($cur, undef); } $self->_mfu_history_head($cur); $self->_mfu_history_tail($cur) unless $self->_mfu_history_tail; $self->_inc_mfu_history_size; } } } return; } sub _clear_additional { } __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::CAR - CLOCK with Adaptive Replacement =head1 SYNOPSIS my $c = Cache::Ref::CAR->new( size => $n, ); =head1 DESCRIPTION This algorithm is an implementation of L. See also L which is probably more appropriate for random access work loads. CAR balances between an MFU like policy and an MRU like policy, automatically tuning itself as the workload varies. =head1 ATTRIBUTES =over 4 =item size The size of the live entries. Note that the cache also remembers this many expired keys, and keeps some metadata about those keys, so for memory usage the overhead is probably around double what L requires. =back =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut Null.pm000644001750001750 247511446730246 17017 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::Null; BEGIN { $Cache::Ref::Null::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Null::VERSION = '0.04'; } # ABSTRACT: Caches nothing use Moose; use namespace::autoclean; extends qw(Cache::Ref); with qw(Cache::Ref::Role::API); sub get { return } sub hit { return } sub set { return } sub remove { return } sub clear { return } sub expire { return } sub compute { return } __PACKAGE__->meta->make_immutable; __PACKAGE__; =pod =encoding utf-8 =head1 NAME Cache::Ref::Null - Caches nothing =head1 SYNOPSIS # useful for comparing the effect of a cache compared to no # caching without code changes: my $c = Cache::Profile::Compare->new( caches => [ Cache::Ref::Foo->new( ... ), Cache::Ref->Null->new, ], ); =head1 DESCRIPTION This cache implementation will cache nothing. This is primarily intended for testing or comparing runtime without a cache against runtime with a cache. It's like L but supports the additional methods in L. =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut __END__ # ex: set sw=4 et: LIFO.pm000644001750001750 461511446730246 16634 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::LIFO; BEGIN { $Cache::Ref::LIFO::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::LIFO::VERSION = '0.04'; } # ABSTRACT: Saves entries until full, discarding subsequent sets. use Moose; use namespace::autoclean; extends qw(Cache::Ref); with ( 'Cache::Ref::Role::API', 'Cache::Ref::Role::Index' => { -alias => { _index_get => "get", }, }, ); has size => ( isa => "Int", is => "ro", required => 1, ); has _keys => ( traits => [qw(Array)], isa => "ArrayRef", is => "ro", default => sub { [] }, handles => { _add_key => "push", #_splice_keys => "splice", _clear_keys => "clear", }, ); sub remove { my ( $self, @keys ) = @_; $self->_index_delete(@keys); my %keys; undef @keys{@keys}; @{ $self->_keys } = grep { not exists $keys{$_} } @{ $self->_keys }; return; } sub clear { my $self = shift; $self->_index_clear; $self->_clear_keys; return; } sub hit { } sub set { my ( $self, $key, $value ) = @_; if ( defined $self->_index_get($key) ) { $self->_index_set($key, $value) } elsif ( $self->_index_size < $self->size ) { $self->_index_set($key, $value); $self->_add_key($key); } return $value; } sub expire { my ( $self, $how_many ) = @_; $how_many ||= 1; #my @keys = $self->_splice_keys( -$how_many ); my @keys = splice @{ $self->_keys }, -$how_many; $self->_index_delete(@keys); } __PACKAGE__->meta->make_immutable; __PACKAGE__; =pod =encoding utf-8 =head1 NAME Cache::Ref::LIFO - Saves entries until full, discarding subsequent sets. =head1 SYNOPSIS my $c = Cache::Ref::LIFO->new( size => $n ); $c->set( foo => 42 ); $c->get("foo"); =head1 DESCRIPTION This is a very naive cache algorithm, it saves cache sets until the cache is full, at which point all additional saves which aren't a value update are discarded immediately. For very predictable workflows this is potentially a good fit, provided the MFU is used early on. The advantages is that the code is very simple as a result. =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut __END__ # ex: set sw=4 et: CART.pm000644001750001750 1711711446730246 16655 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::CART; BEGIN { $Cache::Ref::CART::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::CART::VERSION = '0.04'; } # ABSTRACT: CAR with temporal filtering use Moose; use List::Util qw(max min); use Cache::Ref::CAR::Base (); use namespace::autoclean; extends qw(Cache::Ref); with qw(Cache::Ref::CAR::Base); has _long_term_utility_size => ( # q is => "ro", writer => "_set_long_term_utility_size", default => 0, ); has _mru_history_target_size => ( # q is => "ro", writer => "_set_mru_history_target_size", default => 0, ); sub _inc_long_term_utility_size { my $self = shift; $self->_set_long_term_utility_size( $self->_long_term_utility_size + 1 ); } sub _dec_long_term_utility_size { my $self = shift; $self->_set_long_term_utility_size( $self->_long_term_utility_size - 1 ); } sub _reset_long_term_utility_size { my $self = shift; $self->_set_long_term_utility_size(0); } sub _reset_mru_history_target_size { my $self = shift; $self->_set_mru_history_target_size(0); } sub _mru_history_too_big { my $self = shift; # only if there's something to purge return unless $self->_mru_history_size; # only if we need to purge return unless $self->_mru_history_size + $self->_mfu_history_size == $self->size + 1; # purge from here if there's nothing to purge from mfu return 1 if $self->_mfu_history_size == 0; # or if the target size is too big return 1 if $self->_mru_history_size > $self->_mru_history_target_size; return; } sub _mfu_history_too_big { my $self = shift; return unless $self->_mfu_history_size; # only purge if we actually need to return 1 if $self->_mru_history_size + $self->_mfu_history_size == $self->size + 1; return; } sub _increase_mru_target_size { my $self = shift; my $adjustment = int( ( $self->_mru_size + $self->_mfu_size - $self->_long_term_utility_size ) / $self->_mru_history_size ); $self->_set_mru_target_size( min( $self->size, $self->_mru_target_size + max(1, $adjustment) ) ); } sub _decrease_mru_target_size { my $self = shift; my $adjustment = int( $self->_long_term_utility_size / $self->_mfu_history_size ); $self->_set_mru_target_size( max( 0, $self->_mru_target_size - max(1, $adjustment) ) ); } sub _increase_mru_history_target_size { my $self = shift; $self->_set_mru_history_target_size( min($self->_mru_history_target_size + 1, 2 * $self->size - $self->_mru_size ) ); } sub _decrease_mru_history_target_size { my $self = shift; $self->_set_mru_history_target_size( max($self->_mru_history_target_size - 1, $self->size - $self->_mru_size) ); } sub _restore_from_mfu_history { my ( $self, $e ) = @_; if ( $self->_mfu_history_size + $self->_long_term_utility_size >= $self->size ) { $self->_increase_mru_history_target_size(); } die unless $e->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT(); $self->_inc_long_term_utility_size(); $self->_mru_push($e); } sub _restore_from_mru_history { my ( $self, $e ) = @_; $e->[0] |= Cache::Ref::CAR::Base::LONG_TERM_BIT(); $self->_inc_long_term_utility_size(); $self->_mru_push($e); } sub expire { my ( $self, $how_many ) = @_; $how_many ||= 1; if ( my $mfu = $self->_mfu ) { my $cur = $self->_next($mfu); # mru pool is too big while ( $cur and $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) { $self->_circular_splice($cur); $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; # turn off reference bit # move to t1 (mru) $self->_mru_push($cur); $cur = $self->_next($cur); if ( $self->_mfu_history_size + $self->_long_term_utility_size >= $self->size ) { $self->_increase_mru_history_target_size; } } } if ( my $mru = $self->_mru ) { my $cur = $self->_next($mru); while ( $cur ) { if ( $cur->[0] & Cache::Ref::CAR::Base::REF_BIT ) { $cur->[0] &= ~Cache::Ref::CAR::Base::REF_BIT; if ( $self->_mru_size >= max($self->_mru_history_size, $self->_mru_target_size + 1) and not( $cur->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT ) ) { # FIXME spec says 'x', is this the same as 'head'? $cur->[0] |= Cache::Ref::CAR::Base::LONG_TERM_BIT; $self->_inc_long_term_utility_size; } # $hand++ $self->_mru($cur); $cur = $self->_next($cur); } elsif ( $cur->[0] & Cache::Ref::CAR::Base::LONG_TERM_BIT ) { my $next = $self->_next($cur); $self->_circular_splice($cur); $self->_mfu_push($cur); $cur = $self->_next($self->_mru);; $self->_decrease_mru_history_target_size(); } else { # found a candidate page for removal last; } } } for ( 1 .. $how_many ) { if ( $self->_mru_size >= max(1, $self->_mru_target_size) ) { my $head = $self->_next($self->_mru); $self->_circular_splice($head); if ( $self->_mru_history_head ) { $self->_set_next($head, $self->_mru_history_head); $self->_set_prev($self->_mru_history_head, $head); } $self->_mru_history_head($head); $self->_mru_history_tail($head) unless $self->_mru_history_tail; $self->_inc_mru_history_size; delete $head->[2]; # delete the value } else { my $tail = $self->_mfu || last; my $head = $self->_next($tail) || last; $self->_circular_splice($head); $self->_dec_long_term_utility_size; # entries in mfu *always* have long term set if ( $self->_mfu_history_head ) { $self->_set_next($head, $self->_mfu_history_head); $self->_set_prev($self->_mfu_history_head, $head); } $self->_mfu_history_head($head); $self->_mfu_history_tail($head) unless $self->_mfu_history_tail; $self->_inc_mfu_history_size; delete $head->[2]; # delete the value $head->[0] |= Cache::Ref::CAR::Base::MFU_BIT; } } return; } sub _clear_additional { my $self = shift; $self->_reset_long_term_utility_size; $self->_reset_mru_history_target_size; } __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::CART - CAR with temporal filtering =head1 SYNOPSIS my $c = Cache::Ref::CART->new( size => $n, ); =head1 DESCRIPTION This algorithm is an extension to L that has temporal filtering on the upgrading from MRU to MFU pool. This means that two subsequent accesses to the same key do not automatically make it viable for long term caching, to get upgraded to MFU status a key must be expired but known in the history. This is probably the most general purpose caching algorithm. =head1 ATTRIBUTES =over 4 =item size The size of the live entries. Note that the cache also remembers this many expired keys, and keeps some metadata about those keys, so for memory usage the overhead is probably around double what L requires. =back =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FIFO.pm000644001750001750 315711446730246 16626 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::FIFO; BEGIN { $Cache::Ref::FIFO::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::FIFO::VERSION = '0.04'; } use Moose; use namespace::autoclean; extends qw(Cache::Ref); with qw( Cache::Ref::Role::API Cache::Ref::Role::Index ); has size => ( isa => "Int", is => "ro", required => 1, ); has _fifo => ( isa => "ArrayRef", is => "ro", lazy => 1, default => sub { [] }, ); sub clear { my $self = shift; $self->_index_clear; @{ $self->_fifo } = (); } sub hit { } sub remove { my ( $self, @keys ) = @_; $self->_index_delete(@keys); my %keys; @keys{@keys} = (); my $f = $self->_fifo; @$f = grep { not exists $keys{$_} } @$f; return; } sub get { my ( $self, @keys ) = @_; $self->_index_get(@keys); } sub set { my ( $self, $key, $value ) = @_; unless ( defined $self->_index_get($key) ) { if ( $self->_index_size >= $self->size ) { $self->expire( 1 + $self->_index_size - $self->size ); } push @{ $self->_fifo }, $key; } $self->_index_set($key, $value); } sub expire { my ( $self, $how_many ) = @_; $self->_index_delete( splice @{ $self->_fifo }, 0, $how_many || 1 ); return; } __PACKAGE__->meta->make_immutable; # ex: set sw=4 et: __PACKAGE__; __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::FIFO =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut CLOCK.pm000644001750001750 343411446730246 16734 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::CLOCK; BEGIN { $Cache::Ref::CLOCK::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::CLOCK::VERSION = '0.04'; } # ABSTRACT: CLOCK cache replacement algorithm use Moose; use namespace::autoclean; extends qw(Cache::Ref); with qw(Cache::Ref::CLOCK::Base); has k => ( isa => "Int", is => "ro", default => 1, ); sub _hit { my ( $self, $e ) = @_; my $k = 0+$self->k; # moose stingifies default $_->[0] = $k for @$e; } __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::CLOCK - CLOCK cache replacement algorithm =head1 SYNOPSIS my $c = Cache::Ref::CLOCK->new( size => $n, k => $k, ); =head1 DESCRIPTION This algorithm is provides a second chance FIFO cache expiry policy using a circular buffer. It is a very well accepted page replacement algorithm, but largely for reasons which are irrelevant in this context (cache hits don't need to be serialized in a multiprocessing context as they only require an idempotent operation (setting a bit to 1)). =head1 ATTRIBUTES =over 4 =item size The size of the live entries. =item k This is the initial value given to all hit entries. As the hand moves through the circular buffer it decrements the counters. The default is C<1>, providing semantics similar to a second chance FIFO cache. Larger values of C model LRU more accurately. This is pretty silly though, as L is probably way more efficient for any C bigger than 1. =back =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut release-pod-syntax.t000644001750001750 45011446730246 17405 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod 1.41"; plan skip_all => "Test::Pod 1.41 required for testing POD" if $@; all_pod_files_ok(); GCLOCK.pm000644001750001750 226411446730246 17043 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::GCLOCK; BEGIN { $Cache::Ref::GCLOCK::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::GCLOCK::VERSION = '0.04'; } # ABSTRACT: GCLOCK cache replacement algorithm use Moose; use namespace::autoclean; extends qw(Cache::Ref); with qw(Cache::Ref::CLOCK::Base); sub _hit { my ( $self, $e ) = @_; $_->[0]++ for @$e; } __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::GCLOCK - GCLOCK cache replacement algorithm =head1 SYNOPSIS my $c = Cache::Ref::GCLOCK->new( size => $n, ); =head1 DESCRIPTION This algorithm is related to L but instead of starting all cache hits from C, a counter is increased on every hit. This provides behavior which models an LFU expiry policy (without taking into account the full keyspace). =head1 ATTRIBUTES =over 4 =item size The size of the live entries. =back =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut Random.pm000644001750001750 274411446730246 17324 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Refpackage Cache::Ref::Random; BEGIN { $Cache::Ref::Random::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Random::VERSION = '0.04'; } use Moose; use namespace::autoclean; extends qw(Cache::Ref); with qw( Cache::Ref::Role::API Cache::Ref::Role::Index ); has size => ( isa => "Int", is => "ro", required => 1, ); sub clear { my $self = shift; $self->_index_clear; } sub hit { } sub remove { my ( $self, @keys ) = @_; $self->_index_delete(@keys); return; } sub get { my ( $self, @keys ) = @_; $self->_index_get(@keys); } sub set { my ( $self, $key, $value ) = @_; unless ( defined $self->_index_get($key) ) { if ( $self->_index_size >= $self->size ) { $self->expire( 1 + $self->_index_size - $self->size ); } } $self->_index_set($key, $value); } sub expire { my ( $self, $how_many ) = @_; my @s = ( 0 .. $self->_index_size - 1); my @slice = map { splice @s, int rand @s, 1 } 1 .. ($how_many || 1); my @keys = ($self->_index_keys)[@slice]; $self->_index_delete(@keys); return; } __PACKAGE__->meta->make_immutable; __PACKAGE__; =pod =encoding utf-8 =head1 NAME Cache::Ref::Random =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut __END__ # ex: set sw=4 et: release-pod-coverage.t000644001750001750 76511446730246 17663 0ustar00brunovbrunov000000000000Cache-Ref-0.04/t#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod::Coverage 1.08"; plan skip_all => "Test::Pod::Coverage 1.08 required for testing POD coverage" if $@; eval "use Pod::Coverage::TrustPod"; plan skip_all => "Pod::Coverage::TrustPod required for testing POD coverage" if $@; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); CAR000755001750001750 011446730246 16005 5ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/RefBase.pm000644001750001750 2310011446730246 17370 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/CARpackage Cache::Ref::CAR::Base; BEGIN { $Cache::Ref::CAR::Base::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::CAR::Base::VERSION = '0.04'; } # ABSTRACT: base clase for CAR and CART use Moose::Role; # TODO # this needs lot of cleanup but I ran out of motivation. it works, though. # # the circular buffers should finish being implemented using the doubly linked # list role # # CART needs a bunch of simplifications in the code # # the various linked list APIs should probably be consolidated to respect the # MFU bit, with _move_to_history, _restore_from_history etc checking that # instead of having two methods per each use namespace::autoclean; sub REF_BIT () { 0x01 } sub MFU_BIT () { 0x02 } sub LONG_TERM_BIT () { 0x04 } requires qw( _mru_history_too_big _mfu_history_too_big _restore_from_mfu_history _restore_from_mru_history _clear_additional _decrease_mru_target_size _increase_mru_target_size ); with ( qw( Cache::Ref::Role::API Cache::Ref::Role::Index ), map { ('Cache::Ref::Role::WithDoublyLinkedList' => { # FIXME can it just be circular too? name => $_, value_offset => 1, # the cache key next_offset => 3, prev_offset => 4, }), } qw(_mru_history _mfu_history), # b1, b2 ); sub _next { $_[1][3] } sub _set_next { my ( $self, $node, $next ) = @_; $node->[3] = $next; } sub _prev { $_[1][4] } sub _set_prev { my ( $self, $node, $prev ) = @_; $node->[4] = $prev; } has size => ( isa => "Int", is => "ro", required => 1, ); foreach my $pool (qw(mfu mru)) { # t1, t2 has "_$pool" => ( is => "rw" ); # circular linked list tail foreach my $counter (qw(size history_size)) { has "_${pool}_$counter" => ( #traits => [qw(Counter)], # too slow, not inlined, nytprof gives it about 60% of runtime =P is => "ro", writer => "_set_${pool}_$counter", default => sub { 0 }, #handles => { # "_inc_${pool}_$counter" => "inc", # "_dec_${pool}_$counter" => "dec", # "_reset_${pool}_$counter" => "reset", #}, ); } } sub _reset_mru_size { my $self = shift; $self->_set_mru_size(0); } sub _inc_mru_size { my $self = shift; $self->_set_mru_size( $self->_mru_size + 1 ); } sub _dec_mru_size { my $self = shift; $self->_set_mru_size( $self->_mru_size - 1 ); } sub _reset_mfu_size { my $self = shift; $self->_set_mfu_size(0); } sub _inc_mfu_size { my $self = shift; $self->_set_mfu_size( $self->_mfu_size + 1 ); } sub _dec_mfu_size { my $self = shift; $self->_set_mfu_size( $self->_mfu_size - 1 ); } sub _reset_mru_history_size { my $self = shift; $self->_set_mru_history_size(0); } sub _inc_mru_history_size { my $self = shift; $self->_set_mru_history_size( $self->_mru_history_size + 1 ); } sub _dec_mru_history_size { my $self = shift; $self->_set_mru_history_size( $self->_mru_history_size - 1 ); } sub _reset_mfu_history_size { my $self = shift; $self->_set_mfu_history_size(0); } sub _inc_mfu_history_size { my $self = shift; $self->_set_mfu_history_size( $self->_mfu_history_size + 1 ); } sub _dec_mfu_history_size { my $self = shift; $self->_set_mfu_history_size( $self->_mfu_history_size - 1 ); } has _mru_target_size => ( # p is => "ro", writer => "_set_mru_target_size", default => 0, ); sub hit { my ( $self, @keys ) = @_; $self->_hit( [ grep { defined } $self->_index_get(@keys) ] ); return; } sub peek { my ( $self, @keys ) = @_; my @ret; my @entries = $self->_index_get(@keys); return ( @keys == 1 ? ($entries[0] && $entries[0][2]) : map { $_ && $_->[2] } @entries ); } sub get { my ( $self, @keys ) = @_; my @ret; my @entries = $self->_index_get(@keys); $self->_hit( [ grep { defined } @entries ] ); return ( @keys == 1 ? ($entries[0] && $entries[0][2]) : map { $_ && $_->[2] } @entries ); } sub _circular_splice { my ( $self, $node ) = @_; my $list = $node->[0] & MFU_BIT ? "_mfu" : "_mru"; my $next = $self->_next($node); if ( $next == $node ) { # this is the last element in the list $self->$list(undef); } else { my $prev = $self->_prev($node); $self->_set_next( $prev, $next ); $self->_set_prev( $next, $prev ); if ( $self->$list == $node ) { $self->$list($prev); # only happens on remove() } } $self->_set_next($node, undef); $self->_set_prev($node, undef); $self->${\"_dec${list}_size"}; } sub _mfu_push { my ( $self, $node ) = @_; $node->[0] |= MFU_BIT; $self->_circular_push($node); } sub _mru_push { my ( $self, $node ) = @_; $node->[0] &= ~MFU_BIT; $self->_circular_push($node); } sub _circular_push { my ( $self, $node ) = @_; my $list = $node->[0] & MFU_BIT ? "_mfu" : "_mru"; if ( my $tail = $self->$list ) { my $head = $self->_next($tail); $self->_set_next($tail, $node); $self->_set_prev($node, $tail); $self->_set_next($node, $head); $self->_set_prev($head, $node); } else { $self->_set_next($node, $node); $self->_set_prev($node, $node); } $self->${\"_inc${list}_size"}; # $hand++ $self->$list($node); } sub _hit { my ( $self, $e ) = @_; foreach my $entry ( @$e ) { if ( exists $entry->[2] ) { # if it's in T1 ∪ T2, the value is set $entry->[0] ||= 1; #} else { # cache history hit # has no effect until 'set' } } } sub set { my ( $self, $key, $value ) = @_; my $e = $self->_index_get($key); if ( $e and exists $e->[2] ) { # just a value update $self->_hit([$e]); return $e->[2] = $value; } # the live cache entries are full, we need to expire something if ( $self->_mru_size + $self->_mfu_size == $self->size ) { $self->expire(1); # if the entry wasn't in history we may need to free up something from # there too, to make room for whatever just expired if ( !$e ) { if ( $self->_mru_history_too_big ) { $self->_index_delete( $self->_mru_history_pop ); $self->_dec_mru_history_size; } elsif ( $self->_mfu_history_too_big ) { $self->_index_delete($self->_mfu_history_pop); $self->_dec_mfu_history_size; } } } if ( !$e ) { # cache directory miss # this means the key is neither cached nor recently expired $self->_insert_new_entry( $key, $value ); } else { # cache directory hit # restore from the appropriate history list if ( $e->[0] & MFU_BIT ) { $e->[0] &= ~MFU_BIT; $self->_decrease_mru_target_size(); $self->_mfu_history_splice($e); $self->_dec_mfu_history_size; $self->_restore_from_mfu_history($e); } else { $self->_increase_mru_target_size(); $self->_mru_history_splice($e); $self->_dec_mru_history_size; $self->_restore_from_mru_history($e); } # the entry has a key and flags but no value # it's already indexed currectly, so no need for _index_set $e->[2] = $value; } return $value; } sub _insert_new_entry { my ( $self, $key, $value ) = @_; my $e = [ 0, $key, $value ]; # 0 means no special bits are set # simply insert to the MRU pool $self->_mru_push($e); $self->_index_set( $key => $e ); } sub clear { my $self = shift; $self->_index_clear; $self->_mfu_history_clear; $self->_mru_history_clear; $self->_reset_mru_history_size; $self->_reset_mfu_history_size; $self->_reset_mfu_size; $self->_reset_mru_size; $self->_circular_clear("_mfu"); $self->_circular_clear("_mru"); $self->_clear_additional; return; } sub _circular_clear { my ( $self, $list ) = @_; my $cur = $self->$list; $self->$list(undef); while ( $cur ) { my $next = $cur->[3]; @$cur = (); $cur = $next; } } sub DEMOLISH { shift->clear } sub remove { my ( $self, @keys ) = @_; foreach my $e ( grep { defined } $self->_index_delete(@keys) ) { if ( exists $e->[2] ) { $self->_circular_splice($e); } else { if ( $e->[0] & MFU_BIT ) { $self->_mfu_history_pop; $self->_dec_mfu_history_size; } else { $self->_mru_history_pop; $self->_dec_mru_history_size; } } } return; } __PACKAGE__; __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::CAR::Base - base clase for CAR and CART =head1 SYNOPSIS # see CAR or CART =head1 DESCRIPTION This role provides the common functionality for L and L. =head1 METHODS =over 4 =item get @keys Fetch data from the cache =item set $key, $value Insert data to the cache =item remove @keys Remove entries from the cache. Not in the original CAR algorithm description. =item expire $x Removes C<$x> elements from the cache (hopefully the most useless ones). The default value for C<$x> is 1. =back # ex: set sw=4 et: =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut Role000755001750001750 011446730246 16301 5ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/RefAPI.pm000644001750001750 171611446730246 17414 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/Rolepackage Cache::Ref::Role::API; BEGIN { $Cache::Ref::Role::API::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Role::API::VERSION = '0.04'; } use Moose::Role; use Carp qw(croak); use namespace::autoclean; requires qw( get set remove clear hit expire ); sub compute { my ( $self, $key, $code ) = @_; croak "must specify key and code" unless defined($key) && defined($code); if ( defined( my $cached = $self->get($key) ) ) { return $cached; } else { my $value = $code->(); $self->set( $key => $value ); return $value; } } # ex: set sw=4 et: __PACKAGE__; __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::Role::API =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut Index.pm000644001750001750 247611446730246 20056 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/Rolepackage Cache::Ref::Role::Index; BEGIN { $Cache::Ref::Role::Index::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Role::Index::VERSION = '0.04'; } use Moose::Role; use namespace::autoclean; # the index handles the by key lookup for all expiry methods # the actual entries are set up by the manager though # an entry in the index does not mean the key is live, it only means that it is # known has _index => ( isa => "HashRef", default => sub { return {} }, is => "ro", ); sub _index_clear { my $self = shift; %{ $self->_index } = (); } sub _index_keys { my $self = shift; keys %{ $self->_index }; } sub _index_get { my ( $self, @keys ) = @_; @{ $self->_index }{@keys}; } sub _index_set { my ( $self, $key, $value ) = @_; $self->_index->{$key} = $value; } sub _index_size { my $self = shift; scalar keys %{ $self->_index }; } sub _index_delete { my ( $self, @keys ) = @_; delete @{ $self->_index }{@keys}; } # ex: set sw=4 et: __PACKAGE__; __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::Role::Index =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut CLOCK000755001750001750 011446730246 16233 5ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/RefBase.pm000644001750001750 524611446730246 17611 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/CLOCKpackage Cache::Ref::CLOCK::Base; BEGIN { $Cache::Ref::CLOCK::Base::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::CLOCK::Base::VERSION = '0.04'; } use Moose::Role; use namespace::autoclean; with qw( Cache::Ref::Role::API Cache::Ref::Role::Index ); requires qw(_hit); has size => ( isa => "Int", is => "ro", required => 1, ); has _hand => ( isa => "ScalarRef", is => "ro", default => sub { my $x = -1; return \$x }, ); has _buffer => ( isa => "ArrayRef", is => "ro", lazy => 1, default => sub { my ( $self, $p ) = @_; return [ map { [] } 1 .. $self->size ], }, ); sub hit { my ( $self, @keys ) = @_; $self->_hit( [ grep { defined } $self->_index_get(@keys) ] ); return; } sub get { my ( $self, @keys ) = @_; my @ret; my @entries = $self->_index_get(@keys); $self->_hit( [ grep { defined } @entries ] ); return ( @keys == 1 ? ($entries[0] && $entries[0][2]) : map { $_ && $_->[2] } @entries ); } sub clear { my $self = shift; $self->_index_clear; @$_ = () for @{ $self->_buffer }; } sub remove { my ( $self, @keys ) = @_; @$_ = () for $self->_index_delete(@keys); } sub set { my ( $self, $key, $value ) = @_; if ( my $e = $self->_index_get($key) ) { $e->[2] = $value; } else { my $e = $self->_find_free_slot; @$e = ( 0, $key, $value ); # start at 0, not k $self->_index_set( $key, $e ); } } sub expire { my ( $self, $how_many ) = @_; my $i = $self->_hand; my $b = $self->_buffer; while ( $how_many ) { if ( $$i == $#$b ) { $$i = -1; } if ( my $e = $b->[++$$i] ) { if ( !$e->[0] ) { $self->remove($e->[1]); # also clears @$e $how_many--; } else { $e->[0]--; } } } return; } sub _find_free_slot { my $self = shift; my $i = $self->_hand; my $b = $self->_buffer; loop: { if ( $$i == $#$b ) { $$i = -1; } my $e = $b->[++$$i]; if ( not @$e ) { return $e; } elsif ( !$e->[0] ) { $self->remove($e->[1]); # also clears @$e return $e; } else { $e->[0]--; redo loop; } } } # ex: set sw=4 et: __PACKAGE__; __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::CLOCK::Base =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut LRU000755001750001750 011446730246 16757 5ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/UtilAPI.pm000644001750001750 124311446730246 20065 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/Util/LRUpackage Cache::Ref::Util::LRU::API; BEGIN { $Cache::Ref::Util::LRU::API::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Util::LRU::API::VERSION = '0.04'; } use Moose::Role; use namespace::autoclean; requires qw( insert hit remove clear mru lru remove_mru remove_lru ); __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::Util::LRU::API =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut List.pm000644001750001750 306211446730246 20370 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/Util/LRUpackage Cache::Ref::Util::LRU::List; BEGIN { $Cache::Ref::Util::LRU::List::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Util::LRU::List::VERSION = '0.04'; } use Moose; use namespace::autoclean; with ( 'Cache::Ref::Util::LRU::API', 'Cache::Ref::Role::WithDoublyLinkedList' => { name => "", value_offset => 0, next_offset => 1, prev_offset => 2, head_method => "mru", tail_method => "lru", shift_method => "remove_mru", pop_method => "remove_lru", unshift_method => "insert", }, ); sub hit { my ( $self, @l ) = @_; return unless @l; $self->_splice(@l); $self->_link_sequence(@l); my ( $head, $tail ) = @l[0, -1]; if ( my $old_head = $self->_head ) { # prepend @l to the head of the list $self->_set_prev($old_head, $tail); $self->_set_next($tail, $old_head); } else { # just set @l as the new list $self->_tail($tail); } $self->_head($head); return; } sub remove { my ( $self, @l ) = @_; $self->_splice(@l); @$_ = () for @l; } sub clear { shift->_clear } sub DEMOLISH { shift->clear } __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::Util::LRU::List =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut Array.pm000644001750001750 342411446730246 20535 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/Util/LRUpackage Cache::Ref::Util::LRU::Array; BEGIN { $Cache::Ref::Util::LRU::Array::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Util::LRU::Array::VERSION = '0.04'; } use Moose; use Scalar::Util qw(refaddr); use Hash::Util::FieldHash::Compat qw(id); use namespace::autoclean; has _list => ( traits => [qw(Array)], isa => "ArrayRef", default => sub { [] }, is => "ro", handles => { #size => "length", mru => [ get => 0 ], lru => [ get => -1 ], remove_mru => "shift", remove_lru => "pop", clear => "clear", }, ); with qw(Cache::Ref::Util::LRU::API); # since there's no need for metadata, insert is just like hit sub insert { my ( $self, @elements ) = @_; $self->hit(@elements); return ( @elements == 1 ? $elements[0] : @elements ); } sub _filter { my ( $self, $l, $elements ) = @_; return () unless @$l; confess if grep { not defined } @$elements; my %hash; @hash{map {id($_)} @$elements} = (); grep { not exists $hash{id($_)} } @$l; } sub hit { my ( $self, @elements ) = @_; return unless @elements; my $l = $self->_list; @$l = ( @elements, $self->_filter($l, \@elements) ); return; } sub remove { my ( $self, @elements ) = @_; return unless @elements; my $l = $self->_list; @$l = $self->_filter($l, \@elements); return; } __PACKAGE__->meta->make_immutable; __PACKAGE__; # ex: set sw=4 et: __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::Util::LRU::Array =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut WithDoublyLinkedList.pm000644001750001750 1250611446730246 23077 0ustar00brunovbrunov000000000000Cache-Ref-0.04/lib/Cache/Ref/Rolepackage Cache::Ref::Role::WithDoublyLinkedList; BEGIN { $Cache::Ref::Role::WithDoublyLinkedList::AUTHORITY = 'cpan:NUFFIN'; } BEGIN { $Cache::Ref::Role::WithDoublyLinkedList::VERSION = '0.04'; } use MooseX::Role::Parameterized; parameter name => ( isa => "Str", required => 1, ); parameter [qw(value_offset next_offset prev_offset)] => ( isa => "Int", required => 1, ); foreach my $method (qw(head tail shift pop unshift push)) { parameter "${method}_method" => ( isa => "Str", is => "ro", lazy => 1, default => sub { my $p = shift; $p->name . "_" . $method; } ); } role { my $p = shift; my $name = $p->name; my ( $head_attr, $tail_attr ) = ("${name}_head", "${name}_tail"); # technically a doubly linked list is just the inverse of itself # so really this is like including two inverted parameterized roles (the # notion of next/prev is reversed) for the endian methods for ( { attr => $head_attr, rev_attr => $tail_attr, vo => $p->value_offset, no => $p->next_offset, po => $p->prev_offset, head_method => $p->head_method, shift_method => $p->shift_method, unshift_method => $p->unshift_method }, { attr => $tail_attr, rev_attr => $head_attr, vo => $p->value_offset, no => $p->prev_offset, po => $p->next_offset, head_method => $p->tail_method, shift_method => $p->pop_method, unshift_method => $p->push_method, }, ) { my ( $attr, $rev_attr, $value, $next, $prev ) = @{$_}{qw(attr rev_attr vo no po)}; has $attr => ( is => "rw" ); method $_->{head_method} => sub { (shift->$attr || return)->[$value] }; method $_->{shift_method} => sub { my $self = shift; my $node = $self->$attr or return; if ( my $neighbor = $node->[$next] ) { $self->$attr($neighbor); $neighbor->[$prev] = undef; } else { # list is empty, clear both attrs $self->$attr(undef); $self->$rev_attr(undef); } return $node->[$value]; }; method $_->{unshift_method} => sub { my ( $self, @values ) = @_; my $head = $self->$attr; my @ret; foreach my $v ( reverse @values ) { # cons up a new list my $new = []; $new->[$value] = $v; $new->[$next] = $head; $head->[$prev] = $new; push @ret, $new; $head = $new; } $self->$attr($head); $self->$rev_attr($ret[0]) unless $self->$rev_attr; return ( @ret == 1 ? $ret[0] : reverse @ret ); } } # these methods are per linked list my ( $next_offset, $prev_offset ) = ( $p->next_offset, $p->prev_offset ); method "${name}_clear" => sub { my $self = shift; my $cur = $self->$head_attr; while ( $cur ) { my $next = $cur->[$next_offset]; $cur = (); # FIXME not so general purpose #@{$cur}[$next_offset, $prev_offset] = (); # more general purpose? don't care... $cur = $next; } $self->$head_attr(undef); $self->$tail_attr(undef); }; method "${name}_set_next" => sub { my ( $self, $node, $next ) = @_; $node->[$next_offset] = $next; }; method "${name}_set_prev" => sub { my ( $self, $node, $prev ) = @_; $node->[$prev_offset] = $prev; }; method "${name}_link_sequence" => sub { my ( $self, @nodes ) = @_; return unless @nodes; my $prev = shift @nodes; delete $prev->[$prev_offset]; foreach my $node ( @nodes ) { $prev->[$next_offset] = $node; # $prev->next($l) $node->[$prev_offset] = $prev; $prev = $node; } delete $prev->[$next_offset]; return; }; method "${name}_splice" => sub { my ( $self, @nodes ) = @_; return unless @nodes; foreach my $node ( @nodes ) { # detach node from its current place in the list if ( $node->[$prev_offset] ) { $node->[$prev_offset][$next_offset] = $node->[$next_offset]; } else { # $node is currently head, so unmark it as such $self->$head_attr($node->[$next_offset]); } if ( $node->[$next_offset] ) { $node->[$next_offset][$prev_offset] = $node->[$prev_offset]; } else { # $node is currently tail, so unmark it as such $self->$tail_attr($node->[$prev_offset]); } delete @{ $node }[$prev_offset, $next_offset]; } return }; }; # ex: set sw=4 et: __PACKAGE__; __END__ =pod =encoding utf-8 =head1 NAME Cache::Ref::Role::WithDoublyLinkedList =head1 AUTHOR Yuval Kogman =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2010 by Yuval Kogman. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut