Session.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\redis;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * Redis Session implements a session component using [redis](http://redis.io/) as the storage medium.
  12. *
  13. * Redis Session requires redis version 2.6.12 or higher to work properly.
  14. *
  15. * It needs to be configured with a redis [[Connection]] that is also configured as an application component.
  16. * By default it will use the `redis` application component.
  17. *
  18. * To use redis Session as the session application component, configure the application as follows,
  19. *
  20. * ~~~
  21. * [
  22. * 'components' => [
  23. * 'session' => [
  24. * 'class' => 'yii\redis\Session',
  25. * 'redis' => [
  26. * 'hostname' => 'localhost',
  27. * 'port' => 6379,
  28. * 'database' => 0,
  29. * ]
  30. * ],
  31. * ],
  32. * ]
  33. * ~~~
  34. *
  35. * Or if you have configured the redis [[Connection]] as an application component, the following is sufficient:
  36. *
  37. * ~~~
  38. * [
  39. * 'components' => [
  40. * 'session' => [
  41. * 'class' => 'yii\redis\Session',
  42. * // 'redis' => 'redis' // id of the connection application component
  43. * ],
  44. * ],
  45. * ]
  46. * ~~~
  47. *
  48. * @property bool $useCustomStorage Whether to use custom storage. This property is read-only.
  49. *
  50. * @author Carsten Brandt <mail@cebe.cc>
  51. * @since 2.0
  52. */
  53. class Session extends \yii\web\Session
  54. {
  55. /**
  56. * @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
  57. * This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
  58. * redis connection as an application component.
  59. * After the Session object is created, if you want to change this property, you should only assign it
  60. * with a Redis [[Connection]] object.
  61. */
  62. public $redis = 'redis';
  63. /**
  64. * @var string a string prefixed to every cache key so that it is unique. If not set,
  65. * it will use a prefix generated from [[Application::id]]. You may set this property to be an empty string
  66. * if you don't want to use key prefix. It is recommended that you explicitly set this property to some
  67. * static value if the cached data needs to be shared among multiple applications.
  68. */
  69. public $keyPrefix;
  70. /**
  71. * Initializes the redis Session component.
  72. * This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
  73. * @throws InvalidConfigException if [[redis]] is invalid.
  74. */
  75. public function init()
  76. {
  77. if (is_string($this->redis)) {
  78. $this->redis = Yii::$app->get($this->redis);
  79. } elseif (is_array($this->redis)) {
  80. if (!isset($this->redis['class'])) {
  81. $this->redis['class'] = Connection::className();
  82. }
  83. $this->redis = Yii::createObject($this->redis);
  84. }
  85. if (!$this->redis instanceof Connection) {
  86. throw new InvalidConfigException("Session::redis must be either a Redis connection instance or the application component ID of a Redis connection.");
  87. }
  88. if ($this->keyPrefix === null) {
  89. $this->keyPrefix = substr(md5(Yii::$app->id), 0, 5);
  90. }
  91. parent::init();
  92. }
  93. /**
  94. * Returns a value indicating whether to use custom session storage.
  95. * This method overrides the parent implementation and always returns true.
  96. * @return bool whether to use custom storage.
  97. */
  98. public function getUseCustomStorage()
  99. {
  100. return true;
  101. }
  102. /**
  103. * Session read handler.
  104. * Do not call this method directly.
  105. * @param string $id session ID
  106. * @return string the session data
  107. */
  108. public function readSession($id)
  109. {
  110. $data = $this->redis->executeCommand('GET', [$this->calculateKey($id)]);
  111. return $data === false || $data === null ? '' : $data;
  112. }
  113. /**
  114. * Session write handler.
  115. * Do not call this method directly.
  116. * @param string $id session ID
  117. * @param string $data session data
  118. * @return bool whether session write is successful
  119. */
  120. public function writeSession($id, $data)
  121. {
  122. return (bool) $this->redis->executeCommand('SET', [$this->calculateKey($id), $data, 'EX', $this->getTimeout()]);
  123. }
  124. /**
  125. * Session destroy handler.
  126. * Do not call this method directly.
  127. * @param string $id session ID
  128. * @return bool whether session is destroyed successfully
  129. */
  130. public function destroySession($id)
  131. {
  132. $this->redis->executeCommand('DEL', [$this->calculateKey($id)]);
  133. // @see https://github.com/yiisoft/yii2-redis/issues/82
  134. return true;
  135. }
  136. /**
  137. * Generates a unique key used for storing session data in cache.
  138. * @param string $id session variable name
  139. * @return string a safe cache key associated with the session variable name
  140. */
  141. protected function calculateKey($id)
  142. {
  143. return $this->keyPrefix . md5(json_encode([__CLASS__, $id]));
  144. }
  145. }