Source for file rss_cache.inc

Documentation is available at rss_cache.inc

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

Documentation generated on Mon, 21 Nov 2005 18:21:33 +0100 by phpDocumentor 1.3.0RC3