rss_cache.inc 6.3 KB

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