CacheSession.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\CacheInterface;
  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 bool $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 CacheInterface|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, 'yii\caching\CacheInterface');
  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 bool whether to use custom storage.
  60. */
  61. public function getUseCustomStorage()
  62. {
  63. return true;
  64. }
  65. /**
  66. * Session read handler.
  67. * @internal 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. * @internal Do not call this method directly.
  79. * @param string $id session ID
  80. * @param string $data session data
  81. * @return bool 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. * @internal Do not call this method directly.
  90. * @param string $id session ID
  91. * @return bool whether session is destroyed successfully
  92. */
  93. public function destroySession($id)
  94. {
  95. $cacheId = $this->calculateKey($id);
  96. if ($this->cache->exists($cacheId) === false) {
  97. return true;
  98. }
  99. return $this->cache->delete($cacheId);
  100. }
  101. /**
  102. * Generates a unique key used for storing session data in cache.
  103. * @param string $id session variable name
  104. * @return mixed a safe cache key associated with the session variable name
  105. */
  106. protected function calculateKey($id)
  107. {
  108. return [__CLASS__, $id];
  109. }
  110. }