Redis.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Session\SaveHandler;
  7. use Cm\RedisSession\Handler\ConfigInterface;
  8. use Cm\RedisSession\Handler\LoggerInterface;
  9. use Cm\RedisSession\ConnectionFailedException;
  10. use Cm\RedisSession\ConcurrentConnectionsExceededException;
  11. use Magento\Framework\Exception\SessionException;
  12. use Magento\Framework\Phrase;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\App\Filesystem\DirectoryList;
  15. class Redis implements \SessionHandlerInterface
  16. {
  17. /**
  18. * @var ConfigInterface
  19. */
  20. private $config;
  21. /**
  22. * @var LoggerInterface
  23. */
  24. private $logger;
  25. /**
  26. * @var Filesystem
  27. */
  28. private $filesystem;
  29. /**
  30. * @var \Cm\RedisSession\Handler[]
  31. */
  32. private $connection;
  33. /**
  34. * @param ConfigInterface $config
  35. * @param LoggerInterface $logger
  36. * @param Filesystem $filesystem
  37. * @throws SessionException
  38. */
  39. public function __construct(ConfigInterface $config, LoggerInterface $logger, Filesystem $filesystem)
  40. {
  41. $this->config = $config;
  42. $this->logger = $logger;
  43. $this->filesystem = $filesystem;
  44. }
  45. /**
  46. * Get connection
  47. *
  48. * @return \Cm\RedisSession\Handler
  49. * @throws SessionException
  50. */
  51. private function getConnection()
  52. {
  53. $pid = getmypid();
  54. if (!isset($this->connection[$pid])) {
  55. try {
  56. $this->connection[$pid] = new \Cm\RedisSession\Handler($this->config, $this->logger);
  57. } catch (ConnectionFailedException $e) {
  58. throw new SessionException(new Phrase($e->getMessage()));
  59. }
  60. }
  61. return $this->connection[$pid];
  62. }
  63. /**
  64. * Open session
  65. *
  66. * @param string $savePath ignored
  67. * @param string $sessionName ignored
  68. * @return bool
  69. * @throws SessionException
  70. */
  71. public function open($savePath, $sessionName)
  72. {
  73. return $this->getConnection()->open($savePath, $sessionName);
  74. }
  75. /**
  76. * Fetch session data
  77. *
  78. * @param string $sessionId
  79. * @return string
  80. * @throws ConcurrentConnectionsExceededException
  81. * @throws SessionException
  82. */
  83. public function read($sessionId)
  84. {
  85. try {
  86. return $this->getConnection()->read($sessionId);
  87. } catch (ConcurrentConnectionsExceededException $e) {
  88. require $this->filesystem->getDirectoryRead(DirectoryList::PUB)->getAbsolutePath('errors/503.php');
  89. }
  90. }
  91. /**
  92. * Update session
  93. *
  94. * @param string $sessionId
  95. * @param string $sessionData
  96. * @return boolean
  97. * @throws SessionException
  98. */
  99. public function write($sessionId, $sessionData)
  100. {
  101. return $this->getConnection()->write($sessionId, $sessionData);
  102. }
  103. /**
  104. * Destroy session
  105. *
  106. * @param string $sessionId
  107. * @return boolean
  108. * @throws SessionException
  109. */
  110. public function destroy($sessionId)
  111. {
  112. return $this->getConnection()->destroy($sessionId);
  113. }
  114. /**
  115. * Overridden to prevent calling getLifeTime at shutdown
  116. *
  117. * @return bool
  118. * @throws SessionException
  119. */
  120. public function close()
  121. {
  122. return $this->getConnection()->close();
  123. }
  124. /**
  125. * Garbage collection
  126. *
  127. * @param int $maxLifeTime ignored
  128. * @return boolean
  129. * @throws SessionException
  130. * @SuppressWarnings(PHPMD.ShortMethodName)
  131. */
  132. public function gc($maxLifeTime)
  133. {
  134. return $this->getConnection()->gc($maxLifeTime);
  135. }
  136. /**
  137. * Get the number of failed lock attempts
  138. *
  139. * @return int
  140. * @throws SessionException
  141. */
  142. public function getFailedLockAttempts()
  143. {
  144. return $this->getConnection()->getFailedLockAttempts();
  145. }
  146. }