CCacheHttpSession.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * CCacheHttpSession class
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CCacheHttpSession implements a session component using cache as storage medium.
  12. *
  13. * The cache being used can be any cache application component implementing {@link ICache} interface.
  14. * The ID of the cache application component is specified via {@link cacheID}, 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 {@link CDbCache} as storage medium, use {@link CDbHttpSession}
  19. * is a better choice.
  20. *
  21. * @property boolean $useCustomStorage Whether to use custom storage.
  22. *
  23. * @author Qiang Xue <qiang.xue@gmail.com>
  24. * @package system.web
  25. * @since 1.0
  26. */
  27. class CCacheHttpSession extends CHttpSession
  28. {
  29. /**
  30. * Prefix to the keys for storing cached data
  31. */
  32. const CACHE_KEY_PREFIX='Yii.CCacheHttpSession.';
  33. /**
  34. * @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
  35. */
  36. public $cacheID='cache';
  37. /**
  38. * @var ICache the cache component
  39. */
  40. private $_cache;
  41. /**
  42. * Initializes the application component.
  43. * This method overrides the parent implementation by checking if cache is available.
  44. */
  45. public function init()
  46. {
  47. $this->_cache=Yii::app()->getComponent($this->cacheID);
  48. if(!($this->_cache instanceof ICache))
  49. throw new CException(Yii::t('yii','CCacheHttpSession.cacheID is invalid. Please make sure "{id}" refers to a valid cache application component.',
  50. array('{id}'=>$this->cacheID)));
  51. parent::init();
  52. }
  53. /**
  54. * Returns a value indicating whether to use custom session storage.
  55. * This method overrides the parent implementation and always returns true.
  56. * @return boolean whether to use custom storage.
  57. */
  58. public function getUseCustomStorage()
  59. {
  60. return true;
  61. }
  62. /**
  63. * Session read handler.
  64. * Do not call this method directly.
  65. * @param string $id session ID
  66. * @return string the session data
  67. */
  68. public function readSession($id)
  69. {
  70. $data=$this->_cache->get($this->calculateKey($id));
  71. return $data===false?'':$data;
  72. }
  73. /**
  74. * Session write handler.
  75. * Do not call this method directly.
  76. * @param string $id session ID
  77. * @param string $data session data
  78. * @return boolean whether session write is successful
  79. */
  80. public function writeSession($id,$data)
  81. {
  82. return $this->_cache->set($this->calculateKey($id),$data,$this->getTimeout());
  83. }
  84. /**
  85. * Session destroy handler.
  86. * Do not call this method directly.
  87. * @param string $id session ID
  88. * @return boolean whether session is destroyed successfully
  89. */
  90. public function destroySession($id)
  91. {
  92. return $this->_cache->delete($this->calculateKey($id));
  93. }
  94. /**
  95. * Generates a unique key used for storing session data in cache.
  96. * @param string $id session variable name
  97. * @return string a safe cache key associated with the session variable name
  98. */
  99. protected function calculateKey($id)
  100. {
  101. return self::CACHE_KEY_PREFIX.$id;
  102. }
  103. }