2 |
/* |
/* |
3 |
* Project: MagpieRSS: a simple RSS integration tool |
* Project: MagpieRSS: a simple RSS integration tool |
4 |
* File: rss_cache.inc, a simple, rolling(no GC), cache |
* File: rss_cache.inc, a simple, rolling(no GC), cache |
5 |
* for RSS objects, keyed on URL. |
* for RSS objects, keyed on URL. |
6 |
* Author: Kellan Elliott-McCrea <kellan@protest.net> |
* Author: Kellan Elliott-McCrea <kellan@protest.net> |
7 |
* Version: 0.51 |
* Version: 0.51 |
8 |
* License: GPL |
* License: GPL |
9 |
* |
* |
10 |
* The lastest version of MagpieRSS can be obtained from: |
* The lastest version of MagpieRSS can be obtained from: |
11 |
* http://magpierss.sourceforge.net |
* http://magpierss.sourceforge.net |
17 |
*/ |
*/ |
18 |
|
|
19 |
class RSSCache { |
class RSSCache { |
20 |
var $BASE_CACHE = './cache'; // where the cache files are stored |
var $BASE_CACHE = './cache'; // where the cache files are stored |
21 |
var $MAX_AGE = 3600; // when are files stale, default one hour |
var $MAX_AGE = 3600; // when are files stale, default one hour |
22 |
var $ERROR = ""; // accumulate error messages |
var $ERROR = ""; // accumulate error messages |
23 |
|
|
24 |
function RSSCache ($base='', $age='') { |
function RSSCache ($base='', $age='') { |
25 |
if ( $base ) { |
if ( $base ) { |
26 |
$this->BASE_CACHE = $base; |
$this->BASE_CACHE = $base; |
27 |
} |
} |
28 |
if ( $age ) { |
if ( $age ) { |
29 |
$this->MAX_AGE = $age; |
$this->MAX_AGE = $age; |
30 |
} |
} |
31 |
|
|
32 |
// attempt to make the cache directory |
// attempt to make the cache directory |
33 |
if ( ! file_exists( $this->BASE_CACHE ) ) { |
if ( ! file_exists( $this->BASE_CACHE ) ) { |
34 |
$status = @mkdir( $this->BASE_CACHE, 0755 ); |
$status = @mkdir( $this->BASE_CACHE, 0755 ); |
35 |
|
|
36 |
// if make failed |
// if make failed |
37 |
if ( ! $status ) { |
if ( ! $status ) { |
38 |
$this->error( |
$this->error( |
39 |
"Cache couldn't make dir '" . $this->BASE_CACHE . "'." |
"Cache couldn't make dir '" . $this->BASE_CACHE . "'." |
40 |
); |
); |
41 |
} |
} |
42 |
} |
} |
43 |
} |
} |
44 |
|
|
45 |
/*=======================================================================*\ |
/*=======================================================================*\ |
46 |
Function: set |
Function: set |
47 |
Purpose: add an item to the cache, keyed on url |
Purpose: add an item to the cache, keyed on url |
48 |
Input: url from wich the rss file was fetched |
Input: url from wich the rss file was fetched |
49 |
Output: true on sucess |
Output: true on sucess |
50 |
\*=======================================================================*/ |
\*=======================================================================*/ |
51 |
function set ($url, $rss) { |
function set ($url, $rss) { |
52 |
$this->ERROR = ""; |
$this->ERROR = ""; |
53 |
$cache_file = $this->file_name( $url ); |
$cache_file = $this->file_name( $url ); |
54 |
$fp = @fopen( $cache_file, 'w' ); |
$fp = @fopen( $cache_file, 'w' ); |
55 |
|
|
56 |
if ( ! $fp ) { |
if ( ! $fp ) { |
57 |
$this->error( |
$this->error( |
58 |
"Cache unable to open file for writing: $cache_file" |
"Cache unable to open file for writing: $cache_file" |
59 |
); |
); |
60 |
return 0; |
return 0; |
61 |
} |
} |
62 |
|
|
63 |
|
|
64 |
$data = $this->serialize( $rss ); |
$data = $this->serialize( $rss ); |
65 |
fwrite( $fp, $data ); |
fwrite( $fp, $data ); |
66 |
fclose( $fp ); |
fclose( $fp ); |
67 |
|
|
68 |
return $cache_file; |
return $cache_file; |
69 |
} |
} |
70 |
|
|
71 |
/*=======================================================================*\ |
/*=======================================================================*\ |
72 |
Function: get |
Function: get |
73 |
Purpose: fetch an item from the cache |
Purpose: fetch an item from the cache |
74 |
Input: url from wich the rss file was fetched |
Input: url from wich the rss file was fetched |
75 |
Output: cached object on HIT, false on MISS |
Output: cached object on HIT, false on MISS |
76 |
\*=======================================================================*/ |
\*=======================================================================*/ |
77 |
function get ($url) { |
function get ($url) { |
78 |
$this->ERROR = ""; |
$this->ERROR = ""; |
79 |
$cache_file = $this->file_name( $url ); |
$cache_file = $this->file_name( $url ); |
80 |
|
|
81 |
if ( ! file_exists( $cache_file ) ) { |
if ( ! file_exists( $cache_file ) ) { |
82 |
$this->debug( |
$this->debug( |
83 |
"Cache doesn't contain: $url (cache file: $cache_file)" |
"Cache doesn't contain: $url (cache file: $cache_file)" |
84 |
); |
); |
85 |
return 0; |
return 0; |
86 |
} |
} |
87 |
|
|
88 |
$fp = @fopen($cache_file, 'r'); |
$fp = @fopen($cache_file, 'r'); |
89 |
if ( ! $fp ) { |
if ( ! $fp ) { |
90 |
$this->error( |
$this->error( |
91 |
"Failed to open cache file for reading: $cache_file" |
"Failed to open cache file for reading: $cache_file" |
92 |
); |
); |
93 |
return 0; |
return 0; |
94 |
} |
} |
95 |
|
|
96 |
$data = fread( $fp, filesize($cache_file) ); |
$data = fread( $fp, filesize($cache_file) ); |
97 |
$rss = $this->unserialize( $data ); |
$rss = $this->unserialize( $data ); |
98 |
|
|
99 |
return $rss; |
return $rss; |
100 |
} |
} |
101 |
|
|
102 |
/*=======================================================================*\ |
/*=======================================================================*\ |
103 |
Function: check_cache |
Function: check_cache |
104 |
Purpose: check a url for membership in the cache |
Purpose: check a url for membership in the cache |
105 |
and whether the object is older then MAX_AGE (ie. STALE) |
and whether the object is older then MAX_AGE (ie. STALE) |
106 |
Input: url from wich the rss file was fetched |
Input: url from wich the rss file was fetched |
107 |
Output: cached object on HIT, false on MISS |
Output: cached object on HIT, false on MISS |
108 |
\*=======================================================================*/ |
\*=======================================================================*/ |
109 |
function check_cache ( $url ) { |
function check_cache ( $url ) { |
110 |
$this->ERROR = ""; |
$this->ERROR = ""; |
111 |
$filename = $this->file_name( $url ); |
$filename = $this->file_name( $url ); |
112 |
|
|
113 |
if ( file_exists( $filename ) ) { |
if ( file_exists( $filename ) ) { |
114 |
// find how long ago the file was added to the cache |
// find how long ago the file was added to the cache |
115 |
// and whether that is longer then MAX_AGE |
// and whether that is longer then MAX_AGE |
116 |
$mtime = filemtime( $filename ); |
$mtime = filemtime( $filename ); |
117 |
$age = time() - $mtime; |
$age = time() - $mtime; |
118 |
if ( $this->MAX_AGE > $age ) { |
if ( $this->MAX_AGE > $age ) { |
119 |
// object exists and is current |
// object exists and is current |
120 |
return 'HIT'; |
return 'HIT'; |
121 |
} |
} |
122 |
else { |
else { |
123 |
// object exists but is old |
// object exists but is old |
124 |
return 'STALE'; |
return 'STALE'; |
125 |
} |
} |
126 |
} |
} |
127 |
else { |
else { |
128 |
// object does not exist |
// object does not exist |
129 |
return 'MISS'; |
return 'MISS'; |
130 |
} |
} |
131 |
} |
} |
132 |
|
|
133 |
/*=======================================================================*\ |
/*=======================================================================*\ |
134 |
Function: serialize |
Function: serialize |
135 |
\*=======================================================================*/ |
\*=======================================================================*/ |
136 |
function serialize ( $rss ) { |
function serialize ( $rss ) { |
137 |
return serialize( $rss ); |
return serialize( $rss ); |
138 |
} |
} |
139 |
|
|
140 |
/*=======================================================================*\ |
/*=======================================================================*\ |
141 |
Function: unserialize |
Function: unserialize |
142 |
\*=======================================================================*/ |
\*=======================================================================*/ |
143 |
function unserialize ( $data ) { |
function unserialize ( $data ) { |
144 |
return unserialize( $data ); |
return unserialize( $data ); |
145 |
} |
} |
146 |
|
|
147 |
/*=======================================================================*\ |
/*=======================================================================*\ |
148 |
Function: file_name |
Function: file_name |
149 |
Purpose: map url to location in cache |
Purpose: map url to location in cache |
150 |
Input: url from wich the rss file was fetched |
Input: url from wich the rss file was fetched |
151 |
Output: a file name |
Output: a file name |
152 |
\*=======================================================================*/ |
\*=======================================================================*/ |
153 |
function file_name ($url) { |
function file_name ($url) { |
154 |
$filename = md5( $url ); |
$filename = md5( $url ); |
155 |
return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) ); |
return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) ); |
156 |
} |
} |
157 |
|
|
158 |
/*=======================================================================*\ |
/*=======================================================================*\ |
159 |
Function: error |
Function: error |
160 |
Purpose: register error |
Purpose: register error |
161 |
\*=======================================================================*/ |
\*=======================================================================*/ |
162 |
function error ($errormsg, $lvl=E_USER_WARNING) { |
function error ($errormsg, $lvl=E_USER_WARNING) { |
163 |
// append PHP's error message if track_errors enabled |
// append PHP's error message if track_errors enabled |
164 |
if ( isset($php_errormsg) ) { |
if ( isset($php_errormsg) ) { |
165 |
$errormsg .= " ($php_errormsg)"; |
$errormsg .= " ($php_errormsg)"; |
166 |
} |
} |
167 |
$this->ERROR = $errormsg; |
$this->ERROR = $errormsg; |
168 |
if ( MAGPIE_DEBUG ) { |
if ( MAGPIE_DEBUG ) { |
169 |
trigger_error( $errormsg, $lvl); |
trigger_error( $errormsg, $lvl); |
170 |
} |
} |
171 |
else { |
else { |
172 |
error_log( $errormsg, 0); |
error_log( $errormsg, 0); |
173 |
} |
} |
174 |
} |
} |
175 |
|
|
176 |
function debug ($debugmsg, $lvl=E_USER_NOTICE) { |
function debug ($debugmsg, $lvl=E_USER_NOTICE) { |
177 |
if ( MAGPIE_DEBUG ) { |
if ( MAGPIE_DEBUG ) { |
178 |
$this->error("MagpieRSS [debug] $debugmsg", $lvl); |
$this->error("MagpieRSS [debug] $debugmsg", $lvl); |
179 |
} |
} |
180 |
} |
} |
181 |
|
|
182 |
} |
} |
183 |
|
|