system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CStatePersister

system.base
继承 class CStatePersister » CApplicationComponent » CComponent
实现 IApplicationComponent, IStatePersister
可用自 1.0
源码 framework/base/CStatePersister.php
CStatePersister 实现一个基于文件的持久数据存储。

它可以用来保持多个请求或会话的数据。

默认,CStatePersister 存储数据在一个名字叫 'state.bin' 文件中 它位于应用程序指定的 runtime path 下。 你可以通过设置 stateFile 属性来改变它的位置。

To retrieve the data from CStatePersister, call load(). To save the data, call save().

持久数据,会话和缓存之间的比较如下:
  • session: 单个用户的会话持久数据。
  • state persister: 所有请求的/会话的持久数据(例如,点击统计)。
  • cache: 不稳定并快速的存诸。它可的使用介于会话和持久数据之间。


Since server resource is often limited, be cautious if you plan to use CStatePersister to store large amount of data. You should also consider using database-based persister to improve the throughput.

CStatePersister is a core application component used to store global application state. It may be accessed via CApplication::getStatePersister().

公共属性

隐藏继承的属性

属性类型描述被定义在
behaviors array the behaviors that should be attached to this component. CApplicationComponent
cacheID string the ID of the cache application component that is used to cache the state values. CStatePersister
isInitialized boolean Checks if this application component has been initialized. CApplicationComponent
stateFile string the file path storing the state data. CStatePersister

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() Checks if this application component has been initialized. CApplicationComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() 初始化这个组件。 CStatePersister
load() 从持久存储加载状态数据。 CStatePersister
raiseEvent() Raises an event. CComponent
save() Saves application state in persistent storage. CStatePersister

受保护的方法

隐藏继承的方法

方法描述被定义在
getContent() Loads content from file using a shared lock to avoid data corruption when reading CStatePersister

属性详情

cacheID 属性
public string $cacheID;

the ID of the cache application component that is used to cache the state values. Defaults to 'cache' which refers to the primary cache application component. Set this property to false if you want to disable caching state values.

stateFile 属性
public string $stateFile;

the file path storing the state data. Make sure the directory containing the file exists and is writable by the Web server process. If using relative path, also make sure the path is correct.

方法详情

getContent() 方法 (自版本 v1.1.17 可用)
protected bool|string getContent(string $filename)
$filename string file name
{return} bool|string file contents
源码: framework/base/CStatePersister.php#112 (显示)
protected function getContent($filename)
{
    
$file=@fopen($filename,"r");
    if(
$file && flock($file,LOCK_SH))
    {
        
$contents=@file_get_contents($filename);
        
flock($file,LOCK_UN);
        
fclose($file);
        return 
$contents;
    }
    return 
false;
}

Loads content from file using a shared lock to avoid data corruption when reading the file while it is being written by save()

init() 方法
public void init()
源码: framework/base/CStatePersister.php#61 (显示)
public function init()
{
    
parent::init();
    if(
$this->stateFile===null)
        
$this->stateFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'state.bin';
    
$dir=dirname($this->stateFile);
    if(!
is_dir($dir) || !is_writable($dir))
        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.',
            array(
'{file}'=>$this->stateFile)));
}

初始化这个组件。 这个方法重载了父类的实现, 添加了一个 stateFile 的有效值。

load() 方法
public mixed load()
{return} mixed 状态数据。如果状态数据不可用返回 null。
源码: framework/base/CStatePersister.php#76 (显示)
public function load()
{
    
$stateFile=$this->stateFile;
    if(
$this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
    {
        
$cacheKey='Yii.CStatePersister.'.$stateFile;
        if((
$value=$cache->get($cacheKey))!==false)
            return 
unserialize($value);
        else
        {
            if((
$content=$this->getContent($stateFile))!==false)
            {
                
$unserialized_content=unserialize($content);
                
// If it can't be unserialized, don't cache it:
                
if ($unserialized_content!==false || $content==""
                    
$cache->set($cacheKey,$content,0,new CFileCacheDependency($stateFile));
                return 
$unserialized_content;
            }
            else
                return 
null;
        }
    }
    elseif((
$content=$this->getContent($stateFile))!==false)
        return 
unserialize($content);
    else
        return 
null;
}

从持久存储加载状态数据。

save() 方法
public void save(mixed $state)
$state mixed state data (must be serializable).
源码: framework/base/CStatePersister.php#129 (显示)
public function save($state)
{
    
file_put_contents($this->stateFile,serialize($state),LOCK_EX);
}

Saves application state in persistent storage.