CacheSession.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\web;
  8. use Yii;
  9. use yii\caching\Cache;
  10. use yii\base\InvalidConfigException;
  11. /**
  12. * CacheSession implements a session component using cache as storage medium.
  13. *
  14. * The cache being used can be any cache application component.
  15. * The ID of the cache application component is specified via [[cache]], which defaults to 'cache'.
  16. *
  17. * Beware, by definition cache storage are volatile, which means the data stored on them
  18. * may be swapped out and get lost. Therefore, you must make sure the cache used by this component
  19. * is NOT volatile. If you want to use database as storage medium, [[DbSession]] is a better choice.
  20. *
  21. * The following example shows how you can configure the application to use CacheSession:
  22. * Add the following to your application config under `components`:
  23. *
  24. * ~~~
  25. * 'session' => [
  26. * 'class' => 'yii\web\CacheSession',
  27. * // 'cache' => 'mycache',
  28. * ]
  29. * ~~~
  30. *
  31. * @property boolean $useCustomStorage Whether to use custom storage. This property is read-only.
  32. *
  33. * @author Qiang Xue <[email protected]>
  34. * @since 2.0
  35. */
  36. class CacheSession extends Session
  37. {
  38. /**
  39. * @var Cache|string the cache object or the application component ID of the cache object.
  40. * The session data will be stored using this cache object.
  41. *
  42. * After the CacheSession object is created, if you want to change this property,
  43. * you should only assign it with a cache object.
  44. */
  45. public $cache = 'cache';
  46. /**
  47. * Initializes the application component.
  48. */
  49. public function init()
  50. {
  51. if (is_string($this->cache)) {
  52. $this->cache = Yii::$app->getComponent($this->cache);
  53. }
  54. if (!$this->cache instanceof Cache) {
  55. throw new InvalidConfigException('CacheSession::cache must refer to the application component ID of a cache object.');
  56. }
  57. parent::init();
  58. }
  59. /**
  60. * Returns a value indicating whether to use custom session storage.
  61. * This method overrides the parent implementation and always returns true.
  62. * @return boolean whether to use custom storage.
  63. */
  64. public function getUseCustomStorage()
  65. {
  66. return true;
  67. }
  68. /**
  69. * Session read handler.
  70. * Do not call this method directly.
  71. * @param string $id session ID
  72. * @return string the session data
  73. */
  74. public function readSession($id)
  75. {
  76. $data = $this->cache->get($this->calculateKey($id));
  77. return $data === false ? '' : $data;
  78. }
  79. /**
  80. * Session write handler.
  81. * Do not call this method directly.
  82. * @param string $id session ID
  83. * @param string $data session data
  84. * @return boolean whether session write is successful
  85. */
  86. public function writeSession($id, $data)
  87. {
  88. return $this->cache->set($this->calculateKey($id), $data, $this->getTimeout());
  89. }
  90. /**
  91. * Session destroy handler.
  92. * Do not call this method directly.
  93. * @param string $id session ID
  94. * @return boolean whether session is destroyed successfully
  95. */
  96. public function destroySession($id)
  97. {
  98. return $this->cache->delete($this->calculateKey($id));
  99. }
  100. /**
  101. * Generates a unique key used for storing session data in cache.
  102. * @param string $id session variable name
  103. * @return mixed a safe cache key associated with the session variable name
  104. */
  105. protected function calculateKey($id)
  106. {
  107. return [__CLASS__, $id];
  108. }
  109. }