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

