CStatePersister.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * This file contains classes implementing security manager feature.
  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. * CStatePersister implements a file-based persistent data storage.
  12. *
  13. * It can be used to keep data available through multiple requests and sessions.
  14. *
  15. * By default, CStatePersister stores data in a file named 'state.bin' that is located
  16. * under the application {@link CApplication::getRuntimePath runtime path}.
  17. * You may change the location by setting the {@link stateFile} property.
  18. *
  19. * To retrieve the data from CStatePersister, call {@link load()}. To save the data,
  20. * call {@link save()}.
  21. *
  22. * Comparison among state persister, session and cache is as follows:
  23. * <ul>
  24. * <li>session: data persisting within a single user session.</li>
  25. * <li>state persister: data persisting through all requests/sessions (e.g. hit counter).</li>
  26. * <li>cache: volatile and fast storage. It may be used as storage medium for session or state persister.</li>
  27. * </ul>
  28. *
  29. * Since server resource is often limited, be cautious if you plan to use CStatePersister
  30. * to store large amount of data. You should also consider using database-based persister
  31. * to improve the throughput.
  32. *
  33. * CStatePersister is a core application component used to store global application state.
  34. * It may be accessed via {@link CApplication::getStatePersister()}.
  35. * page state persistent method based on cache.
  36. *
  37. * @author Qiang Xue <qiang.xue@gmail.com>
  38. * @package system.base
  39. * @since 1.0
  40. */
  41. class CStatePersister extends CApplicationComponent implements IStatePersister
  42. {
  43. /**
  44. * @var string the file path storing the state data. Make sure the directory containing
  45. * the file exists and is writable by the Web server process. If using relative path, also
  46. * make sure the path is correct.
  47. */
  48. public $stateFile;
  49. /**
  50. * @var string the ID of the cache application component that is used to cache the state values.
  51. * Defaults to 'cache' which refers to the primary cache application component.
  52. * Set this property to false if you want to disable caching state values.
  53. */
  54. public $cacheID='cache';
  55. /**
  56. * Initializes the component.
  57. * This method overrides the parent implementation by making sure {@link stateFile}
  58. * contains valid value.
  59. */
  60. public function init()
  61. {
  62. parent::init();
  63. if($this->stateFile===null)
  64. $this->stateFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'state.bin';
  65. $dir=dirname($this->stateFile);
  66. if(!is_dir($dir) || !is_writable($dir))
  67. throw new CException(Yii::t('yii','Unable to create application state file "{file}". Make sure the directory containing the file exists and is writable by the Web server process.',
  68. array('{file}'=>$this->stateFile)));
  69. }
  70. /**
  71. * Loads state data from persistent storage.
  72. * @return mixed state data. Null if no state data available.
  73. */
  74. public function load()
  75. {
  76. $stateFile=$this->stateFile;
  77. if($this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
  78. {
  79. $cacheKey='Yii.CStatePersister.'.$stateFile;
  80. if(($value=$cache->get($cacheKey))!==false)
  81. return unserialize($value);
  82. elseif(($content=@file_get_contents($stateFile))!==false)
  83. {
  84. $cache->set($cacheKey,$content,0,new CFileCacheDependency($stateFile));
  85. return unserialize($content);
  86. }
  87. else
  88. return null;
  89. }
  90. elseif(($content=@file_get_contents($stateFile))!==false)
  91. return unserialize($content);
  92. else
  93. return null;
  94. }
  95. /**
  96. * Saves application state in persistent storage.
  97. * @param mixed $state state data (must be serializable).
  98. */
  99. public function save($state)
  100. {
  101. file_put_contents($this->stateFile,serialize($state),LOCK_EX);
  102. }
  103. }