Quiq/Cache.pm

filter: Statementstmt 53.6% 22 / 41 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 70.0% 7 / 10 Podpod 100% 3 / 3 totaltotal 46.3% 32 / 69 CC 6 SCAR 24.3
CC
6
Cov
42%
read
30.0
write
6.0
new
2.0
CRAP 11.4
« Quiq/File/Video.pm ↑ Summary Quiq/TimeLapse/Filename.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Cache - Cache Datenstruktur in Datei
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::Cache;
16
17 sub ... {
18 ...
19
20 # Instantiiere Cache
21
22 my $c = Quiq::Cache->new($cacheDir,$duration,\@key)
23 -inactive => $condition,
24 );
25 if (my $ref = $c->read) {
26 return $ref; # liefere Datenstruktur aus Cache
27 }
28
29 # Baue Datenstruktur auf
30
31 ...
32
33 $c->write($ref); # schreibe Datenstuktur auf Cache
34
35 return $ref; # liefere Datenstruktur
36 }
37  
38 =head1 DESCRIPTION
39  
40 Ein Objekt der Klasse verwaltet einen Cache. Der Cache ist ein Verzeichnis
41 (C<$cacheDir>) im Dateisystem. Eine Cachedatei speichert eine beliebige
42 Datenstruktur. Diese wird von C<Storable> geschrieben und gelesen.
43 Die Datenstruktur darf ein Objekt (also geblesst) oder zirkulär sein.
44 Die Cachdatei ist $duration Sekunden gültig. Danach ist sie
45 ungültig und wird neu berechnet.
46  
47 Die Aufrufe L<read|"read() - Lies Daten aus Cachedatei">() und L<write|"write() - Schreibe Daten auf Cachdatei">() haben keine Wirkung, wenn
48 C<$condition> wahr ist. Damit kann das Caching für bestimmte
49 Fälle abgschaltet werden.
50  
51 Das Array @key besteht aus ein oder mehr einzelnen Werten, die zusammen
52 den Schlüssel für die Cachdatei bilden. Der interne Schüssel ist der
53 MD5-Hash über diesen Werten. Dieser bildet den Namen der Cachedatei.
54  
55 =head1 EXAMPLE
56  
57 Cachen einer HTML-Seite, die von einem einzigen Parameter $day abhängt:
58  
59 my $day = $self->param('day') // $today;
60
61 my $c = Quiq::Cache->new('~/var/html-cache',43_200,[$day],
62 -inactive => $day eq $today,
63 );
64 if (my $ref = $c->read) {
65 $self->render(text=>$$ref);
66 return;
67 }
68
69 my $html = ...HTML erzeugen...
70
71 $c->write(\$html);
72  
73 =cut
74  
75 # -----------------------------------------------------------------------------
76  
77 package Quiq::Cache;
78 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 1 our $VERSION = '1.240';
85  
86 1 ▶use Quiq::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use Quiq::Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 # -----------------------------------------------------------------------------
91  
92 =head1 METHODS
93  
94 =head2 Klassenmethoden
95  
96 =head3 new() - Konstruktor
97  
98 =head4 Synopsis
99  
100 $c = $class->new($dir,$duration,\@key,@opt);
101  
102 =head4 Arguments
103  
104 =over 4
105  
106 =item $dir
107  
108 Verzeichnis, in dem die Cachdatei gespeichert wird.
109  
110 =item $duration
111  
112 Zeitdauer in Sekunden, die die Cachdatei ab ihrer Erzeugung
113 gültig ist.
114  
115 =item @key
116  
117 Die zur Bildung des Hash herangezogenen Werte.
118  
119 =back
120  
121 =head4 Options
122  
123 =over 4
124  
125 =item -inactive => $bool (default: 0)
126  
127 Wenn wahr, ist der Cache inaktiv, d.h. $c->L<read|"read() - Lies Daten aus Cachedatei">() liefert immer
128 C<undef> und $c->L<write|"write() - Schreibe Daten auf Cachdatei">() hat keine Wirkung.
129  
130 =item -prefix => $str (Default: '')
131  
132 Prefix, der der Cachedatei vorangestellt wird.
133  
134 =back
135  
136 =head4 Returns
137  
138 Cache-Objekt
139  
140 =head4 Description
141  
142 Instantiiere ein Cache-Objekt und liefere eine Referenz auf
143 dieses Objekt zurück.
144  
145 =cut
146  
147 # -----------------------------------------------------------------------------
148  
149 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
150 0 my ($class,$dir,$duration,$keyA) = splice @_,0,4;
151  
152 # Optionen und Argumente
153  
154 0 my $inactive = 0;
155 0 my $prefix = '';
156  
157 0 $class->parameters(\@_,
158 -inactive => \$inactive,
159 -prefix => \$prefix,
160 );
161  
162 0 return $class->SUPER::new(
163 duration => $duration,
164 file => sprintf('%s/%s%s',$dir,$prefix,Quiq::Digest->md5(@$keyA)),
165 inactive => $inactive,
166 );
167 }
168  
169 # -----------------------------------------------------------------------------
170  
171 =head2 Objektmethoden
172  
173 =head3 read() - Lies Daten aus Cachedatei
174  
175 =head4 Synopsis
176  
177 $ref = $c->read;
178  
179 =head4 Returns
180  
181 Referenz auf Datenstruktur oder C<undef>
182  
183 =head4 Description
184  
185 Liefere eine Referenz auf die Datenstruktur in der Cachdatei oder
186 C<undef>. Wir liefern C<undef>, wenn
187  
188 =over 2
189  
190 =item *
191  
192 der Cache inaktiv ist
193  
194 =item *
195  
196 die Cachdatei nicht existiert
197  
198 =item *
199  
200 die Cachdatei exisiert, aber älter ist als die Gültigkeitsdauer
201  
202 =back
203  
204 =cut
205  
206 # -----------------------------------------------------------------------------
207  
208 0 ▶sub read {
Subroutine read: not called CC=5 SCAR=34.0
Pod documented
209 0 my $self = shift;
210  
211 0 ▶ if ($self->{'inactive'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inactive"}) 0 0 0
212 0 return undef;
213 }
214  
215 0 my $p = Quiq::Path->new;
216 0 my ($file,$duration) = @{$self}{qw/file duration/};
217 0 ▶ if (!$p->exists($file) || $duration && $p->age($file) > $duration) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p->exists($file) or $duration and $p->age($file) > $duration) 0 0 0
Condition0%0/6
not $p->exists($file) or $duration and $p->age($file) > $duration
l!l&&r!l&&!r
000
$duration and $p->age($file) > $duration
!ll&&!rl&&r
000
MC/DC0%0/3
not $p->exists($file) or $duration and $p->age($file) > $duration
not $p->exists($file) $duration $p->age($file) > $duration
Truth table0%0/4
not(A) or B and C
A= not $p->exists($file)B= $durationC= $p->age($file) > $duration
ABCresult
1XX1
0111
00X0
0100
218 0 return undef;
219 }
220  
221 0 return Quiq::Storable->thaw(Quiq::Path->read($file));
222 }
223  
224 # -----------------------------------------------------------------------------
225  
226 =head3 write() - Schreibe Daten auf Cachdatei
227  
228 =head4 Synopsis
229  
230 $c->write($ref);
231  
232 =head4 Arguments
233  
234 =over 4
235  
236 =item $ref
237  
238 Referenz auf Datenstruktur.
239  
240 =back
241  
242 =head4 Description
243  
244 Schreibe Datenstruktur $ref auf die Cachedatei.
245  
246 =cut
247  
248 # -----------------------------------------------------------------------------
249  
250 0 ▶sub write {
Subroutine write: not called CC=2 SCAR=17.9
Pod documented
251 0 my ($self,$ref) = @_;
252  
253 0 ▶ if (!$self->{'inactive'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inactive"}) 0 0 0
254 0 my $file = $self->{'file'};
255 0 Quiq::Path->write($file,Quiq::Storable->freeze($ref));
256 }
257  
258 0 return;
259 }
260  
261 # -----------------------------------------------------------------------------
262  
263 =head1 VERSION
264  
265 1.240
266  
267 =head1 AUTHOR
268  
269 Frank Seitz, L<http://fseitz.de/>
270  
271 =head1 COPYRIGHT
272  
273 Copyright (C) 2026 Frank Seitz
274  
275 =head1 LICENSE
276  
277 This code is free software; you can redistribute it and/or modify
278 it under the same terms as Perl itself.
279  
280 =cut
281  
282 # -----------------------------------------------------------------------------
283  
284 1 1;
285  
286 # eof
« Quiq/File/Video.pm ↑ Summary Quiq/TimeLapse/Filename.pm »