PredisCache.php 3.2 KB

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