Memcached.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Sam Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * Caches data to memcached
  45. *
  46. * Registered for URLs with the "memcached" protocol
  47. *
  48. * For example, `memcached://localhost:11211/?timeout=3600&prefix=sp_` will
  49. * connect to memcached on `localhost` on port 11211. All tables will be
  50. * prefixed with `sp_` and data will expire after 3600 seconds
  51. *
  52. * @package SimplePie
  53. * @subpackage Caching
  54. * @author Paul L. McNeely
  55. * @uses Memcached
  56. */
  57. class SimplePie_Cache_Memcached implements SimplePie_Cache_Base
  58. {
  59. /**
  60. * Memcached instance
  61. * @var Memcached
  62. */
  63. protected $cache;
  64. /**
  65. * Options
  66. * @var array
  67. */
  68. protected $options;
  69. /**
  70. * Cache name
  71. * @var string
  72. */
  73. protected $name;
  74. /**
  75. * Create a new cache object
  76. * @param string $location Location string (from SimplePie::$cache_location)
  77. * @param string $name Unique ID for the cache
  78. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  79. */
  80. public function __construct($location, $name, $type) {
  81. $this->options = array(
  82. 'host' => '127.0.0.1',
  83. 'port' => 11211,
  84. 'extras' => array(
  85. 'timeout' => 3600, // one hour
  86. 'prefix' => 'simplepie_',
  87. ),
  88. );
  89. $this->options = SimplePie_Misc::array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));
  90. $this->name = $this->options['extras']['prefix'] . md5("$name:$type");
  91. $this->cache = new Memcached();
  92. $this->cache->addServer($this->options['host'], (int)$this->options['port']);
  93. }
  94. /**
  95. * Save data to the cache
  96. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  97. * @return bool Successfulness
  98. */
  99. public function save($data) {
  100. if ($data instanceof SimplePie) {
  101. $data = $data->data;
  102. }
  103. return $this->setData(serialize($data));
  104. }
  105. /**
  106. * Retrieve the data saved to the cache
  107. * @return array Data for SimplePie::$data
  108. */
  109. public function load() {
  110. $data = $this->cache->get($this->name);
  111. if ($data !== false) {
  112. return unserialize($data);
  113. }
  114. return false;
  115. }
  116. /**
  117. * Retrieve the last modified time for the cache
  118. * @return int Timestamp
  119. */
  120. public function mtime() {
  121. $data = $this->cache->get($this->name . '_mtime');
  122. return (int) $data;
  123. }
  124. /**
  125. * Set the last modified time to the current time
  126. * @return bool Success status
  127. */
  128. public function touch() {
  129. $data = $this->cache->get($this->name);
  130. return $this->setData($data);
  131. }
  132. /**
  133. * Remove the cache
  134. * @return bool Success status
  135. */
  136. public function unlink() {
  137. return $this->cache->delete($this->name, 0);
  138. }
  139. /**
  140. * Set the last modified time and data to Memcached
  141. * @return bool Success status
  142. */
  143. private function setData($data) {
  144. if ($data !== false) {
  145. $this->cache->set($this->name . '_mtime', time(), (int)$this->options['extras']['timeout']);
  146. return $this->cache->set($this->name, $data, (int)$this->options['extras']['timeout']);
  147. }
  148. return false;
  149. }
  150. }