Predis.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Predis cache backend.
  4. */
  5. class Redis_Cache_Predis 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. $values = array(0, 0);
  27. } else {
  28. if (empty($values[0])) {
  29. $values[0] = 0;
  30. }
  31. if (empty($values[1])) {
  32. $values[1] = 0;
  33. }
  34. }
  35. return $values;
  36. }
  37. public function get($id)
  38. {
  39. $client = $this->getClient();
  40. $key = $this->getKey($id);
  41. $values = $client->hgetall($key);
  42. // Recent versions of PhpRedis will return the Redis instance
  43. // instead of an empty array when the HGETALL target key does
  44. // not exists. I see what you did there.
  45. if (empty($values) || !is_array($values)) {
  46. return false;
  47. }
  48. return $values;
  49. }
  50. public function getMultiple(array $idList)
  51. {
  52. $ret = array();
  53. $pipe = $this->getClient()->pipeline();
  54. foreach ($idList as $id) {
  55. $pipe->hgetall($this->getKey($id));
  56. }
  57. $replies = $pipe->execute();
  58. foreach (array_values($idList) as $line => $id) {
  59. // HGETALL signature seems to differ depending on Predis versions.
  60. // This was found just after Predis update. Even though I'm not sure
  61. // this comes from Predis or just because we're misusing it.
  62. if (!empty($replies[$line]) && is_array($replies[$line])) {
  63. $ret[$id] = $replies[$line];
  64. }
  65. }
  66. return $ret;
  67. }
  68. public function set($id, $data, $ttl = null, $volatile = false)
  69. {
  70. // Ensure TTL consistency: if the caller gives us an expiry timestamp
  71. // in the past the key will expire now and will never be read.
  72. // Behavior between Predis and PhpRedis seems to change here: when
  73. // setting a negative expire time, PhpRedis seems to ignore the
  74. // command and leave the key permanent.
  75. if (null !== $ttl && $ttl <= 0) {
  76. return;
  77. }
  78. $key = $this->getKey($id);
  79. $data['volatile'] = (int)$volatile;
  80. $pipe = $this->getClient()->pipeline();
  81. $pipe->hmset($key, $data);
  82. if (null !== $ttl) {
  83. $pipe->expire($key, $ttl);
  84. }
  85. $pipe->execute();
  86. }
  87. public function delete($id)
  88. {
  89. $client = $this->getClient();
  90. $client->del($this->getKey($id));
  91. }
  92. public function deleteMultiple(array $idList)
  93. {
  94. $pipe = $this->getClient()->pipeline();
  95. foreach ($idList as $id) {
  96. $pipe->del($this->getKey($id));
  97. }
  98. $pipe->execute();
  99. }
  100. public function deleteByPrefix($prefix)
  101. {
  102. $client = $this->getClient();
  103. $ret = $client->eval(self::EVAL_DELETE_PREFIX, 0, $this->getKey($prefix . '*'));
  104. if (1 != $ret) {
  105. trigger_error(sprintf("EVAL failed: %s", $client->getLastError()), E_USER_ERROR);
  106. }
  107. }
  108. public function flush()
  109. {
  110. $client = $this->getClient();
  111. $ret = $client->eval(self::EVAL_DELETE_PREFIX, 0, $this->getKey('*'));
  112. if (1 != $ret) {
  113. trigger_error(sprintf("EVAL failed: %s", $client->getLastError()), E_USER_ERROR);
  114. }
  115. }
  116. public function flushVolatile()
  117. {
  118. $client = $this->getClient();
  119. $ret = $client->eval(self::EVAL_DELETE_VOLATILE, 0, $this->getKey('*'));
  120. if (1 != $ret) {
  121. trigger_error(sprintf("EVAL failed: %s", $client->getLastError()), E_USER_ERROR);
  122. }
  123. }
  124. }