CCacheHttpSession.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. const CACHE_KEY_PREFIX='assistant_cache_session_';
  34. /**
  35. * @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
  36. */
  37. public $cacheID='cache';
  38. /**
  39. * @var ICache the cache component
  40. */
  41. private $_cache;
  42. /**
  43. * Initializes the application component.
  44. * This method overrides the parent implementation by checking if cache is available.
  45. */
  46. public function init()
  47. {
  48. $this->_cache=Yii::app()->getComponent($this->cacheID);
  49. if(!($this->_cache instanceof ICache))
  50. throw new CException(Yii::t('yii','CCacheHttpSession.cacheID is invalid. Please make sure "{id}" refers to a valid cache application component.',
  51. array('{id}'=>$this->cacheID)));
  52. parent::init();
  53. }
  54. /**
  55. * Returns a value indicating whether to use custom session storage.
  56. * This method overrides the parent implementation and always returns true.
  57. * @return boolean whether to use custom storage.
  58. */
  59. public function getUseCustomStorage()
  60. {
  61. return true;
  62. }
  63. /**
  64. * Session read handler.
  65. * Do not call this method directly.
  66. * @param string $id session ID
  67. * @return string the session data
  68. */
  69. public function readSession($id)
  70. {
  71. $data=$this->_cache->get($this->calculateKey($id));
  72. return $data===false?'':$data;
  73. }
  74. /**
  75. * Session write handler.
  76. * Do not call this method directly.
  77. * @param string $id session ID
  78. * @param string $data session data
  79. * @return boolean whether session write is successful
  80. */
  81. public function writeSession($id,$data)
  82. {
  83. return $this->_cache->set($this->calculateKey($id),$data,$this->getTimeout());
  84. }
  85. /**
  86. * Session destroy handler.
  87. * Do not call this method directly.
  88. * @param string $id session ID
  89. * @return boolean whether session is destroyed successfully
  90. */
  91. public function destroySession($id)
  92. {
  93. return $this->_cache->delete($this->calculateKey($id));
  94. }
  95. /**
  96. * Generates a unique key used for storing session data in cache.
  97. * @param string $id session variable name
  98. * @return string a safe cache key associated with the session variable name
  99. */
  100. protected function calculateKey($id)
  101. {
  102. return self::CACHE_KEY_PREFIX.$id;
  103. }
  104. }