Session.php 4.7 KB

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