Redis.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $this->cache = $redis;
  62. if (!is_null($options) && is_array($options)) {
  63. $this->options = $options;
  64. } else {
  65. $this->options = array (
  66. 'prefix' => 'rss:simple_primary:',
  67. 'expire' => 0,
  68. );
  69. }
  70. $this->name = $this->options['prefix'] . $name;
  71. }
  72. /**
  73. * @param \Redis $cache
  74. */
  75. public function setRedisClient(\Redis $cache) {
  76. $this->cache = $cache;
  77. }
  78. /**
  79. * Save data to the cache
  80. *
  81. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
  82. * @return bool Successfulness
  83. */
  84. public function save($data) {
  85. if ($data instanceof SimplePie) {
  86. $data = $data->data;
  87. }
  88. $response = $this->cache->set($this->name, serialize($data));
  89. if ($this->options['expire']) {
  90. $this->cache->expire($this->name, $this->options['expire']);
  91. }
  92. return $response;
  93. }
  94. /**
  95. * Retrieve the data saved to the cache
  96. *
  97. * @return array Data for SimplePie::$data
  98. */
  99. public function load() {
  100. $data = $this->cache->get($this->name);
  101. if ($data !== false) {
  102. return unserialize($data);
  103. }
  104. return false;
  105. }
  106. /**
  107. * Retrieve the last modified time for the cache
  108. *
  109. * @return int Timestamp
  110. */
  111. public function mtime() {
  112. $data = $this->cache->get($this->name);
  113. if ($data !== false) {
  114. return time();
  115. }
  116. return false;
  117. }
  118. /**
  119. * Set the last modified time to the current time
  120. *
  121. * @return bool Success status
  122. */
  123. public function touch() {
  124. $data = $this->cache->get($this->name);
  125. if ($data !== false) {
  126. $return = $this->cache->set($this->name, $data);
  127. if ($this->options['expire']) {
  128. return $this->cache->expire($this->name, $this->ttl);
  129. }
  130. return $return;
  131. }
  132. return false;
  133. }
  134. /**
  135. * Remove the cache
  136. *
  137. * @return bool Success status
  138. */
  139. public function unlink() {
  140. return $this->cache->set($this->name, null);
  141. }
  142. }