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 * Mapgie 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 $data = fread( $fp, filesize($cache_file) );
97 $rss = $this->unserialize( $data );
102 /*=======================================================================*\
103 Function: check_cache
104 Purpose: check a url for membership in the cache
105 and whether the object is older then MAX_AGE (ie. STALE)
106 Input: url from wich the rss file was fetched
107 Output: cached object on HIT, false on MISS
108 \*=======================================================================*/
109 function check_cache ( $url ) {
111 $filename = $this->file_name( $url );
113 if ( file_exists( $filename ) ) {
114 // find how long ago the file was added to the cache
115 // and whether that is longer then MAX_AGE
116 $mtime = filemtime( $filename );
117 $age = time() - $mtime;
118 if ( $this->MAX_AGE > $age ) {
119 // object exists and is current
123 // object exists but is old
128 // object does not exist
133 /*=======================================================================*\
135 \*=======================================================================*/
136 function serialize ( $rss ) {
137 return serialize( $rss );
140 /*=======================================================================*\
141 Function: unserialize
142 \*=======================================================================*/
143 function unserialize ( $data ) {
144 return unserialize( $data );
147 /*=======================================================================*\
149 Purpose: map url to location in cache
150 Input: url from wich the rss file was fetched
152 \*=======================================================================*/
153 function file_name ($url) {
154 $filename = md5( $url );
155 return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
158 /*=======================================================================*\
160 Purpose: register error
161 \*=======================================================================*/
162 function error ($errormsg, $lvl=E_USER_WARNING) {
163 // append PHP's error message if track_errors enabled
164 if ( $php_errormsg ) {
165 $errormsg .= " ($php_errormsg)";
167 $this->ERROR = $errormsg;
168 if ( MAGPIE_DEBUG ) {
169 trigger_error( $errormsg, $lvl);
172 error_log( $errormsg, 0);