3
* Project: MagpieRSS: a simple RSS integration tool
4
* File: rss_cache.inc, a simple, rolling(no GC), cache
5
* for RSS objects, keyed on URL.
6
* Author: Kellan Elliott-McCrea <kellan@protest.net>
10
* The lastest version of MagpieRSS can be obtained from:
11
* http://magpierss.sourceforge.net
13
* For questions, help, comments, discussion, etc., please join the
14
* Magpie mailing list:
15
* http://lists.sourceforge.net/lists/listinfo/magpierss-general
20
var $BASE_CACHE = './cache'; // where the cache files are stored
21
var $MAX_AGE = 3600; // when are files stale, default one hour
22
var $ERROR = ""; // accumulate error messages
24
function RSSCache ($base='', $age='') {
26
$this->BASE_CACHE = $base;
29
$this->MAX_AGE = $age;
32
// attempt to make the cache directory
33
if ( ! file_exists( $this->BASE_CACHE ) ) {
34
$status = @mkdir( $this->BASE_CACHE, 0755 );
39
"Cache couldn't make dir '" . $this->BASE_CACHE . "'."
45
/*=======================================================================*\
47
Purpose: add an item to the cache, keyed on url
48
Input: url from wich the rss file was fetched
49
Output: true on sucess
50
\*=======================================================================*/
51
function set ($url, $rss) {
53
$cache_file = $this->file_name( $url );
54
$fp = @fopen( $cache_file, 'w' );
58
"Cache unable to open file for writing: $cache_file"
64
$data = $this->serialize( $rss );
71
/*=======================================================================*\
73
Purpose: fetch an item from the cache
74
Input: url from wich the rss file was fetched
75
Output: cached object on HIT, false on MISS
76
\*=======================================================================*/
79
$cache_file = $this->file_name( $url );
81
if ( ! file_exists( $cache_file ) ) {
83
"Cache doesn't contain: $url (cache file: $cache_file)"
88
$fp = @fopen($cache_file, 'r');
91
"Failed to open cache file for reading: $cache_file"
96
if ($filesize = filesize($cache_file) ) {
97
$data = fread( $fp, filesize($cache_file) );
98
$rss = $this->unserialize( $data );
106
/*=======================================================================*\
107
Function: check_cache
108
Purpose: check a url for membership in the cache
109
and whether the object is older then MAX_AGE (ie. STALE)
110
Input: url from wich the rss file was fetched
111
Output: cached object on HIT, false on MISS
112
\*=======================================================================*/
113
function check_cache ( $url ) {
115
$filename = $this->file_name( $url );
117
if ( file_exists( $filename ) ) {
118
// find how long ago the file was added to the cache
119
// and whether that is longer then MAX_AGE
120
$mtime = filemtime( $filename );
121
$age = time() - $mtime;
122
if ( $this->MAX_AGE > $age ) {
123
// object exists and is current
127
// object exists but is old
132
// object does not exist
137
function cache_age( $cache_key ) {
138
$filename = $this->file_name( $url );
139
if ( file_exists( $filename ) ) {
140
$mtime = filemtime( $filename );
141
$age = time() - $mtime;
149
/*=======================================================================*\
151
\*=======================================================================*/
152
function serialize ( $rss ) {
153
return serialize( $rss );
156
/*=======================================================================*\
157
Function: unserialize
158
\*=======================================================================*/
159
function unserialize ( $data ) {
160
return unserialize( $data );
163
/*=======================================================================*\
165
Purpose: map url to location in cache
166
Input: url from wich the rss file was fetched
168
\*=======================================================================*/
169
function file_name ($url) {
170
$filename = md5( $url );
171
return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
174
/*=======================================================================*\
176
Purpose: register error
177
\*=======================================================================*/
178
function error ($errormsg, $lvl=E_USER_WARNING) {
179
// append PHP's error message if track_errors enabled
180
if ( isset($php_errormsg) ) {
181
$errormsg .= " ($php_errormsg)";
183
$this->ERROR = $errormsg;
184
if ( MAGPIE_DEBUG ) {
185
trigger_error( $errormsg, $lvl);
188
error_log( $errormsg, 0);
192
function debug ($debugmsg, $lvl=E_USER_NOTICE) {
193
if ( MAGPIE_DEBUG ) {
194
$this->error("MagpieRSS [debug] $debugmsg", $lvl);