You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 line
3.4KB

  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\di\Instance;
  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. * ```php
  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 <qiang.xue@gmail.com>
  34. * @since 2.0
  35. */
  36. class CacheSession extends Session
  37. {
  38. /**
  39. * @var Cache|array|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. * Starting from version 2.0.2, this can also be a configuration array for creating the object.
  46. */
  47. public $cache = 'cache';
  48. /**
  49. * Initializes the application component.
  50. */
  51. public function init()
  52. {
  53. parent::init();
  54. $this->cache = Instance::ensure($this->cache, Cache::className());
  55. }
  56. /**
  57. * Returns a value indicating whether to use custom session storage.
  58. * This method overrides the parent implementation and always returns true.
  59. * @return boolean whether to use custom storage.
  60. */
  61. public function getUseCustomStorage()
  62. {
  63. return true;
  64. }
  65. /**
  66. * Session read handler.
  67. * Do not call this method directly.
  68. * @param string $id session ID
  69. * @return string the session data
  70. */
  71. public function readSession($id)
  72. {
  73. $data = $this->cache->get($this->calculateKey($id));
  74. return $data === false ? '' : $data;
  75. }
  76. /**
  77. * Session write handler.
  78. * Do not call this method directly.
  79. * @param string $id session ID
  80. * @param string $data session data
  81. * @return boolean whether session write is successful
  82. */
  83. public function writeSession($id, $data)
  84. {
  85. return $this->cache->set($this->calculateKey($id), $data, $this->getTimeout());
  86. }
  87. /**
  88. * Session destroy handler.
  89. * Do not call this method directly.
  90. * @param string $id session ID
  91. * @return boolean whether session is destroyed successfully
  92. */
  93. public function destroySession($id)
  94. {
  95. return $this->cache->delete($this->calculateKey($id));
  96. }
  97. /**
  98. * Generates a unique key used for storing session data in cache.
  99. * @param string $id session variable name
  100. * @return mixed a safe cache key associated with the session variable name
  101. */
  102. protected function calculateKey($id)
  103. {
  104. return [__CLASS__, $id];
  105. }
  106. }