LRU.pm

filter: Statementstmt 100% 8 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 10 / 10 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« LRU/Array.pm ↑ Summary LRU/Virtual.pm »
1 package Tie::Cache::LRU;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use base qw(Tie::Cache::LRU::Array);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = 20150301;
8  
9  
10 =head1 NAME
11  
12 Tie::Cache::LRU - A Least-Recently Used cache
13  
14  
15 =head1 SYNOPSIS
16  
17 use Tie::Cache::LRU;
18  
19 tie %cache, 'Tie::Cache::LRU', 500;
20 tie %cache, 'Tie::Cache::LRU', '400k'; #UNIMPLEMENTED
21  
22 # Use like a normal hash.
23  
24 $cache_obj = tied %cache;
25 $current_size = $cache_obj->curr_size;
26  
27 $max_size = $cache_obj->max_size;
28 $cache_obj->max_size($new_size);
29  
30  
31 =head1 DESCRIPTION
32  
33 B<NOTE> There are no plans to update this module. Please consider
34 using L<CHI> or other caching framework.
35  
36 This is an implementation of a least-recently used (LRU) cache keeping
37 the cache in RAM.
38  
39 A LRU cache is similar to the kind of cache used by a web browser.
40 New items are placed into the top of the cache. When the cache grows
41 past its size limit, it throws away items off the bottom. The trick
42 is that whenever an item is -accessed-, it is pulled back to the top.
43 The end result of all this is that items which are frequently accessed
44 tend to stay in the cache.
45  
46  
47  
48 =head1 USAGE
49  
50 The cache is extremely simple, is just holds a simple scalar. If you
51 want to cache an object, just place it into the cache:
52  
53 $cache{$obj->id} = $obj;
54  
55 This doesn't make a copy of the object, it just holds a reference to
56 it. (Note: This means that your object's destructor will not be
57 called until it has fallen out of the cache (and all other references
58 to it have disappeared, of course)!)
59  
60 If you want to cache an array, place a reference to it in the cache:
61  
62 $cache{$some_id} = \@array;
63  
64 Or, if you're worried about the consequences of tossing around
65 references and want to cache a copy instead, you can do something like
66 this:
67  
68 $cache{$some_id} = [@array];
69  
70  
71 =head2 Tied Interface
72  
73 =over 4
74  
75 =item B<tie>
76  
77 tie %cache, 'Tie::Cache::LRU';
78 tie %cache, 'Tie::Cache::LRU', $cache_size;
79  
80 This ties a cache to %cache which will hold a maximum of $cache_size
81 keys. If $cache_size is not given it uses a default value,
82 Tie::Cache::LRU->DEFAULT_MAX_SIZE.
83  
84 If the size is set to 0, the cache is effectively turned off. This is
85 useful for "removing" the cache from a program without having to make
86 deep alterations to the program itself, or for checking performance
87 differences with and without a cache.
88  
89 All of the expected hash operations (exists, delete, slices, etc...)
90 work on the %cache.
91  
92  
93 =back
94  
95 =head2 Object Interface
96  
97 There's a few things you just can't do through the tied interface. To
98 do them, you need to get at the underlying object, which you do with
99 tied().
100  
101 $cache_obj = tied %cache;
102  
103 And then you can call a few methods on that object:
104  
105 =over 4
106  
107 =item B<max_size>
108  
109 $cache_obj->max_size($size);
110 $size = $cache_obj->max_size;
111  
112 An accessor to alter the maximum size of the cache on the fly.
113  
114 If max_size() is reset, and it is lower than the current size, the cache
115 is immediately truncated.
116  
117 The size must be an integer greater than or equal to 0.
118  
119  
120 =item B<curr_size>
121  
122 $size = $cache_obj->curr_size;
123  
124 Returns the current number of items in the cache.
125  
126  
127 =back
128  
129  
130 =head1 NOTES
131  
132 This is just a thin subclass of Tie::Cache::LRU::Array.
133  
134  
135 =head1 TODO
136  
137 Should eventually allow the cache to be in shared memory.
138  
139 Max size by memory use unimplemented.
140  
141  
142 =head1 COPYRIGHT AND LICENSE
143  
144 Copyright 1999-2015 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
145  
146 This program is free software; you can redistribute it and/or
147 modify it under the same terms as Perl 5 itself.
148  
149 See L<http://dev.perl.org/licenses/>
150  
151  
152 =head1 AUTHOR
153  
154 Michael G Schwern <schwern@pobox.com>.
155  
156  
157 =head1 SEE ALSO
158  
159 L<CHI> for a more modern cache implementation.
160  
161 L<Tie::Cache::LRU::Array>, L<Tie::Cache::LRU::LinkedList>,
162 L<Tie::Cache::LRU::Virtual>, L<Tie::Cache>
163  
164 =cut
165  
166 1 return q|Look at me, look at me! I'm super fast! I'm bionic! I'm bionic!|;
« LRU/Array.pm ↑ Summary LRU/Virtual.pm »