lib/AI/MicroStructure/Cache.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 103 CC 6 SCAR 17.9
CC
6
Cov
0%
insertCache
20.0
new
6.0
check_cache
6.0
CRAP 6.0
1 #!/usr/bin/perl -W
2 package AI::MicroStructure::Cache;
3 use strict;
4 use warnings;
5 use Getopt::Long;
6 use Digest::MD5 qw(md5_hex);
7 use Storable qw(lock_retrieve lock_store);
8 our $VERSION = '0.000';
9 sub new {
10 my $pkg = shift;
11 my $self = bless {}, $pkg;
12  
13 our %opts = (
14 timeout => -1,
15 cache => -1,
16 flush=>0,
17 max_cache_age => -1,
18 cache_file => "~/zcall_cache",
19  
20 );
21  
22  
23  
24 GetOptions (\%opts,
25 "timeout=i",
26 "cache!",
27 "flush=i",
28 "max_cache_age=i",
29 "cache_file=s");
30  
31  
32 $self->{cache_file} = $opts{cache_file};
33 $self->{cache_file} = $pkg->tildeexp($self->{cache_file});
34 $self->{opts} = %opts;
35 eval {
36  
37 $self->{cache} = lock_retrieve($self->{cache_file});
38 if($opts{flush}==1){
39 $self->{cache} = {};
40 lock_store($self->{cache},$self->{cache_file});
41 }
42 };
43
44  
45  
46 return $self;
47 }
48  
49  
50 sub insertCache {
51  
52 my $self = shift;
53 my $key = shift;
54
55 $self->{"make-time"} = time;
56 $self->{"expire"} = $self->{"make-time"}+$self->{opts}{max_cache_age};
57 $self->{data} = {} unless(defined($self->{data}));
58 $self->{data}->{$key}=$self->{data}->{$key}?++$self->{data}->{$key}:1;
59 lock_store($self->{cache},$self->{cache_file});
60  
61  
62  
63 $self->insert(@_) if @_;
64  
65 }
66  
67 sub tildeexp {
68 my $self= shift;
69 my $path = shift;
70 $path =~ s{^~([^/]*)} {
71 $1
72 ? (getpwnam($1))[7]
73 : ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7])
74 }ex;
75 return $path;
76 }
77  
78 sub store{
79  
80 my $self= shift;
81 my $cache = shift;
82  
83 lock_store($cache,$self->{cache_file});
84  
85 }
86  
87 sub members {
88 return values %{$_[0]};
89 }
90  
91 sub size {
92 return scalar keys %{$_[0]};
93 }
94  
95 sub insert {
96 my $self = shift;
97 my $data = shift;
98  
99 $self->{cache} = lock_retrieve($self->{cache_file});
100 $self->{cache} = $data;
101  
102 lock_store($data, $self->{cache_file});
103  
104 }
105  
106 sub refetch {
107 my ($self, $call) = @_;
108  
109 $self->{cache}->{$call}->{data}=time;
110  
111 lock_store($self->{cache}, $self->{cache_file});
112  
113 }
114  
115 sub check_cache {
116 my ($self, $call) = @_;
117  
118 $self->{cache} = lock_retrieve($self->{cache_file});
119  
120 if(!$self->{cache}->{$call}->{data}){
121 return 0;
122 }else{
123 return 1;
124 }
125 }
126 sub retrieve { return @_; }
127 sub includes { exists $_[0]->{ $_[1]->name } }
128 sub includes_name { exists $_[0]->{ $_[1] } }
129  
130  
131  
132 1;
133 # ABSTRACT: this is part of AI-MicroStructure the cache will be containing micro structure parts in http user session context
134