CacheSession.php 3.29 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

Qiang Xue committed
10 11 12 13
use Yii;
use yii\caching\Cache;
use yii\base\InvalidConfigException;

Qiang Xue committed
14 15 16
/**
 * CacheSession implements a session component using cache as storage medium.
 *
Qiang Xue committed
17
 * The cache being used can be any cache application component.
18
 * The ID of the cache application component is specified via [[cache]], which defaults to 'cache'.
Qiang Xue committed
19 20 21
 *
 * 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
22 23 24 25 26 27 28 29 30 31 32
 * is NOT volatile. If you want to use database as storage medium, [[DbSession]] is a better choice.
 *
 * The following example shows how you can configure the application to use CacheSession:
 * Add the following to your application config under `components`:
 *
 * ~~~
 * 'session' => [
 *     'class' => 'yii\web\CacheSession',
 *     // 'cache' => 'mycache',
 * ]
 * ~~~
Qiang Xue committed
33
 *
34
 * @property boolean $useCustomStorage Whether to use custom storage. This property is read-only.
35
 *
Qiang Xue committed
36
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
37
 * @since 2.0
Qiang Xue committed
38 39 40 41
 */
class CacheSession extends Session
{
	/**
42 43 44 45 46
	 * @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.
Qiang Xue committed
47
	 */
48
	public $cache = 'cache';
Qiang Xue committed
49 50

	/**
51
	 * Initializes the application component.
Qiang Xue committed
52
	 */
53 54 55 56 57 58 59 60
	public function 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.');
		}
61
		parent::init();
62
	}
Qiang Xue committed
63 64

	/**
Qiang Xue committed
65 66 67 68 69 70 71 72
	 * 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;
	}
Qiang Xue committed
73

Qiang Xue committed
74 75 76 77 78 79 80 81
	/**
	 * Session read handler.
	 * Do not call this method directly.
	 * @param string $id session ID
	 * @return string the session data
	 */
	public function readSession($id)
	{
82
		$data = $this->cache->get($this->calculateKey($id));
Qiang Xue committed
83 84 85 86 87 88 89 90 91 92 93 94
		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)
	{
95
		return $this->cache->set($this->calculateKey($id), $data, $this->getTimeout());
Qiang Xue committed
96 97 98 99 100 101 102 103 104 105
	}

	/**
	 * 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)
	{
106
		return $this->cache->delete($this->calculateKey($id));
Qiang Xue committed
107 108 109 110 111
	}

	/**
	 * Generates a unique key used for storing session data in cache.
	 * @param string $id session variable name
112
	 * @return mixed a safe cache key associated with the session variable name
Qiang Xue committed
113 114 115
	 */
	protected function calculateKey($id)
	{
Alexander Makarov committed
116
		return [__CLASS__, $id];
Qiang Xue committed
117 118
	}
}