File: LRU.pm

package info (click to toggle)
libtie-cache-lru-perl 20150301-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 528 kB
  • sloc: perl: 563; makefile: 2
file content (166 lines) | stat: -rw-r--r-- 3,848 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package Tie::Cache::LRU;

use strict;

use base qw(Tie::Cache::LRU::Array);

our $VERSION = 20150301;


=head1 NAME

Tie::Cache::LRU - A Least-Recently Used cache


=head1 SYNOPSIS

    use Tie::Cache::LRU;

    tie %cache, 'Tie::Cache::LRU', 500;
    tie %cache, 'Tie::Cache::LRU', '400k'; #UNIMPLEMENTED

    # Use like a normal hash.

    $cache_obj = tied %cache;
    $current_size = $cache_obj->curr_size;

    $max_size = $cache_obj->max_size;
    $cache_obj->max_size($new_size);


=head1 DESCRIPTION

B<NOTE> There are no plans to update this module.  Please consider
using L<CHI> or other caching framework.

This is an implementation of a least-recently used (LRU) cache keeping
the cache in RAM.

A LRU cache is similar to the kind of cache used by a web browser.
New items are placed into the top of the cache.  When the cache grows
past its size limit, it throws away items off the bottom.  The trick
is that whenever an item is -accessed-, it is pulled back to the top.
The end result of all this is that items which are frequently accessed
tend to stay in the cache.



=head1 USAGE

The cache is extremely simple, is just holds a simple scalar.  If you
want to cache an object, just place it into the cache:

    $cache{$obj->id} = $obj;

This doesn't make a copy of the object, it just holds a reference to
it.  (Note: This means that your object's destructor will not be
called until it has fallen out of the cache (and all other references
to it have disappeared, of course)!)

If you want to cache an array, place a reference to it in the cache:

    $cache{$some_id} = \@array;

Or, if you're worried about the consequences of tossing around
references and want to cache a copy instead, you can do something like
this:

    $cache{$some_id} = [@array];


=head2 Tied Interface

=over 4

=item B<tie>

    tie %cache, 'Tie::Cache::LRU';
    tie %cache, 'Tie::Cache::LRU', $cache_size;

This ties a cache to %cache which will hold a maximum of $cache_size
keys.  If $cache_size is not given it uses a default value,
Tie::Cache::LRU->DEFAULT_MAX_SIZE.

If the size is set to 0, the cache is effectively turned off.  This is
useful for "removing" the cache from a program without having to make
deep alterations to the program itself, or for checking performance
differences with and without a cache.

All of the expected hash operations (exists, delete, slices, etc...) 
work on the %cache.


=back

=head2 Object Interface

There's a few things you just can't do through the tied interface.  To
do them, you need to get at the underlying object, which you do with
tied().

    $cache_obj = tied %cache;

And then you can call a few methods on that object:

=over 4

=item B<max_size>

  $cache_obj->max_size($size);
  $size = $cache_obj->max_size;

An accessor to alter the maximum size of the cache on the fly.

If max_size() is reset, and it is lower than the current size, the cache
is immediately truncated.

The size must be an integer greater than or equal to 0.


=item B<curr_size>

  $size = $cache_obj->curr_size;

Returns the current number of items in the cache.


=back


=head1 NOTES

This is just a thin subclass of Tie::Cache::LRU::Array.


=head1 TODO

Should eventually allow the cache to be in shared memory.

Max size by memory use unimplemented.


=head1 COPYRIGHT AND LICENSE

Copyright 1999-2015 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl 5 itself.

See L<http://dev.perl.org/licenses/>


=head1 AUTHOR

Michael G Schwern <schwern@pobox.com>.


=head1 SEE ALSO

L<CHI> for a more modern cache implementation.

L<Tie::Cache::LRU::Array>, L<Tie::Cache::LRU::LinkedList>,
L<Tie::Cache::LRU::Virtual>, L<Tie::Cache>

=cut

return q|Look at me, look at me!  I'm super fast!  I'm bionic!  I'm bionic!|;