| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Class::Simple::Cached; |
|
2
|
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
453218
|
use strict; |
|
|
4
|
|
|
|
|
26
|
|
|
|
4
|
|
|
|
|
97
|
|
|
4
|
4
|
|
|
4
|
|
18
|
use warnings; |
|
|
4
|
|
|
|
|
6
|
|
|
|
4
|
|
|
|
|
87
|
|
|
5
|
4
|
|
|
4
|
|
18
|
use Carp; |
|
|
4
|
|
|
|
|
5
|
|
|
|
4
|
|
|
|
|
175
|
|
|
6
|
4
|
|
|
4
|
|
1667
|
use Class::Simple; |
|
|
4
|
|
|
|
|
23562
|
|
|
|
4
|
|
|
|
|
1887
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my @ISA = ('Class::Simple'); |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Class::Simple::Cached - cache messages to an object |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Version 0.03 |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
A sub-class of L which caches calls to read |
|
25
|
|
|
|
|
|
|
the status of an object that are otherwise expensive. |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
It is up to the caller to maintain the cache if the object comes out of sync with the cache, |
|
28
|
|
|
|
|
|
|
for example by changing its state. |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 new |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Creates a Class::Simple::Cached object. |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
It takes one mandatory parameter: cache, |
|
37
|
|
|
|
|
|
|
which is an object which understands get() and set() calls, |
|
38
|
|
|
|
|
|
|
such as an L object. |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
It takes one optional argument: object, |
|
41
|
|
|
|
|
|
|
which is an object which is taken to be the object to be cached. |
|
42
|
|
|
|
|
|
|
If not given, an object of the class L is instantiated |
|
43
|
|
|
|
|
|
|
and that is used. |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub new { |
|
48
|
4
|
|
|
4
|
1
|
203040
|
my $proto = shift; |
|
49
|
4
|
|
33
|
|
|
23
|
my $class = ref($proto) || $proto; |
|
50
|
|
|
|
|
|
|
|
|
51
|
4
|
50
|
|
|
|
13
|
return unless(defined($class)); |
|
52
|
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
8
|
my %args; |
|
54
|
4
|
100
|
|
|
|
23
|
if(ref($_[0]) eq 'HASH') { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
2
|
%args = %{$_[0]}; |
|
|
1
|
|
|
|
|
4
|
|
|
56
|
|
|
|
|
|
|
} elsif(ref($_[0])) { |
|
57
|
0
|
|
|
|
|
0
|
Carp::carp('Usage: ', __PACKAGE__, '->new(cache => $cache [, object => $object ], %args)'); |
|
58
|
0
|
|
|
|
|
0
|
return; |
|
59
|
|
|
|
|
|
|
} elsif(@_ % 2 == 0) { |
|
60
|
3
|
|
|
|
|
10
|
%args = @_; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
4
|
100
|
|
|
|
14
|
if(!defined($args{'object'})) { |
|
64
|
3
|
|
|
|
|
18
|
$args{'object'} = Class::Simple->new(%args); |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
4
|
100
|
|
|
|
861
|
if($args{'cache'}) { |
|
68
|
2
|
|
|
|
|
8
|
return bless \%args, $class; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
2
|
|
|
|
|
5
|
Carp::carp('Usage: ', __PACKAGE__, '->new(cache => $cache [, object => $object ], %args)'); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _caller_class |
|
74
|
|
|
|
|
|
|
{ |
|
75
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
76
|
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
0
|
if(ref($self->{'object'}) eq 'Class::Simple') { |
|
78
|
|
|
|
|
|
|
# return $self->SUPER::_caller_class(@_); |
|
79
|
0
|
|
|
|
|
0
|
return $self->Class::Simple::_caller_class(@_); |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub AUTOLOAD { |
|
84
|
17
|
|
|
17
|
|
6259
|
our $AUTOLOAD; |
|
85
|
17
|
|
|
|
|
28
|
my $param = $AUTOLOAD; |
|
86
|
17
|
|
|
|
|
83
|
$param =~ s/.*:://; |
|
87
|
|
|
|
|
|
|
|
|
88
|
17
|
|
|
|
|
31
|
my $self = shift; |
|
89
|
17
|
|
|
|
|
34
|
my $cache = $self->{'cache'}; |
|
90
|
|
|
|
|
|
|
|
|
91
|
17
|
100
|
|
|
|
41
|
if($param eq 'DESTROY') { |
|
92
|
2
|
50
|
33
|
|
|
87
|
if(defined($^V) && ($^V ge 'v5.14.0')) { |
|
93
|
2
|
50
|
|
|
|
8
|
return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; # >= 5.14.0 only |
|
94
|
|
|
|
|
|
|
} |
|
95
|
2
|
|
|
|
|
30
|
$cache->clear(); |
|
96
|
2
|
|
|
|
|
35
|
return; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# my $func = $self->{'object'} . "::$param"; |
|
100
|
15
|
|
|
|
|
21
|
my $func = $param; |
|
101
|
15
|
|
|
|
|
22
|
my $object = $self->{'object'}; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# if($param =~ /^[gs]et_/) { |
|
104
|
|
|
|
|
|
|
# # $param = "SUPER::$param"; |
|
105
|
|
|
|
|
|
|
# return $object->$func(\@_); |
|
106
|
|
|
|
|
|
|
# } |
|
107
|
|
|
|
|
|
|
|
|
108
|
15
|
100
|
|
|
|
89
|
if(scalar(@_) == 0) { |
|
109
|
|
|
|
|
|
|
# Retrieving a value |
|
110
|
12
|
100
|
|
|
|
61
|
if(my $rc = $cache->get($param)) { |
|
111
|
7
|
50
|
|
|
|
509
|
if(ref($rc) eq 'ARRAY') { |
|
112
|
0
|
|
|
|
|
0
|
return @{$rc}; |
|
|
0
|
|
|
|
|
0
|
|
|
113
|
|
|
|
|
|
|
} |
|
114
|
7
|
100
|
|
|
|
15
|
if($rc eq __PACKAGE__ . ">UNDEF<") { |
|
115
|
1
|
|
|
|
|
5
|
return; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
6
|
|
|
|
|
37
|
return $rc; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
5
|
100
|
|
|
|
338
|
if(wantarray) { |
|
120
|
2
|
|
|
|
|
7
|
my @rc = $object->$func(); |
|
121
|
2
|
50
|
|
|
|
11
|
if(scalar(@rc) == 0) { |
|
122
|
0
|
|
|
|
|
0
|
return; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
2
|
|
|
|
|
6
|
$cache->set($param, \@rc, 'never'); |
|
125
|
2
|
|
|
|
|
209
|
return @rc; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
3
|
|
|
|
|
10
|
my $rc = $object->$func(); |
|
128
|
3
|
100
|
|
|
|
17
|
if(!defined($rc)) { |
|
129
|
1
|
|
|
|
|
5
|
$cache->set($param, __PACKAGE__ . ">UNDEF<", 'never'); |
|
130
|
1
|
|
|
|
|
104
|
return; |
|
131
|
|
|
|
|
|
|
} |
|
132
|
2
|
|
|
|
|
14
|
return $cache->set($param, $rc, 'never'); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# $param = "SUPER::$param"; |
|
136
|
|
|
|
|
|
|
# return $cache->set($param, $self->$param(@_), 'never'); |
|
137
|
3
|
100
|
|
|
|
10
|
if($_[1]) { |
|
138
|
|
|
|
|
|
|
# Storing an array |
|
139
|
|
|
|
|
|
|
# We store a ref to the array, and dereference on retrieval |
|
140
|
1
|
|
|
|
|
17
|
my $val = $object->$func(\@_); |
|
141
|
1
|
|
|
|
|
80
|
$cache->set($param, $val, 'never'); |
|
142
|
1
|
|
|
|
|
218
|
return @{$val}; |
|
|
1
|
|
|
|
|
4
|
|
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
# Storing a scalar |
|
145
|
2
|
|
|
|
|
15
|
return $cache->set($param, $object->$func($_[0]), 'never'); |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 AUTHOR |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Nigel Horne, C<< >> |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 BUGS |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Doesn't work with L. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, |
|
157
|
|
|
|
|
|
|
or through the web interface at |
|
158
|
|
|
|
|
|
|
L. |
|
159
|
|
|
|
|
|
|
I will be notified, and then you'll |
|
160
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
params() returns a ref which means that calling routines can change the hash |
|
163
|
|
|
|
|
|
|
for other routines. |
|
164
|
|
|
|
|
|
|
Take a local copy before making amendments to the table if you don't want unexpected |
|
165
|
|
|
|
|
|
|
things to happen. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
L, L |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 SUPPORT |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
perldoc Class::Simple::Cached |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
You can also look for information at: |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=over 4 |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
L |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
L |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item * Search CPAN |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
L |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=back |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Author Nigel Horne: C |
|
198
|
|
|
|
|
|
|
Copyright (C) 2019, Nigel Horne |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Usage is subject to licence terms. |
|
201
|
|
|
|
|
|
|
The licence terms of this software are as follows: |
|
202
|
|
|
|
|
|
|
Personal single user, single computer use: GPL2 |
|
203
|
|
|
|
|
|
|
All other users (including Commercial, Charity, Educational, Government) |
|
204
|
|
|
|
|
|
|
must apply in writing for a licence for use from Nigel Horne at the |
|
205
|
|
|
|
|
|
|
above e-mail. |
|
206
|
|
|
|
|
|
|
=cut |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
1; |