1: <?php
2: /**
3: * CCacheHttpSession class
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:
12: /**
13: * CCacheHttpSession implements a session component using cache as storage medium.
14: *
15: * The cache being used can be any cache application component implementing {@link ICache} interface.
16: * The ID of the cache application component is specified via {@link cacheID}, which defaults to 'cache'.
17: *
18: * Beware, by definition cache storage are volatile, which means the data stored on them
19: * may be swapped out and get lost. Therefore, you must make sure the cache used by this component
20: * is NOT volatile. If you want to use {@link CDbCache} as storage medium, use {@link CDbHttpSession}
21: * is a better choice.
22: *
23: * @property boolean $useCustomStorage Whether to use custom storage.
24: *
25: * @author Qiang Xue <qiang.xue@gmail.com>
26: * @package system.web
27: * @since 1.0
28: */
29: class CCacheHttpSession extends CHttpSession
30: {
31: /**
32: * Prefix to the keys for storing cached data
33: */
34: const CACHE_KEY_PREFIX='Yii.CCacheHttpSession.';
35: /**
36: * @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
37: */
38: public $cacheID='cache';
39:
40: /**
41: * @var ICache the cache component
42: */
43: private $_cache;
44:
45: /**
46: * Initializes the application component.
47: * This method overrides the parent implementation by checking if cache is available.
48: */
49: public function init()
50: {
51: $this->_cache=Yii::app()->getComponent($this->cacheID);
52: if(!($this->_cache instanceof ICache))
53: throw new CException(Yii::t('yii','CCacheHttpSession.cacheID is invalid. Please make sure "{id}" refers to a valid cache application component.',
54: array('{id}'=>$this->cacheID)));
55: parent::init();
56: }
57:
58: /**
59: * Returns a value indicating whether to use custom session storage.
60: * This method overrides the parent implementation and always returns true.
61: * @return boolean whether to use custom storage.
62: */
63: public function getUseCustomStorage()
64: {
65: return true;
66: }
67:
68: /**
69: * Session read handler.
70: * Do not call this method directly.
71: * @param string $id session ID
72: * @return string the session data
73: */
74: public function readSession($id)
75: {
76: $data=$this->_cache->get($this->calculateKey($id));
77: return $data===false?'':$data;
78: }
79:
80: /**
81: * Session write handler.
82: * Do not call this method directly.
83: * @param string $id session ID
84: * @param string $data session data
85: * @return boolean whether session write is successful
86: */
87: public function writeSession($id,$data)
88: {
89: return $this->_cache->set($this->calculateKey($id),$data,$this->getTimeout());
90: }
91:
92: /**
93: * Session destroy handler.
94: * Do not call this method directly.
95: * @param string $id session ID
96: * @return boolean whether session is destroyed successfully
97: */
98: public function destroySession($id)
99: {
100: return $this->_cache->delete($this->calculateKey($id));
101: }
102:
103: /**
104: * Generates a unique key used for storing session data in cache.
105: * @param string $id session variable name
106: * @return string a safe cache key associated with the session variable name
107: */
108: protected function calculateKey($id)
109: {
110: return self::CACHE_KEY_PREFIX.$id;
111: }
112: }
113: