line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
###################################################################### |
2
|
|
|
|
|
|
|
# $Id: CacheSizer.pm,v 1.4 2002/04/07 17:04:46 dclinton Exp $ |
3
|
|
|
|
|
|
|
# Copyright (C) 2001-2003 DeWitt Clinton All Rights Reserved |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Software distributed under the License is distributed on an "AS |
6
|
|
|
|
|
|
|
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or |
7
|
|
|
|
|
|
|
# implied. See the License for the specific language governing |
8
|
|
|
|
|
|
|
# rights and limitations under the License. |
9
|
|
|
|
|
|
|
###################################################################### |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
package Cache::CacheSizer; |
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
8
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
55
|
|
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
2
|
|
9
|
use Cache::Cache; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
17
|
2
|
|
|
2
|
|
933
|
use Cache::CacheMetaData; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
54
|
|
18
|
2
|
|
|
2
|
|
916
|
use Cache::CacheUtils qw ( Assert_Defined ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
151
|
|
19
|
2
|
|
|
2
|
|
1332
|
use Cache::SizeAwareCache qw ( $NO_MAX_SIZE ); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1466
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new |
23
|
|
|
|
|
|
|
{ |
24
|
6
|
|
|
6
|
1
|
14
|
my ( $proto, $p_cache, $p_max_size ) = @_; |
25
|
6
|
|
33
|
|
|
32
|
my $class = ref( $proto ) || $proto; |
26
|
6
|
|
|
|
|
20
|
my $self = {}; |
27
|
6
|
|
|
|
|
16
|
bless( $self, $class ); |
28
|
6
|
|
|
|
|
23
|
Assert_Defined( $p_cache ); |
29
|
6
|
|
|
|
|
18
|
Assert_Defined( $p_max_size ); |
30
|
6
|
|
|
|
|
26
|
$self->_set_cache( $p_cache ); |
31
|
6
|
|
|
|
|
22
|
$self->set_max_size( $p_max_size ); |
32
|
6
|
|
|
|
|
28
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub update_access_time |
37
|
|
|
|
|
|
|
{ |
38
|
62
|
|
|
62
|
1
|
111
|
my ( $self, $p_key ) = @_; |
39
|
|
|
|
|
|
|
|
40
|
62
|
|
|
|
|
199
|
Assert_Defined( $p_key ); |
41
|
|
|
|
|
|
|
|
42
|
62
|
|
|
|
|
262
|
my $object = $self->_get_cache( )->get_object( $p_key ); |
43
|
|
|
|
|
|
|
|
44
|
62
|
100
|
|
|
|
219
|
if ( defined $object ) |
45
|
|
|
|
|
|
|
{ |
46
|
46
|
|
|
|
|
151
|
$object->set_accessed_at( time( ) ); |
47
|
46
|
|
|
|
|
113
|
$self->_get_cache( )->set_object( $p_key, $object ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub limit_size |
53
|
|
|
|
|
|
|
{ |
54
|
70
|
|
|
70
|
1
|
120
|
my ( $self, $p_new_size ) = @_; |
55
|
|
|
|
|
|
|
|
56
|
70
|
|
|
|
|
203
|
Assert_Defined( $p_new_size ); |
57
|
|
|
|
|
|
|
|
58
|
70
|
100
|
|
|
|
364
|
return if $p_new_size == $NO_MAX_SIZE; |
59
|
|
|
|
|
|
|
|
60
|
6
|
|
|
|
|
25
|
_Limit_Size( $self->_get_cache( ), |
61
|
|
|
|
|
|
|
$self->_build_cache_meta_data( ), |
62
|
|
|
|
|
|
|
$p_new_size ); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# take a Cache reference and a CacheMetaData reference and |
67
|
|
|
|
|
|
|
# limit the cache's size to new_size |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _Limit_Size |
70
|
|
|
|
|
|
|
{ |
71
|
6
|
|
|
6
|
|
12
|
my ( $p_cache, $p_cache_meta_data, $p_new_size ) = @_; |
72
|
|
|
|
|
|
|
|
73
|
6
|
|
|
|
|
20
|
Assert_Defined( $p_cache ); |
74
|
6
|
|
|
|
|
25
|
Assert_Defined( $p_cache_meta_data ); |
75
|
6
|
|
|
|
|
19
|
Assert_Defined( $p_new_size ); |
76
|
|
|
|
|
|
|
|
77
|
6
|
50
|
|
|
|
17
|
$p_new_size >= 0 or |
78
|
|
|
|
|
|
|
throw Error::Simple( "p_new_size >= 0 required" ); |
79
|
|
|
|
|
|
|
|
80
|
6
|
|
|
|
|
21
|
my $size_estimate = $p_cache_meta_data->get_cache_size( ); |
81
|
|
|
|
|
|
|
|
82
|
6
|
50
|
|
|
|
24
|
return if $size_estimate <= $p_new_size; |
83
|
|
|
|
|
|
|
|
84
|
6
|
|
|
|
|
28
|
foreach my $key ( $p_cache_meta_data->build_removal_list( ) ) |
85
|
|
|
|
|
|
|
{ |
86
|
20
|
100
|
|
|
|
66
|
return if $size_estimate <= $p_new_size; |
87
|
14
|
|
|
|
|
43
|
$size_estimate -= $p_cache_meta_data->build_object_size( $key ); |
88
|
14
|
|
|
|
|
47
|
$p_cache->remove( $key ); |
89
|
14
|
|
|
|
|
54
|
$p_cache_meta_data->remove( $key ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
0
|
warn( "Couldn't limit size to $p_new_size" ); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _build_cache_meta_data |
97
|
|
|
|
|
|
|
{ |
98
|
6
|
|
|
6
|
|
14
|
my ( $self ) = @_; |
99
|
|
|
|
|
|
|
|
100
|
6
|
|
|
|
|
48
|
my $cache_meta_data = new Cache::CacheMetaData( ); |
101
|
|
|
|
|
|
|
|
102
|
6
|
|
|
|
|
23
|
foreach my $key ( $self->_get_cache( )->get_keys( ) ) |
103
|
|
|
|
|
|
|
{ |
104
|
20
|
50
|
|
|
|
49
|
my $object = $self->_get_cache( )->get_object( $key ) or |
105
|
|
|
|
|
|
|
next; |
106
|
|
|
|
|
|
|
|
107
|
20
|
|
|
|
|
65
|
$cache_meta_data->insert( $object ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
6
|
|
|
|
|
29
|
return $cache_meta_data; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _get_cache |
116
|
|
|
|
|
|
|
{ |
117
|
140
|
|
|
140
|
|
200
|
my ( $self ) = @_; |
118
|
|
|
|
|
|
|
|
119
|
140
|
|
|
|
|
623
|
return $self->{_Cache}; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub _set_cache |
124
|
|
|
|
|
|
|
{ |
125
|
6
|
|
|
6
|
|
13
|
my ( $self, $p_cache ) = @_; |
126
|
|
|
|
|
|
|
|
127
|
6
|
|
|
|
|
25
|
$self->{_Cache} = $p_cache; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub get_max_size |
132
|
|
|
|
|
|
|
{ |
133
|
66
|
|
|
66
|
1
|
98
|
my ( $self ) = @_; |
134
|
|
|
|
|
|
|
|
135
|
66
|
|
|
|
|
285
|
return $self->{_Max_Size}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub set_max_size |
140
|
|
|
|
|
|
|
{ |
141
|
8
|
|
|
8
|
0
|
16
|
my ( $self, $p_max_size ) = @_; |
142
|
|
|
|
|
|
|
|
143
|
8
|
|
|
|
|
22
|
$self->{_Max_Size} = $p_max_size; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
__END__ |