SimplePie_Cache_Memcache

Caches data to memcache.

Defined (1)

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

/wp-includes/SimplePie/Cache/Memcache.php  
  1. class SimplePie_Cache_Memcache implements SimplePie_Cache_Base 
  2. /** 
  3. * Memcache instance 
  4. * @var Memcache 
  5. */ 
  6. protected $cache; 
  7.  
  8. /** 
  9. * Options 
  10. * @var array 
  11. */ 
  12. protected $options; 
  13.  
  14. /** 
  15. * Cache name 
  16. * @var string 
  17. */ 
  18. protected $name; 
  19.  
  20. /** 
  21. * Create a new cache object 
  22. * @param string $location Location string (from SimplePie::$cache_location) 
  23. * @param string $name Unique ID for the cache 
  24. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data 
  25. */ 
  26. public function __construct($location, $name, $type) 
  27. $this->options = array( 
  28. 'host' => '127.0.0.1',  
  29. 'port' => 11211,  
  30. 'extras' => array( 
  31. 'timeout' => 3600, // one hour 
  32. 'prefix' => 'simplepie_',  
  33. ),  
  34. ); 
  35. $parsed = SimplePie_Cache::parse_URL($location); 
  36. $this->options['host'] = empty($parsed['host']) ? $this->options['host'] : $parsed['host']; 
  37. $this->options['port'] = empty($parsed['port']) ? $this->options['port'] : $parsed['port']; 
  38. $this->options['extras'] = array_merge($this->options['extras'], $parsed['extras']); 
  39. $this->name = $this->options['extras']['prefix'] . md5("$name:$type"); 
  40.  
  41. $this->cache = new Memcache(); 
  42. $this->cache->addServer($this->options['host'], (int) $this->options['port']); 
  43.  
  44. /** 
  45. * Save data to the cache 
  46. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property 
  47. * @return bool Successfulness 
  48. */ 
  49. public function save($data) 
  50. if ($data instanceof SimplePie) 
  51. $data = $data->data; 
  52. return $this->cache->set($this->name, serialize($data), MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']); 
  53.  
  54. /** 
  55. * Retrieve the data saved to the cache 
  56. * @return array Data for SimplePie::$data 
  57. */ 
  58. public function load() 
  59. $data = $this->cache->get($this->name); 
  60.  
  61. if ($data !== false) 
  62. return unserialize($data); 
  63. return false; 
  64.  
  65. /** 
  66. * Retrieve the last modified time for the cache 
  67. * @return int Timestamp 
  68. */ 
  69. public function mtime() 
  70. $data = $this->cache->get($this->name); 
  71.  
  72. if ($data !== false) 
  73. // essentially ignore the mtime because Memcache expires on it's own 
  74. return time(); 
  75.  
  76. return false; 
  77.  
  78. /** 
  79. * Set the last modified time to the current time 
  80. * @return bool Success status 
  81. */ 
  82. public function touch() 
  83. $data = $this->cache->get($this->name); 
  84.  
  85. if ($data !== false) 
  86. return $this->cache->set($this->name, $data, MEMCACHE_COMPRESSED, (int) $this->duration); 
  87.  
  88. return false; 
  89.  
  90. /** 
  91. * Remove the cache 
  92. * @return bool Success status 
  93. */ 
  94. public function unlink() 
  95. return $this->cache->delete($this->name, 0);