lib/NewsClipper/Cache.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 132 CC 12 SCAR 38.8
CC
12
Cov
0%
_Outdated
110.0
_IsStillValid
12.0
new
2.0
CRAP 48.2
1 # -*- mode: Perl; -*-
2 package NewsClipper::Cache;
3  
4 # This package implements a cache for HTML files.
5  
6 use strict;
7 # To parse dates
8 use Time::CTime;
9 use Time::ParseDate;
10 # To get the local time zone
11 use POSIX ();
12 use File::Cache;
13  
14 use vars qw( $VERSION @ISA );
15  
16 @ISA = qw(File::Cache);
17 $VERSION = 0.35;
18  
19 use NewsClipper::Globals;
20  
21 # ------------------------------------------------------------------------------
22  
23 sub new
24 {
25 my $proto = shift;
26  
27 my $self = $proto->SUPER::new( { cache_key => $config{cache_location},
28 namespace => 'html',
29 username => '',
30 filemode => 0666,
31 auto_remove_stale => 0,
32 max_size => $config{max_cache_size},
33 persistence_mechanism => 'Data::Dumper',
34 });
35  
36 return $self;
37 }
38  
39 # ------------------------------------------------------------------------------
40  
41 # Gets the data from the cache, if it is available, as well as the status. The
42 # possible return combinations are:
43 # DATA, valid: Data is in cache and not stale
44 # DATA, stale: Data is in cache but stale
45 # undef, not found: Data is not in cache
46  
47 sub get($$@)
48 {
49 my $self = shift;
50 my $url = shift;
51 my @updateTimes = shift;
52  
53 # Since the expiration time is infinite, any data in the cache will always
54 # be fresh (i.e. we don't have to check get_stale)
55 return ($self->SUPER::get($url),$self->_IsStillValid($url,@updateTimes));
56 }
57  
58 # ------------------------------------------------------------------------------
59  
60 # Determine when the content was cached. Return the time in seconds since the
61 # epoch, or undef if the cached item can't be found.
62  
63 sub GetTimeCached
64 {
65 my $self = shift;
66 my $url = shift;
67  
68 return $self->SUPER::get_creation_time($url);
69 }
70  
71 # ------------------------------------------------------------------------------
72  
73 # Updates the last modified time of the information associated with the key.
74  
75 sub UpdateTime($$)
76 {
77 my $self = shift;
78 my $url = shift;
79  
80 # This is a slow way to do it, but let's do it until the File::Cache module
81 # provides support.
82 my $data = $self->SUPER::get($url);
83 $self->SUPER::set($url,$data);
84 }
85 # ------------------------------------------------------------------------------
86  
87 # Checks the cache for the url's data. One of three return values are
88 # possible:
89 # valid: The data exists and isn't old
90 # stale: The data is exists but is old
91 # not found: The data is not in the cache
92  
93 sub _IsStillValid($@)
94 {
95 my $self = shift;
96 my $url = shift;
97 my @updateTimes = shift;
98  
99 dprint "Checking cache for data for URL:\n $url";
100  
101 my $lastUpdated = $self->get_creation_time($url);
102  
103 # Return 'not found' if we couldn't find it in the cache.
104 unless (defined $lastUpdated)
105 {
106 dprint "Couldn't find cached data ";
107 return 'not found';
108 }
109  
110 if (_Outdated(\@updateTimes,$lastUpdated))
111 {
112 dprint "Data is stale";
113 return 'stale';
114 }
115 else
116 {
117 dprint "Reusing cached data";
118 return 'valid';
119 }
120 }
121  
122 # ------------------------------------------------------------------------------
123  
124 # Figures out whether the cached data is out of date
125  
126 sub _Outdated($$)
127 {
128 # Get the times that the user has specified, or the default times.
129 my @relativeUpdateTimes = @{shift @_};
130 my $lastUpdated = shift;
131  
132 # Iterate over the times to find out which is the most recent time we should
133 # have updated.
134 my $mostRecentUpdateTime = 0;
135 foreach my $timeSpec (@relativeUpdateTimes)
136 {
137 # Extract the day and timezone from the time specification from the
138 # handler. Replace $timeSpec with the middle, which should be the hours.
139 my ($day,$timezone);
140 ($day,$timeSpec,$timezone) =
141 $timeSpec =~ /^([a-z]*)\D*([\d ,]*)\s*([a-z]*)/i;
142  
143 # Move all the hours from 2,3,4,5 form into an array.
144 my @hours = split /\D+/,$timeSpec;
145  
146 # chop off extra characters on the day, just in case they did thurs
147 # instead of thu.
148 $day =~ s/^(...).*/$1/;
149  
150 # If they didn't specify a day, it's today
151 $day = 'today'
152 if $day eq '' || lc(strftime("%a",localtime(time))) eq lc($day);
153  
154 # If they didn't specify a timezone, it's pacific (in recognition of the
155 # multitude of internet companies in California)
156 $timezone = 'PST' if $timezone eq '';
157  
158 # Set the timezone for the current timezone if LOCAL_TIME_ZONE is specified
159 $timezone = POSIX::strftime("%Z",localtime)
160 if $timezone eq 'LOCAL_TIME_ZONE';
161  
162 # Now iterate through each hour in the list, looking for the most recent
163 # update hour.
164 foreach my $hour (@hours)
165 {
166 $day = "last $day"
167 unless $day =~ /(today|yesterday)/i || $day =~ /^last /i;
168 my $tempDate = parsedate("$day $hour:00", ZONE => $timezone);
169  
170 # Correct apparently future times. Basically, they meant "yesterday at
171 # 2pm", not "today at 2pm" (if it's earlier than 2pm)
172 if ($tempDate > parsedate('now'))
173 {
174 if ($day eq 'today')
175 {
176 $day = 'yesterday';
177 }
178 elsif ($day eq 'yesterday')
179 {
180 $day = "-2 days";
181 }
182 }
183  
184 # Parse the date again, in case we corrected the day.
185 my $parsedDate = parsedate("$day $hour:00", ZONE => $timezone);
186  
187 $mostRecentUpdateTime = $parsedDate
188 if $parsedDate > $mostRecentUpdateTime;
189 }
190 }
191  
192 dprint ("Comparing dates:");
193 dprint (" Last Updated: $lastUpdated");
194 dprint (" Most Recent Update Time: $mostRecentUpdateTime");
195 dprint (" Now: ",parsedate('now'));
196  
197 if ($lastUpdated < $mostRecentUpdateTime)
198 {
199 dprint "Update is needed";
200 return 1;
201 }
202 else
203 {
204 dprint "Update is not needed";
205 return 0;
206 }
207 }
208  
209 1;