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