1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TCacheHttpSession class |
4
|
|
|
* |
5
|
|
|
* @author Carl G. Mathisen <[email protected]> |
6
|
|
|
* @author Qiang Xue <[email protected]> |
7
|
|
|
* @link https://github.com/pradosoft/prado |
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
9
|
|
|
* @package Prado\Web |
10
|
|
|
* @since 3.1.1 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Prado\Web; |
14
|
|
|
|
15
|
|
|
use Prado\Caching\ICache; |
16
|
|
|
use Prado\Exceptions\TConfigurationException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* TCacheHttpSession class |
20
|
|
|
* |
21
|
|
|
* TCacheHttpSession provides access for storing session data using a cache module (e.g. TMemCache, TDbCache). |
22
|
|
|
* To specify the cache module for data storage, set the {@link setCacheModuleID CacheModuleID} property |
23
|
|
|
* which should refer to a valid cache module configured in the application configuration. |
24
|
|
|
* |
25
|
|
|
* The following example shows how we configure TCacheHttpSession: |
26
|
|
|
* <code> |
27
|
|
|
* <modules> |
28
|
|
|
* <module id="cache" class="System.Caching.TMemCache" Host="localhost" Port="11211" /> |
29
|
|
|
* <module id="session" class="System.Web.TCacheHttpSession" |
30
|
|
|
* CacheModuleID="cache" SessionName="SSID" |
31
|
|
|
* CookieMode="Allow" AutoStart="true" GCProbability="1" |
32
|
|
|
* UseTransparentSessionID="true" TimeOut="3600" /> |
33
|
|
|
* </modules> |
34
|
|
|
* </code> |
35
|
|
|
* |
36
|
|
|
* Beware, by definition cache storage are volatile, which means the data stored on them |
37
|
|
|
* may be swapped out and get lost. This may not be the case for certain cache storage, |
38
|
|
|
* such as database. So make sure you manage your cache properly to avoid loss of session data. |
39
|
|
|
* |
40
|
|
|
* @author Carl G. Mathisen <[email protected]> |
41
|
|
|
* @author Qiang Xue <[email protected]> |
42
|
|
|
* @package Prado\Web |
43
|
|
|
* @since 3.1.1 |
44
|
|
|
*/ |
45
|
|
|
class TCacheHttpSession extends THttpSession |
46
|
|
|
{ |
47
|
|
|
private $_prefix = 'session'; |
48
|
|
|
private $_cacheModuleID = ''; |
49
|
|
|
private $_cache; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initializes the module. |
53
|
|
|
* This method is required by IModule. |
54
|
|
|
* It reads the CacheModule property. |
55
|
|
|
* @param TXmlElement $config module configuration |
56
|
|
|
*/ |
57
|
10 |
|
public function init($config) |
58
|
|
|
{ |
59
|
10 |
|
if ($this->_cacheModuleID === '') { |
60
|
1 |
|
throw new TConfigurationException('cachesession_cachemoduleid_required'); |
61
|
10 |
|
} elseif (($cache = $this->getApplication()->getModule($this->_cacheModuleID)) === null) { |
62
|
1 |
|
throw new TConfigurationException('cachesession_cachemodule_inexistent', $this->_cacheModuleID); |
63
|
10 |
|
} elseif ($cache instanceof ICache) { |
64
|
10 |
|
$this->_cache = $cache; |
65
|
|
|
} else { |
66
|
|
|
throw new TConfigurationException('cachesession_cachemodule_invalid', $this->_cacheModuleID); |
67
|
|
|
} |
68
|
10 |
|
$this->setUseCustomStorage(true); |
69
|
10 |
|
parent::init($config); |
70
|
10 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string the ID of the cache module. |
74
|
|
|
*/ |
75
|
1 |
|
public function getCacheModuleID() |
76
|
|
|
{ |
77
|
1 |
|
return $this->_cacheModuleID; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $value the ID of the cache module. |
82
|
|
|
*/ |
83
|
10 |
|
public function setCacheModuleID($value) |
84
|
|
|
{ |
85
|
10 |
|
$this->_cacheModuleID = $value; |
86
|
10 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return ICache the cache module being used for data storage |
90
|
|
|
*/ |
91
|
1 |
|
public function getCache() |
92
|
|
|
{ |
93
|
1 |
|
return $this->_cache; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Session read handler. |
98
|
|
|
* @param string $id session ID |
99
|
|
|
* @return string the session data |
100
|
|
|
*/ |
101
|
|
|
public function _read($id) |
102
|
|
|
{ |
103
|
|
|
return (string) $this->_cache->get($this->calculateKey($id)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Session write handler. |
108
|
|
|
* @param string $id session ID |
109
|
|
|
* @param string $data session data |
110
|
|
|
* @return bool whether session write is successful |
111
|
|
|
*/ |
112
|
|
|
public function _write($id, $data) |
113
|
|
|
{ |
114
|
|
|
return $this->_cache->set($this->calculateKey($id), $data, $this->getTimeout()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Session destroy handler. |
119
|
|
|
* This method should be overriden if {@link setUseCustomStorage UseCustomStorage} is set true. |
120
|
|
|
* @param string $id session ID |
121
|
|
|
* @return bool whether session is destroyed successfully |
122
|
|
|
*/ |
123
|
|
|
public function _destroy($id) |
124
|
|
|
{ |
125
|
|
|
return $this->_cache->delete($this->calculateKey($id)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string prefix of session variable name to avoid conflict with other cache data. Defaults to 'session'. |
130
|
|
|
*/ |
131
|
1 |
|
public function getKeyPrefix() |
132
|
|
|
{ |
133
|
1 |
|
return $this->_prefix; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $value prefix of session variable name to avoid conflict with other cache data |
138
|
|
|
*/ |
139
|
1 |
|
public function setKeyPrefix($value) |
140
|
|
|
{ |
141
|
1 |
|
$this->_prefix = $value; |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $id session variable name |
146
|
|
|
* @return string a safe cache key associated with the session variable name |
147
|
|
|
*/ |
148
|
|
|
protected function calculateKey($id) |
149
|
|
|
{ |
150
|
|
|
return $this->_prefix . ':' . $id; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|