| 1 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | Quiq::Storable - Perl-Datenstrukturen persistent speichern | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | L<Quiq::Object> | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | Die Klasse ist ein objektorientierter Wrapper für das Core-Modul | ||||||||||||||||||||||||||||||||||||||||||
| 16 | Storable, speziell für die Funktionen freeze(), thaw(), clone(). | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | package Quiq::Storable; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | use base qw/Quiq::Object/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | use v5.10; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | our $VERSION = '1.240'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 6 | ▶ | use Storable (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | ▶ | use Quiq::Path; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | =head2 Klassenmethoden | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head3 clone() - Deep Copy einer Datenstruktur | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head4 Synopsis | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | $cloneRef = Quiq::Storable->clone($ref); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | sub clone { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 52 | # @_: $ref | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | return Storable::dclone($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =head3 freeze() - Serialisiere Datenstruktur zu Zeichenkette | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =head4 Synopsis | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | $str = Quiq::Storable->freeze($ref); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | sub freeze { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
freeze: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 70 | # @_: $ref | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | return Storable::freeze($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =head3 thaw() - Deserialisiere Zeichenkette zu Datenstruktur | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =head4 Synopsis | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | $ref = Quiq::Storable->thaw($str); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub thaw { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
thaw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 88 | # @_: $str | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | return Storable::thaw($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head3 memoize() - Cache Datenstruktur in Datei | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head4 Synopsis | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | $ref = Quiq::Storable->memoize($file,$sub); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | $ref = Quiq::Storable->memoize($file,$timeout,$sub); | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =head4 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =item $file | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | Pfad der Cachedatei. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =item $timeout | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | Dauer in Sekunden, die die Cachdatei gültig ist. Falls nicht angegeben | ||||||||||||||||||||||||||||||||||||||||||
| 112 | oder C<undef>, ist die Cachdatei unbegrenzt lange gültig. Ist $timeout | ||||||||||||||||||||||||||||||||||||||||||
| 113 | negativ, verfällt die Cachdatei, wenn sie abs($timeout) Sekunden | ||||||||||||||||||||||||||||||||||||||||||
| 114 | nicht zugegriffen wurde (mit jedem Aufruf wird die Datei in diesem | ||||||||||||||||||||||||||||||||||||||||||
| 115 | Fall getouched). | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =item $sub | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | Subroutine, die die Datenstruktur aufbaut und eine Referenz auf | ||||||||||||||||||||||||||||||||||||||||||
| 120 | diese zurückliefert. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =head4 Description | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | Existiert Datei $file, deserialisiere die enthaltene Datenstruktur. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | Andernfalls erzeuge die Datenstruktur durch Aufruf der Subroutine $sub | ||||||||||||||||||||||||||||||||||||||||||
| 128 | und speichere das Resultat in Datei $file. In beiden Fällen liefere eine | ||||||||||||||||||||||||||||||||||||||||||
| 129 | Referenz auf die Datenstuktur zurück. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Soll die Datenstuktur erneut generiert werden, genügt es, die Datei | ||||||||||||||||||||||||||||||||||||||||||
| 132 | zuvor zu löschen. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =head4 Example | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | Cache Hash (hier mit zyklischer Struktur): | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | my $cacheFile = '~/tmp/test5674'; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | my $objectH = Quiq::Storable->memoize($cacheFile,sub { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | my $h; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | $h->{'A'} = [1,undef]; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | $h->{'B'} = [2,undef]; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | $h->{'A'}[1] = \$h->{'B'}; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | $h->{'B'}[1] = \$h->{'A'}; | ||||||||||||||||||||||||||||||||||||||||||
| 145 | return $h; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | sub memoize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
memoize: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 2 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 2 | ▶ | my $timeout = ref $_[0]? undef: shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | my $sub = shift; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | my $p = Quiq::Path->new; | |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | my $ref; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | ▶ | if ($p->exists($file) && (!defined($timeout) || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$p->exists($file) and !defined($timeout) || "Quiq::Path"->age($file) <= abs $timeout
not defined $timeout or "Quiq::Path"->age($file) <= abs $timeout
MC/DC33%1/3
$p->exists($file) and !defined($timeout) || "Quiq::Path"->age($file) <= abs $timeout
$p->exists($file)
not defined $timeout
"Quiq::Path"->age($file) <= abs $timeout
Truth table50%2/4
A and not(B) or C
A= $p->exists($file)B= not defined $timeoutC= "Quiq::Path"->age($file) <= abs $timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | Quiq::Path->age($file) <= abs $timeout)) { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | $ref = $class->thaw($p->read($file)); | |||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | if (defined($timeout) && $timeout < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $timeout and $timeout < 0
MC/DC0%0/2
defined $timeout and $timeout < 0
defined $timeout
$timeout < 0
Truth table33%1/3
A and B
A= defined $timeoutB= $timeout < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $p->touch($file); | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | $ref = $sub->(); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | $p->write($file,$class->freeze($ref)); | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | return $ref; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1.240 | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | Copyright (C) 2026 Frank Seitz | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | This code is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 193 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | # eof | ||||||||||||||||||||||||||||||||||||||||||