RSSCache

The WordPress Core RSSCache class.

Defined (1)

The class is defined in the following location(s).

/wp-includes/rss.php  
  1. class RSSCache { 
  2. var $BASE_CACHE; // where the cache files are stored 
  3. var $MAX_AGE = 43200; // when are files stale, default twelve hours 
  4. var $ERROR = ''; // accumulate error messages 
  5.  
  6. /** 
  7. * PHP5 constructor. 
  8. */ 
  9. function __construct( $base = '', $age = '' ) { 
  10. $this->BASE_CACHE = WP_CONTENT_DIR . '/cache'; 
  11. if ( $base ) { 
  12. $this->BASE_CACHE = $base; 
  13. if ( $age ) { 
  14. $this->MAX_AGE = $age; 
  15.  
  16.  
  17. /** 
  18. * PHP4 constructor. 
  19. */ 
  20. public function RSSCache( $base = '', $age = '' ) { 
  21. self::__construct( $base, $age ); 
  22.  
  23. /**=======================================================================*\ 
  24. Function: set 
  25. Purpose: add an item to the cache, keyed on url 
  26. Input: url from which the rss file was fetched 
  27. Output: true on success 
  28. \*=======================================================================*/ 
  29. function set ($url, $rss) { 
  30. $cache_option = 'rss_' . $this->file_name( $url ); 
  31.  
  32. set_transient($cache_option, $rss, $this->MAX_AGE); 
  33.  
  34. return $cache_option; 
  35.  
  36. /**=======================================================================*\ 
  37. Function: get 
  38. Purpose: fetch an item from the cache 
  39. Input: url from which the rss file was fetched 
  40. Output: cached object on HIT, false on MISS 
  41. \*=======================================================================*/ 
  42. function get ($url) { 
  43. $this->ERROR = ""; 
  44. $cache_option = 'rss_' . $this->file_name( $url ); 
  45.  
  46. if ( ! $rss = get_transient( $cache_option ) ) { 
  47. $this->debug( 
  48. "Cache doesn't contain: $url (cache option: $cache_option)" 
  49. ); 
  50. return 0; 
  51.  
  52. return $rss; 
  53.  
  54. /**=======================================================================*\ 
  55. Function: check_cache 
  56. Purpose: check a url for membership in the cache 
  57. and whether the object is older then MAX_AGE (ie. STALE) 
  58. Input: url from which the rss file was fetched 
  59. Output: cached object on HIT, false on MISS 
  60. \*=======================================================================*/ 
  61. function check_cache ( $url ) { 
  62. $this->ERROR = ""; 
  63. $cache_option = 'rss_' . $this->file_name( $url ); 
  64.  
  65. if ( get_transient($cache_option) ) { 
  66. // object exists and is current 
  67. return 'HIT'; 
  68. } else { 
  69. // object does not exist 
  70. return 'MISS'; 
  71.  
  72. /**=======================================================================*\ 
  73. Function: serialize 
  74. \*=======================================================================*/ 
  75. function serialize ( $rss ) { 
  76. return serialize( $rss ); 
  77.  
  78. /**=======================================================================*\ 
  79. Function: unserialize 
  80. \*=======================================================================*/ 
  81. function unserialize ( $data ) { 
  82. return unserialize( $data ); 
  83.  
  84. /**=======================================================================*\ 
  85. Function: file_name 
  86. Purpose: map url to location in cache 
  87. Input: url from which the rss file was fetched 
  88. Output: a file name 
  89. \*=======================================================================*/ 
  90. function file_name ($url) { 
  91. return md5( $url ); 
  92.  
  93. /**=======================================================================*\ 
  94. Function: error 
  95. Purpose: register error 
  96. \*=======================================================================*/ 
  97. function error ($errormsg, $lvl=E_USER_WARNING) { 
  98. // append PHP's error message if track_errors enabled 
  99. if ( isset($php_errormsg) ) { 
  100. $errormsg .= " ($php_errormsg)"; 
  101. $this->ERROR = $errormsg; 
  102. if ( MAGPIE_DEBUG ) { 
  103. trigger_error( $errormsg, $lvl); 
  104. else { 
  105. error_log( $errormsg, 0); 
  106. function debug ($debugmsg, $lvl=E_USER_NOTICE) { 
  107. if ( MAGPIE_DEBUG ) { 
  108. $this->error("MagpieRSS [debug] $debugmsg", $lvl);