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
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
5
|
use Cache::Cache; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
17
|
1
|
|
|
1
|
|
653
|
use Cache::CacheMetaData; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
18
|
1
|
|
|
1
|
|
842
|
use Cache::CacheUtils qw ( Assert_Defined ); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
75
|
|
19
|
1
|
|
|
1
|
|
767
|
use Cache::SizeAwareCache qw ( $NO_MAX_SIZE ); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
950
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new |
23
|
|
|
|
|
|
|
{ |
24
|
3
|
|
|
3
|
1
|
9
|
my ( $proto, $p_cache, $p_max_size ) = @_; |
25
|
3
|
|
33
|
|
|
19
|
my $class = ref( $proto ) || $proto; |
26
|
3
|
|
|
|
|
14
|
my $self = {}; |
27
|
3
|
|
|
|
|
9
|
bless( $self, $class ); |
28
|
3
|
|
|
|
|
14
|
Assert_Defined( $p_cache ); |
29
|
3
|
|
|
|
|
9
|
Assert_Defined( $p_max_size ); |
30
|
3
|
|
|
|
|
12
|
$self->_set_cache( $p_cache ); |
31
|
3
|
|
|
|
|
13
|
$self->set_max_size( $p_max_size ); |
32
|
3
|
|
|
|
|
34
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub update_access_time |
37
|
|
|
|
|
|
|
{ |
38
|
31
|
|
|
31
|
1
|
53
|
my ( $self, $p_key ) = @_; |
39
|
|
|
|
|
|
|
|
40
|
31
|
|
|
|
|
102
|
Assert_Defined( $p_key ); |
41
|
|
|
|
|
|
|
|
42
|
31
|
|
|
|
|
104
|
my $object = $self->_get_cache( )->get_object( $p_key ); |
43
|
|
|
|
|
|
|
|
44
|
31
|
100
|
|
|
|
109
|
if ( defined $object ) |
45
|
|
|
|
|
|
|
{ |
46
|
23
|
|
|
|
|
85
|
$object->set_accessed_at( time( ) ); |
47
|
23
|
|
|
|
|
57
|
$self->_get_cache( )->set_object( $p_key, $object ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub limit_size |
53
|
|
|
|
|
|
|
{ |
54
|
35
|
|
|
35
|
1
|
60
|
my ( $self, $p_new_size ) = @_; |
55
|
|
|
|
|
|
|
|
56
|
35
|
|
|
|
|
96
|
Assert_Defined( $p_new_size ); |
57
|
|
|
|
|
|
|
|
58
|
35
|
100
|
|
|
|
193
|
return if $p_new_size == $NO_MAX_SIZE; |
59
|
|
|
|
|
|
|
|
60
|
3
|
|
|
|
|
11
|
_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
|
3
|
|
|
3
|
|
7
|
my ( $p_cache, $p_cache_meta_data, $p_new_size ) = @_; |
72
|
|
|
|
|
|
|
|
73
|
3
|
|
|
|
|
8
|
Assert_Defined( $p_cache ); |
74
|
3
|
|
|
|
|
6
|
Assert_Defined( $p_cache_meta_data ); |
75
|
3
|
|
|
|
|
8
|
Assert_Defined( $p_new_size ); |
76
|
|
|
|
|
|
|
|
77
|
3
|
50
|
|
|
|
8
|
$p_new_size >= 0 or |
78
|
|
|
|
|
|
|
throw Error::Simple( "p_new_size >= 0 required" ); |
79
|
|
|
|
|
|
|
|
80
|
3
|
|
|
|
|
9
|
my $size_estimate = $p_cache_meta_data->get_cache_size( ); |
81
|
|
|
|
|
|
|
|
82
|
3
|
50
|
|
|
|
9
|
return if $size_estimate <= $p_new_size; |
83
|
|
|
|
|
|
|
|
84
|
3
|
|
|
|
|
11
|
foreach my $key ( $p_cache_meta_data->build_removal_list( ) ) |
85
|
|
|
|
|
|
|
{ |
86
|
10
|
100
|
|
|
|
29
|
return if $size_estimate <= $p_new_size; |
87
|
7
|
|
|
|
|
24
|
$size_estimate -= $p_cache_meta_data->build_object_size( $key ); |
88
|
7
|
|
|
|
|
22
|
$p_cache->remove( $key ); |
89
|
7
|
|
|
|
|
24
|
$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
|
3
|
|
|
3
|
|
6
|
my ( $self ) = @_; |
99
|
|
|
|
|
|
|
|
100
|
3
|
|
|
|
|
21
|
my $cache_meta_data = new Cache::CacheMetaData( ); |
101
|
|
|
|
|
|
|
|
102
|
3
|
|
|
|
|
10
|
foreach my $key ( $self->_get_cache( )->get_keys( ) ) |
103
|
|
|
|
|
|
|
{ |
104
|
10
|
50
|
|
|
|
22
|
my $object = $self->_get_cache( )->get_object( $key ) or |
105
|
|
|
|
|
|
|
next; |
106
|
|
|
|
|
|
|
|
107
|
10
|
|
|
|
|
30
|
$cache_meta_data->insert( $object ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
3
|
|
|
|
|
13
|
return $cache_meta_data; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _get_cache |
116
|
|
|
|
|
|
|
{ |
117
|
70
|
|
|
70
|
|
110
|
my ( $self ) = @_; |
118
|
|
|
|
|
|
|
|
119
|
70
|
|
|
|
|
284
|
return $self->{_Cache}; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub _set_cache |
124
|
|
|
|
|
|
|
{ |
125
|
3
|
|
|
3
|
|
6
|
my ( $self, $p_cache ) = @_; |
126
|
|
|
|
|
|
|
|
127
|
3
|
|
|
|
|
16
|
$self->{_Cache} = $p_cache; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub get_max_size |
132
|
|
|
|
|
|
|
{ |
133
|
33
|
|
|
33
|
1
|
52
|
my ( $self ) = @_; |
134
|
|
|
|
|
|
|
|
135
|
33
|
|
|
|
|
166
|
return $self->{_Max_Size}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub set_max_size |
140
|
|
|
|
|
|
|
{ |
141
|
4
|
|
|
4
|
0
|
9
|
my ( $self, $p_max_size ) = @_; |
142
|
|
|
|
|
|
|
|
143
|
4
|
|
|
|
|
12
|
$self->{_Max_Size} = $p_max_size; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
__END__ |