PredisCache.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use Predis\ClientInterface;
  4. use function array_combine;
  5. use function array_filter;
  6. use function array_map;
  7. use function array_values;
  8. use function call_user_func_array;
  9. use function serialize;
  10. use function unserialize;
  11. /**
  12. * Predis cache provider.
  13. *
  14. * @deprecated Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0
  15. */
  16. class PredisCache extends CacheProvider
  17. {
  18. /** @var ClientInterface */
  19. private $client;
  20. public function __construct(ClientInterface $client)
  21. {
  22. $this->client = $client;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function doFetch($id)
  28. {
  29. $result = $this->client->get($id);
  30. if ($result === null) {
  31. return false;
  32. }
  33. return unserialize($result);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function doFetchMultiple(array $keys)
  39. {
  40. $fetchedItems = call_user_func_array([$this->client, 'mget'], array_values($keys));
  41. return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  47. {
  48. if ($lifetime) {
  49. $success = true;
  50. // Keys have lifetime, use SETEX for each of them
  51. foreach ($keysAndValues as $key => $value) {
  52. $response = (string) $this->client->setex($key, $lifetime, serialize($value));
  53. if ($response == 'OK') {
  54. continue;
  55. }
  56. $success = false;
  57. }
  58. return $success;
  59. }
  60. // No lifetime, use MSET
  61. $response = $this->client->mset(array_map(static function ($value) {
  62. return serialize($value);
  63. }, $keysAndValues));
  64. return (string) $response == 'OK';
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function doContains($id)
  70. {
  71. return (bool) $this->client->exists($id);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. protected function doSave($id, $data, $lifeTime = 0)
  77. {
  78. $data = serialize($data);
  79. if ($lifeTime > 0) {
  80. $response = $this->client->setex($id, $lifeTime, $data);
  81. } else {
  82. $response = $this->client->set($id, $data);
  83. }
  84. return $response === true || $response == 'OK';
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function doDelete($id)
  90. {
  91. return $this->client->del($id) >= 0;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function doDeleteMultiple(array $keys)
  97. {
  98. return $this->client->del($keys) >= 0;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function doFlush()
  104. {
  105. $response = $this->client->flushdb();
  106. return $response === true || $response == 'OK';
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. protected function doGetStats()
  112. {
  113. $info = $this->client->info();
  114. return [
  115. Cache::STATS_HITS => $info['Stats']['keyspace_hits'],
  116. Cache::STATS_MISSES => $info['Stats']['keyspace_misses'],
  117. Cache::STATS_UPTIME => $info['Server']['uptime_in_seconds'],
  118. Cache::STATS_MEMORY_USAGE => $info['Memory']['used_memory'],
  119. Cache::STATS_MEMORY_AVAILABLE => false,
  120. ];
  121. }
  122. }