PredisCache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use Predis\ClientInterface;
  4. /**
  5. * Predis cache provider.
  6. *
  7. * @author othillo <othillo@othillo.nl>
  8. */
  9. class PredisCache extends CacheProvider
  10. {
  11. /**
  12. * @var ClientInterface
  13. */
  14. private $client;
  15. /**
  16. * @param ClientInterface $client
  17. *
  18. * @return void
  19. */
  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 (null === $result) {
  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(array($this->client, 'mget'), $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 = $this->client->setex($key, $lifetime, serialize($value));
  53. if ((string) $response != 'OK') {
  54. $success = false;
  55. }
  56. }
  57. return $success;
  58. }
  59. // No lifetime, use MSET
  60. $response = $this->client->mset(array_map(function ($value) {
  61. return serialize($value);
  62. }, $keysAndValues));
  63. return (string) $response == 'OK';
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function doContains($id)
  69. {
  70. return (bool) $this->client->exists($id);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function doSave($id, $data, $lifeTime = 0)
  76. {
  77. $data = serialize($data);
  78. if ($lifeTime > 0) {
  79. $response = $this->client->setex($id, $lifeTime, $data);
  80. } else {
  81. $response = $this->client->set($id, $data);
  82. }
  83. return $response === true || $response == 'OK';
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function doDelete($id)
  89. {
  90. return $this->client->del($id) >= 0;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function doFlush()
  96. {
  97. $response = $this->client->flushdb();
  98. return $response === true || $response == 'OK';
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function doGetStats()
  104. {
  105. $info = $this->client->info();
  106. return array(
  107. Cache::STATS_HITS => $info['Stats']['keyspace_hits'],
  108. Cache::STATS_MISSES => $info['Stats']['keyspace_misses'],
  109. Cache::STATS_UPTIME => $info['Server']['uptime_in_seconds'],
  110. Cache::STATS_MEMORY_USAGE => $info['Memory']['used_memory'],
  111. Cache::STATS_MEMORY_AVAILABLE => false
  112. );
  113. }
  114. }