PhpRedis.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Predis cache backend.
  4. */
  5. class Redis_Cache_PhpRedis extends Redis_Cache_Base
  6. {
  7. public function setLastFlushTimeFor($time, $volatile = false)
  8. {
  9. $client = $this->getClient();
  10. $key = $this->getKey(self::LAST_FLUSH_KEY);
  11. if ($volatile) {
  12. $client->hset($key, 'volatile', $time);
  13. } else {
  14. $client->hmset($key, array(
  15. 'permanent' => $time,
  16. 'volatile' => $time,
  17. ));
  18. }
  19. }
  20. public function getLastFlushTime()
  21. {
  22. $client = $this->getClient();
  23. $key = $this->getKey(self::LAST_FLUSH_KEY);
  24. $values = $client->hmget($key, array("permanent", "volatile"));
  25. if (empty($values) || !is_array($values)) {
  26. $ret = array(0, 0);
  27. } else {
  28. if (empty($values['permanent'])) {
  29. $values['permanent'] = 0;
  30. }
  31. if (empty($values['volatile'])) {
  32. $values['volatile'] = 0;
  33. }
  34. $ret = array($values['permanent'], $values['volatile']);
  35. }
  36. return $ret;
  37. }
  38. public function get($id)
  39. {
  40. $client = $this->getClient();
  41. $key = $this->getKey($id);
  42. $values = $client->hgetall($key);
  43. // Recent versions of PhpRedis will return the Redis instance
  44. // instead of an empty array when the HGETALL target key does
  45. // not exists. I see what you did there.
  46. if (empty($values) || !is_array($values)) {
  47. return false;
  48. }
  49. return $values;
  50. }
  51. public function getMultiple(array $idList)
  52. {
  53. $client = $this->getClient();
  54. $ret = array();
  55. $pipe = $client->multi(Redis::PIPELINE);
  56. foreach ($idList as $id) {
  57. $pipe->hgetall($this->getKey($id));
  58. }
  59. $replies = $pipe->exec();
  60. foreach (array_values($idList) as $line => $id) {
  61. if (!empty($replies[$line]) && is_array($replies[$line])) {
  62. $ret[$id] = $replies[$line];
  63. }
  64. }
  65. return $ret;
  66. }
  67. public function set($id, $data, $ttl = null, $volatile = false)
  68. {
  69. // Ensure TTL consistency: if the caller gives us an expiry timestamp
  70. // in the past the key will expire now and will never be read.
  71. // Behavior between Predis and PhpRedis seems to change here: when
  72. // setting a negative expire time, PhpRedis seems to ignore the
  73. // command and leave the key permanent.
  74. if (null !== $ttl && $ttl <= 0) {
  75. return;
  76. }
  77. $data['volatile'] = (int)$volatile;
  78. $client = $this->getClient();
  79. $key = $this->getKey($id);
  80. $pipe = $client->multi(Redis::PIPELINE);
  81. $pipe->hmset($key, $data);
  82. if (null !== $ttl) {
  83. $pipe->expire($key, $ttl);
  84. }
  85. $pipe->exec();
  86. }
  87. public function delete($id)
  88. {
  89. $this->getClient()->del($this->getKey($id));
  90. }
  91. public function deleteMultiple(array $idList)
  92. {
  93. $client = $this->getClient();
  94. $pipe = $client->multi(Redis::PIPELINE);
  95. foreach ($idList as $id) {
  96. $pipe->del($this->getKey($id));
  97. }
  98. // Don't care if something failed.
  99. $pipe->exec();
  100. }
  101. public function deleteByPrefix($prefix)
  102. {
  103. $client = $this->getClient();
  104. $ret = $client->eval(self::EVAL_DELETE_PREFIX, array($this->getKey($prefix . '*')));
  105. if (1 != $ret) {
  106. trigger_error(sprintf("EVAL failed: %s", $client->getLastError()), E_USER_ERROR);
  107. }
  108. }
  109. public function flush()
  110. {
  111. $client = $this->getClient();
  112. $ret = $client->eval(self::EVAL_DELETE_PREFIX, array($this->getKey('*')));
  113. if (1 != $ret) {
  114. trigger_error(sprintf("EVAL failed: %s", $client->getLastError()), E_USER_ERROR);
  115. }
  116. }
  117. public function flushVolatile()
  118. {
  119. $client = $this->getClient();
  120. $ret = $client->eval(self::EVAL_DELETE_VOLATILE, array($this->getKey('*')));
  121. if (1 != $ret) {
  122. trigger_error(sprintf("EVAL failed: %s", $client->getLastError()), E_USER_ERROR);
  123. }
  124. }
  125. }