You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.0 KiB
107 lines
3.0 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
12 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\web;
|
||
|
|
||
12 years ago
|
use Yii;
|
||
|
use yii\caching\Cache;
|
||
|
use yii\base\InvalidConfigException;
|
||
|
|
||
12 years ago
|
/**
|
||
|
* CacheSession implements a session component using cache as storage medium.
|
||
|
*
|
||
12 years ago
|
* The cache being used can be any cache application component.
|
||
12 years ago
|
* The ID of the cache application component is specified via [[cache]], which defaults to 'cache'.
|
||
12 years ago
|
*
|
||
|
* Beware, by definition cache storage are volatile, which means the data stored on them
|
||
|
* may be swapped out and get lost. Therefore, you must make sure the cache used by this component
|
||
12 years ago
|
* is NOT volatile. If you want to use database as storage medium, use [[DbSession]] is a better choice.
|
||
12 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
12 years ago
|
* @since 2.0
|
||
12 years ago
|
*/
|
||
|
class CacheSession extends Session
|
||
|
{
|
||
|
/**
|
||
12 years ago
|
* @var Cache|string the cache object or the application component ID of the cache object.
|
||
|
* The session data will be stored using this cache object.
|
||
|
*
|
||
|
* After the CacheSession object is created, if you want to change this property,
|
||
|
* you should only assign it with a cache object.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public $cache = 'cache';
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* Initializes the application component.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
|
if (is_string($this->cache)) {
|
||
|
$this->cache = Yii::$app->getComponent($this->cache);
|
||
|
}
|
||
|
if (!$this->cache instanceof Cache) {
|
||
|
throw new InvalidConfigException('CacheSession::cache must refer to the application component ID of a cache object.');
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* Returns a value indicating whether to use custom session storage.
|
||
|
* This method overrides the parent implementation and always returns true.
|
||
|
* @return boolean whether to use custom storage.
|
||
|
*/
|
||
|
public function getUseCustomStorage()
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Session read handler.
|
||
|
* Do not call this method directly.
|
||
|
* @param string $id session ID
|
||
|
* @return string the session data
|
||
|
*/
|
||
|
public function readSession($id)
|
||
|
{
|
||
12 years ago
|
$data = $this->cache->get($this->calculateKey($id));
|
||
12 years ago
|
return $data === false ? '' : $data;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Session write handler.
|
||
|
* Do not call this method directly.
|
||
|
* @param string $id session ID
|
||
|
* @param string $data session data
|
||
|
* @return boolean whether session write is successful
|
||
|
*/
|
||
|
public function writeSession($id, $data)
|
||
|
{
|
||
12 years ago
|
return $this->cache->set($this->calculateKey($id), $data, $this->getTimeout());
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Session destroy handler.
|
||
|
* Do not call this method directly.
|
||
|
* @param string $id session ID
|
||
|
* @return boolean whether session is destroyed successfully
|
||
|
*/
|
||
|
public function destroySession($id)
|
||
|
{
|
||
12 years ago
|
return $this->cache->delete($this->calculateKey($id));
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Generates a unique key used for storing session data in cache.
|
||
|
* @param string $id session variable name
|
||
|
* @return string a safe cache key associated with the session variable name
|
||
|
*/
|
||
|
protected function calculateKey($id)
|
||
|
{
|
||
12 years ago
|
return $this->cache->buildKey(array(__CLASS__, $id));
|
||
12 years ago
|
}
|
||
|
}
|