Predis.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * PhpRedis implementation.
  4. *
  5. * @todo
  6. * Set high expire value to the hash for rotation when memory is empty
  7. * React upon cache clear all and rebuild path list?
  8. */
  9. class Redis_Path_Predis extends Redis_Path_AbstractHashLookup
  10. {
  11. protected function saveInHash($key, $hkey, $hvalue)
  12. {
  13. $client = $this->getClient();
  14. $value = $client->hget($key, $hkey);
  15. if ($value === self::VALUE_NULL) { // Remove any null values
  16. $value = null;
  17. }
  18. if ($value) {
  19. $existing = explode(self::VALUE_SEPARATOR, $value);
  20. if (!in_array($hvalue, $existing)) {
  21. // Prepend the most recent path to ensure it always be
  22. // first fetched one
  23. // @todo Ensure in case of update that its position does
  24. // not changes (pid ordering in Drupal core)
  25. $value = $hvalue . self::VALUE_SEPARATOR . $value;
  26. } else { // Do nothing on empty value
  27. $value = null;
  28. }
  29. } else if (empty($hvalue)) {
  30. $value = self::VALUE_NULL;
  31. } else {
  32. $value = $hvalue;
  33. }
  34. if (!empty($value)) {
  35. $client->hset($key, $hkey, $value);
  36. }
  37. // Empty value here means that we already got it
  38. }
  39. protected function deleteInHash($key, $hkey, $hvalue)
  40. {
  41. $client = $this->getClient();
  42. $value = $client->hget($key, $hkey);
  43. if ($value) {
  44. $existing = explode(self::VALUE_SEPARATOR, $value);
  45. if (false !== ($index = array_search($hvalue, $existing))) {
  46. if (1 === count($existing)) {
  47. $client->hdel($key, $hkey);
  48. } else {
  49. unset($existing[$index]);
  50. $client->hset($key, $hkey, implode(self::VALUE_SEPARATOR, $existing));
  51. }
  52. }
  53. }
  54. }
  55. protected function lookupInHash($keyPrefix, $hkey, $language = null)
  56. {
  57. $client = $this->getClient();
  58. if (null === $language) {
  59. $language = LANGUAGE_NONE;
  60. $doNoneLookup = false;
  61. } else if (LANGUAGE_NONE === $language) {
  62. $doNoneLookup = false;
  63. } else {
  64. $doNoneLookup = true;
  65. }
  66. $ret = $client->hget($this->getKey(array($keyPrefix, $language)), $hkey);
  67. if ($doNoneLookup && (!$ret || self::VALUE_NULL === $ret)) {
  68. $previous = $ret;
  69. $ret = $client->hget($this->getKey(array($keyPrefix, LANGUAGE_NONE)), $hkey);
  70. if (!$ret || self::VALUE_NULL === $ret) {
  71. // Restore null placeholder else we loose conversion to false
  72. // and drupal_lookup_path() would attempt saving it once again
  73. $ret = $previous;
  74. }
  75. }
  76. if (self::VALUE_NULL === $ret) {
  77. return false; // Needs conversion
  78. }
  79. if (empty($ret)) {
  80. return null; // Value not found
  81. }
  82. $existing = explode(self::VALUE_SEPARATOR, $ret);
  83. return reset($existing);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function deleteLanguage($language)
  89. {
  90. $client = $this->getClient();
  91. $client->del($this->getKey(array(self::KEY_ALIAS, $language)));
  92. $client->del($this->getKey(array(self::KEY_SOURCE, $language)));
  93. }
  94. }