NumSeq/Base/Cache.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 58 CC 3 SCAR 12.4
CC
3
Cov
0%
END
6.0
make_key
6.0
BEGIN
2.0
CRAP 3.5
1 # Copyright 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-NumSeq.
4 #
5 # Math-NumSeq is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-NumSeq is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # Experimental ...
20  
21  
22 package Math::NumSeq::Base::Cache;
23 use 5.004;
24 use strict;
25  
26 use vars '$VERSION', '@ISA', '@EXPORT_OK';
27 $VERSION = 75;
28 @ISA = ('Exporter');
29 @EXPORT_OK = ('cache_hash', 'make_key');
30  
31 use vars '%cache';
32 my $tempdir;
33 use constant::defer cache_hash => sub {
34 require SDBM_File;
35 require File::Temp;
36 $tempdir = File::Temp->newdir;
37 ### $tempdir
38 ### tempdir: $tempdir->dirname
39 tie (%cache, 'SDBM_File',
40 File::Spec->catfile ($tempdir->dirname, "cache"),
41 Fcntl::O_RDWR()|Fcntl::O_CREAT(),
42 0666)
43 or die "Couldn't tie SDBM file 'filename': $!; aborting";
44  
45 END {
46 if ($tempdir) {
47 ### unlink cache ...
48 untie %cache;
49 my $dirname = $tempdir->dirname;
50 unlink File::Spec->catfile ($dirname, "cache.pag");
51 unlink File::Spec->catfile ($dirname, "cache.dir");
52 }
53 }
54 # END {
55 # if ($tempdir) {
56 # ### cache diagnostics ...
57 # my $count = 0;
58 # while (each %cache) {
59 # $count++;
60 # }
61 # untie %cache;
62 # my $dirname = $tempdir->dirname;
63 # print "cache final $count file sizes cache.pag ",
64 # (-s File::Spec->catfile($dirname,"cache.pag")),
65 # " cache.dir ",
66 # (-s File::Spec->catfile($dirname,"cache.dir")),
67 # "\n";
68 # }
69 # }
70 return \%cache;
71 };
72  
73 my $cache_key = 0;
74 sub make_key {
75 my $params = "CacheKey:" . join (',',@_);
76 ### $params
77 if (my $c = cache_hash()->{$params}) {
78 return $c;
79 }
80 return sprintf 'k%X:', (cache_hash()->{$params} = $cache_key++);
81 }
82  
83 1;
84 __END__