| 包 | system.web |
|---|---|
| 继承 | class CCacheHttpsession » CHttpSession » CApplicationComponent » CComponent |
| 实现 | Countable, ArrayAccess, Traversable, IteratorAggregate, IApplicationComponent |
| 源自 | 1.0 |
| 版本 | $Id: CCacheHttpSession.php 3426 2011-10-25 00:01:09Z alexander.makarow $ |
| 源码 |
CCacheHttpSession实现了使用缓存作为存储介质的会话组件。
只要是实现了ICache接口的应用组件都可以作为缓存。 这个缓存ID是通过cacheID来指定的,默认值是‘cache’。
要注意的是,缓存介质是很容易改变的, 那就意味着存储在上面的数据会被刷掉。因此,你必须确定这个组件使用的存储是容易改变。 如果你想要用CDbCache作为存储介质,CDbHttpSession 会是一个更好的选择。
只要是实现了ICache接口的应用组件都可以作为缓存。 这个缓存ID是通过cacheID来指定的,默认值是‘cache’。
要注意的是,缓存介质是很容易改变的, 那就意味着存储在上面的数据会被刷掉。因此,你必须确定这个组件使用的存储是容易改变。 如果你想要用CDbCache作为存储介质,CDbHttpSession 会是一个更好的选择。
公共属性
| 属性 | 类型 | 描述 | 定义在 |
|---|---|---|---|
| autoStart | boolean | whether the session should be automatically started when the session application component is initialized, defaults to true. | CHttpSession |
| behaviors | array | 这个应用组件附加的行为。 这此行为将在应用组件调用init时附加在应用组件上。 请参照CModel::behaviors如何指定此属性值。 | CApplicationComponent |
| cacheID | string | 缓存应用组件ID。默认是‘cache’(原始缓存应用组件。) | CCacheHttpSession |
| cookieMode | string | how to use cookie to store session ID. | CHttpSession |
| cookieParams | array | the session cookie parameters. | CHttpSession |
| count | integer | Returns the number of items in the session. | CHttpSession |
| gCProbability | integer | the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. | CHttpSession |
| isInitialized | boolean | 检查应用组件是否已经初始化。 | CApplicationComponent |
| isStarted | boolean | whether the session has started | CHttpSession |
| iterator | CHttpSessionIterator | Returns an iterator for traversing the session variables. | CHttpSession |
| keys | array | the list of session variable names | CHttpSession |
| savePath | string | the current session save path, defaults to '/tmp'. | CHttpSession |
| sessionID | string | the current session ID | CHttpSession |
| sessionName | string | the current session name | CHttpSession |
| timeout | integer | the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds. | CHttpSession |
| useCustomStorage | boolean | 返回一个值,指示是否使用自定义的会话存储。 | CCacheHttpSession |
| useTransparentSessionID | boolean | whether transparent sid support is enabled or not, defaults to false. | CHttpSession |
公共方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| __call() | 如果类中没有调的方法名,则调用这个方法。 | CComponent |
| __get() | 返回一个属性值、一个事件处理程序列表或一个行为名称。 | CComponent |
| __isset() | 检查一个属性是否为null。 | CComponent |
| __set() | 设置一个组件的属性值。 | CComponent |
| __unset() | 设置一个组件的属性为null。 | CComponent |
| add() | Adds a session variable. | CHttpSession |
| asa() | 返回这个名字的行为对象。 | CComponent |
| attachBehavior() | 附加一个行为到组件。 | CComponent |
| attachBehaviors() | 附加一个行为列表到组件。 | CComponent |
| attachEventHandler() | 为事件附加一个事件处理程序。 | CComponent |
| canGetProperty() | 确定属性是否可读。 | CComponent |
| canSetProperty() | 确定属性是否可写。 | CComponent |
| clear() | Removes all session variables | CHttpSession |
| close() | Ends the current session and store session data. | CHttpSession |
| closeSession() | Session close handler. | CHttpSession |
| contains() | CHttpSession | |
| count() | Returns the number of items in the session. | CHttpSession |
| destroy() | Frees all session variables and destroys all data registered to a session. | CHttpSession |
| destroySession() | 会话销毁处理程序。 | CCacheHttpSession |
| detachBehavior() | 从组件中分离一个行为。 | CComponent |
| detachBehaviors() | 从组件中分离所有行为。 | CComponent |
| detachEventHandler() | 分离一个存在的事件处理程序。 | CComponent |
| disableBehavior() | 禁用一个附加行为。 | CComponent |
| disableBehaviors() | 禁用组件附加的所有行为。 | CComponent |
| enableBehavior() | 启用一个附加行为。 | CComponent |
| enableBehaviors() | 启用组件附加的所有行为。 | CComponent |
| evaLuateExpression() | 计算一个PHP表达式,或根据组件上下文执行回调。 | CComponent |
| gcSession() | Session GC (garbage collection) handler. | CHttpSession |
| get() | Returns the session variable value with the session variable name. | CHttpSession |
| getCookieMode() | 返回how to use cookie to store session ID. Defaults to 'Allow'. | CHttpSession |
| getCookieParams() | 返回the session cookie parameters. | CHttpSession |
| getCount() | Returns the number of items in the session. | CHttpSession |
| getEventHandlers() | 返回一个事件的附加处理程序列表。 | CComponent |
| getGCProbability() | 返回the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. | CHttpSession |
| getIsInitialized() | 检查应用组件是否已经初始化。 | CApplicationComponent |
| getIsStarted() | 检查whether the session has started | CHttpSession |
| getIterator() | Returns an iterator for traversing the session variables. | CHttpSession |
| getKeys() | 返回the list of session variable names | CHttpSession |
| getSavePath() | 返回the current session save path, defaults to '/tmp'. | CHttpSession |
| getSessionID() | 返回the current session ID | CHttpSession |
| getSessionName() | 返回the current session name | CHttpSession |
| getTimeout() | 返回the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds. | CHttpSession |
| getUseCustomStorage() | 返回一个值,指示是否使用自定义的会话存储。 | CCacheHttpSession |
| getUseTransparentSessionID() | 返回whether transparent sid support is enabled or not, defaults to false. | CHttpSession |
| hasEvent() | 确定一个事件是否定义。 | CComponent |
| hasEventHandler() | 检查事件是否有附加的处理程序。 | CComponent |
| hasProperty() | 确定属性是否被定义。 | CComponent |
| init() | 初始化这个应用组件。 | CCacheHttpSession |
| itemAt() | Returns the session variable value with the session variable name. | CHttpSession |
| offsetExists() | This method is required by the interface ArrayAccess. | CHttpSession |
| offsetGet() | This method is required by the interface ArrayAccess. | CHttpSession |
| offsetSet() | This method is required by the interface ArrayAccess. | CHttpSession |
| offsetUnset() | This method is required by the interface ArrayAccess. | CHttpSession |
| open() | Starts the session if it has not started yet. | CHttpSession |
| openSession() | Session open handler. | CHttpSession |
| raiseEvent() | 发起一个事件。 | CComponent |
| readSession() | 会话读取处理程序。 | CCacheHttpSession |
| regenerateID() | Updates the current session id with a newly generated one . | CHttpSession |
| remove() | Removes a session variable. | CHttpSession |
| setCookieMode() | 设置how to use cookie to store session ID. Valid values include 'none', 'allow' and 'only'. | CHttpSession |
| setCookieParams() | Sets the session cookie parameters. | CHttpSession |
| setGCProbability() | 设置the probability (percentage) that the gc (garbage collection) process is started on every session initialization. | CHttpSession |
| setSavePath() | 设置the current session save path | CHttpSession |
| setSessionID() | 设置the session ID for the current session | CHttpSession |
| setSessionName() | 设置the session name for the current session, must be an alphanumeric string, defaults to PHPSESSID | CHttpSession |
| setTimeout() | 设置the number of seconds after which data will be seen as 'garbage' and cleaned up | CHttpSession |
| setUseTransparentSessionID() | 设置whether transparent sid support is enabled or not. | CHttpSession |
| toArray() | CHttpSession | |
| writeSession() | 会话写入处理程序。 | CCacheHttpSession |
受保护方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| calculateKey() | 为将要存储的会员数据生成一个唯一的键名。 | CCacheHttpSession |
属性详细
cacheID
属性
public string $cacheID;
缓存应用组件ID。默认是‘cache’(原始缓存应用组件。)
useCustomStorage
属性
只读
public boolean getUseCustomStorage()
返回一个值,指示是否使用自定义的会话存储。 这个方法是覆盖了父类方法,总是返回true。
方法详细
calculateKey()
方法
|
protected string calculateKey(string $id)
| ||
| $id | string | 会话变量名字 |
| {return} | string | 安全的缓存键值和会话变量名字 |
protected function calculateKey($id)
{
return self::CACHE_KEY_PREFIX.$id;
}
为将要存储的会员数据生成一个唯一的键名。
destroySession()
方法
|
public boolean destroySession(string $id)
| ||
| $id | string | 会话ID |
| {return} | boolean | 会话是否被成功销毁 |
public function destroySession($id)
{
return $this->_cache->delete($this->calculateKey($id));
}
会话销毁处理程序。 不要直接调用这个方法。
getUseCustomStorage()
方法
|
public boolean getUseCustomStorage()
| ||
| {return} | boolean | 是否使用常规存储。 |
public function getUseCustomStorage()
{
return true;
}
返回一个值,指示是否使用自定义的会话存储。 这个方法是覆盖了父类方法,总是返回true。
init()
方法
|
public void init()
|
public function init()
{
$this->_cache=Yii::app()->getComponent($this->cacheID);
if(!($this->_cache instanceof ICache))
throw new CException(Yii::t('yii','CCacheHttpSession.cacheID is invalid. Please make sure "{id}" refers to a valid cache application component.',
array('{id}'=>$this->cacheID)));
parent::init();
}
初始化这个应用组件。 这个方法覆盖了父类方法,主要是检查缓存是否可用。
readSession()
方法
|
public string readSession(string $id)
| ||
| $id | string | 会话ID |
| {return} | string | the 会话数据 |
public function readSession($id)
{
$data=$this->_cache->get($this->calculateKey($id));
return $data===false?'':$data;
}
会话读取处理程序。 不要直接调用这个方法。
writeSession()
方法
|
public boolean writeSession(string $id, string $data)
| ||
| $id | string | 会话ID |
| $data | string | 会话数据 |
| {return} | boolean | 是否成功写入了会话数据 |
public function writeSession($id,$data)
{
return $this->_cache->set($this->calculateKey($id),$data,$this->getTimeout());
}
会话写入处理程序。 不要直接调用这个方法。