CacheSession.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use yii\caching\CacheInterface;
  9. use yii\di\Instance;
  10. /**
  11. * CacheSession implements a session component using cache as storage medium.
  12. *
  13. * The cache being used can be any cache application component.
  14. * The ID of the cache application component is specified via [[cache]], which defaults to 'cache'.
  15. *
  16. * Beware, by definition cache storage are volatile, which means the data stored on them
  17. * may be swapped out and get lost. Therefore, you must make sure the cache used by this component
  18. * is NOT volatile. If you want to use database as storage medium, [[DbSession]] is a better choice.
  19. *
  20. * The following example shows how you can configure the application to use CacheSession:
  21. * Add the following to your application config under `components`:
  22. *
  23. * ```php
  24. * 'session' => [
  25. * 'class' => 'yii\web\CacheSession',
  26. * // 'cache' => 'mycache',
  27. * ]
  28. * ```
  29. *
  30. * @property-read bool $useCustomStorage Whether to use custom storage.
  31. *
  32. * @author Qiang Xue <qiang.xue@gmail.com>
  33. * @since 2.0
  34. */
  35. class CacheSession extends Session
  36. {
  37. /**
  38. * @var CacheInterface|array|string the cache object or the application component ID of the cache object.
  39. * The session data will be stored using this cache object.
  40. *
  41. * After the CacheSession object is created, if you want to change this property,
  42. * you should only assign it with a cache object.
  43. *
  44. * Starting from version 2.0.2, this can also be a configuration array for creating the object.
  45. */
  46. public $cache = 'cache';
  47. /**
  48. * Initializes the application component.
  49. */
  50. public function init()
  51. {
  52. parent::init();
  53. $this->cache = Instance::ensure($this->cache, 'yii\caching\CacheInterface');
  54. }
  55. /**
  56. * Returns a value indicating whether to use custom session storage.
  57. * This method overrides the parent implementation and always returns true.
  58. * @return bool whether to use custom storage.
  59. */
  60. public function getUseCustomStorage()
  61. {
  62. return true;
  63. }
  64. /**
  65. * Session open handler.
  66. * @internal Do not call this method directly.
  67. * @param string $savePath session save path
  68. * @param string $sessionName session name
  69. * @return bool whether session is opened successfully
  70. */
  71. public function openSession($savePath, $sessionName)
  72. {
  73. if ($this->getUseStrictMode()) {
  74. $id = $this->getId();
  75. if (!$this->cache->exists($this->calculateKey($id))) {
  76. //This session id does not exist, mark it for forced regeneration
  77. $this->_forceRegenerateId = $id;
  78. }
  79. }
  80. return parent::openSession($savePath, $sessionName);
  81. }
  82. /**
  83. * Session read handler.
  84. * @internal Do not call this method directly.
  85. * @param string $id session ID
  86. * @return string the session data
  87. */
  88. public function readSession($id)
  89. {
  90. $data = $this->cache->get($this->calculateKey($id));
  91. return $data === false ? '' : $data;
  92. }
  93. /**
  94. * Session write handler.
  95. * @internal Do not call this method directly.
  96. * @param string $id session ID
  97. * @param string $data session data
  98. * @return bool whether session write is successful
  99. */
  100. public function writeSession($id, $data)
  101. {
  102. if ($this->getUseStrictMode() && $id === $this->_forceRegenerateId) {
  103. //Ignore write when forceRegenerate is active for this id
  104. return true;
  105. }
  106. return $this->cache->set($this->calculateKey($id), $data, $this->getTimeout());
  107. }
  108. /**
  109. * Session destroy handler.
  110. * @internal Do not call this method directly.
  111. * @param string $id session ID
  112. * @return bool whether session is destroyed successfully
  113. */
  114. public function destroySession($id)
  115. {
  116. $cacheId = $this->calculateKey($id);
  117. if ($this->cache->exists($cacheId) === false) {
  118. return true;
  119. }
  120. return $this->cache->delete($cacheId);
  121. }
  122. /**
  123. * Generates a unique key used for storing session data in cache.
  124. * @param string $id session variable name
  125. * @return mixed a safe cache key associated with the session variable name
  126. */
  127. protected function calculateKey($id)
  128. {
  129. return [__CLASS__, $id];
  130. }
  131. }