Redis.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * SimplePie Redis Cache Extension
  4. *
  5. * @package SimplePie
  6. * @author Jan Kozak <galvani78@gmail.com>
  7. * @link http://galvani.cz/
  8. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  9. * @version 0.2.9
  10. */
  11. /**
  12. * Caches data to redis
  13. *
  14. * Registered for URLs with the "redis" protocol
  15. *
  16. * For example, `redis://localhost:6379/?timeout=3600&prefix=sp_&dbIndex=0` will
  17. * connect to redis on `localhost` on port 6379. All tables will be
  18. * prefixed with `simple_primary-` and data will expire after 3600 seconds
  19. *
  20. * @package SimplePie
  21. * @subpackage Caching
  22. * @uses Redis
  23. */
  24. class SimplePie_Cache_Redis implements SimplePie_Cache_Base {
  25. /**
  26. * Redis instance
  27. *
  28. * @var \Redis
  29. */
  30. protected $cache;
  31. /**
  32. * Options
  33. *
  34. * @var array
  35. */
  36. protected $options;
  37. /**
  38. * Cache name
  39. *
  40. * @var string
  41. */
  42. protected $name;
  43. /**
  44. * Cache Data
  45. *
  46. * @var type
  47. */
  48. protected $data;
  49. /**
  50. * Create a new cache object
  51. *
  52. * @param string $location Location string (from SimplePie::$cache_location)
  53. * @param string $name Unique ID for the cache
  54. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
  55. */
  56. public function __construct($location, $name, $options = null) {
  57. //$this->cache = \flow\simple\cache\Redis::getRedisClientInstance();
  58. $parsed = SimplePie_Cache::parse_URL($location);
  59. $redis = new Redis();
  60. $redis->connect($parsed['host'], $parsed['port']);
  61. if (isset($parsed['pass'])) {
  62. $redis->auth($parsed['pass']);
  63. }
  64. if (isset($parsed['path'])) {
  65. $redis->select((int)substr($parsed['path'], 1));
  66. }
  67. $this->cache = $redis;
  68. if (!is_null($options) && is_array($options)) {
  69. $this->options = $options;
  70. } else {
  71. $this->options = array (
  72. 'prefix' => 'rss:simple_primary:',
  73. 'expire' => 0,
  74. );
  75. }
  76. $this->name = $this->options['prefix'] . $name;
  77. }
  78. /**
  79. * @param \Redis $cache
  80. */
  81. public function setRedisClient(\Redis $cache) {
  82. $this->cache = $cache;
  83. }
  84. /**
  85. * Save data to the cache
  86. *
  87. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  88. * @return bool Successfulness
  89. */
  90. public function save($data) {
  91. if ($data instanceof SimplePie) {
  92. $data = $data->data;
  93. }
  94. $response = $this->cache->set($this->name, serialize($data));
  95. if ($this->options['expire']) {
  96. $this->cache->expire($this->name, $this->options['expire']);
  97. }
  98. return $response;
  99. }
  100. /**
  101. * Retrieve the data saved to the cache
  102. *
  103. * @return array Data for SimplePie::$data
  104. */
  105. public function load() {
  106. $data = $this->cache->get($this->name);
  107. if ($data !== false) {
  108. return unserialize($data);
  109. }
  110. return false;
  111. }
  112. /**
  113. * Retrieve the last modified time for the cache
  114. *
  115. * @return int Timestamp
  116. */
  117. public function mtime() {
  118. $data = $this->cache->get($this->name);
  119. if ($data !== false) {
  120. return time();
  121. }
  122. return false;
  123. }
  124. /**
  125. * Set the last modified time to the current time
  126. *
  127. * @return bool Success status
  128. */
  129. public function touch() {
  130. $data = $this->cache->get($this->name);
  131. if ($data !== false) {
  132. $return = $this->cache->set($this->name, $data);
  133. if ($this->options['expire']) {
  134. return $this->cache->expire($this->name, $this->options['expire']);
  135. }
  136. return $return;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Remove the cache
  142. *
  143. * @return bool Success status
  144. */
  145. public function unlink() {
  146. return $this->cache->set($this->name, null);
  147. }
  148. }