├── .gitignore ├── README.md ├── application └── libraries │ └── MY_Session.php ├── license.txt └── system └── libraries └── Cache ├── Cache.php └── drivers ├── Cache_apc.php ├── Cache_dummy.php ├── Cache_file.php ├── Cache_memcache.php └── Cache_memcached.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Numerous always-ignore extensions 2 | *.diff 3 | *.err 4 | *.orig 5 | *.log 6 | *.rej 7 | *.swo 8 | *.swp 9 | *.vi 10 | *~ 11 | *.sass-cache 12 | 13 | # OS or Editor folders 14 | .DS_Store 15 | Thumbs.db 16 | .cache 17 | .project 18 | .settings 19 | .tmproj 20 | *.esproj 21 | nbproject 22 | *.sublime-project 23 | *.sublime-workspace 24 | 25 | # Dreamweaver added files 26 | _notes 27 | dwsync.xml 28 | 29 | # Komodo 30 | *.komodoproject 31 | .komodotools 32 | 33 | # Folders to ignore 34 | .hg 35 | .svn 36 | .CVS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [查看简体中文版README](https://github.com/cnsaturn/codeigniter-my-session/wiki) 2 | 3 | ## Intro 4 | 5 | Another CodeIgniter session library that utilizes the Native PHP session mechanism instead of the original CI cookie-based OR DB-based way. It takes Memcached as the session datastore, which will boost the performance of your app. Also, it supports all the [original features](http://codeigniter.com/user_guide/libraries/sessions.html), such as flash session. 6 | 7 | ## Author 8 | 9 | * [HU Yang Gang](https://github.com/cnsaturn), aka. Saturn 10 | 11 | ## Requirement 12 | 13 | * PHP 5.2.0 + 14 | * CodeIgniter 2.0 + 15 | * [libmemcached php extension](http://pecl.php.net/package/memcached) 16 | 17 | ## HOW TO USE? 18 | 19 | 1. Drop libraries/MY_Session.php into your application/libraries folder 20 | 2. Drop system/libraries/Cache folder into your system/libraries folder, overwrite the existing files if needed when prompted. 21 | 3. Load the Session library manually OR you can autoload it in config/autoload.php file. 22 | 4. Use it like you are using the original [CI Session library](http://codeigniter.com/user_guide/libraries/sessions.html). 23 | 5. That's all, enjoy! 24 | 25 | ## License 26 | 27 | The MIT license 28 | 29 | 30 | -------------------------------------------------------------------------------- /application/libraries/MY_Session.php: -------------------------------------------------------------------------------- 1 | 15 | * @license The MIT license 16 | * @link https://github.com/cnsaturn/codeigniter-my-session 17 | */ 18 | 19 | // Set session configs... 20 | new Cache_Session_Handler; 21 | session_start(); 22 | 23 | Class MY_Session Extends CI_Session 24 | { 25 | /** 26 | * Session Constructor 27 | * 28 | * The constructor runs the session routines automatically 29 | * whenever the class is instantiated. 30 | */ 31 | public function __construct($params = array()) 32 | { 33 | parent::__construct($params); 34 | } 35 | 36 | // -------------------------------------------------------------------- 37 | 38 | /** 39 | * Fetch the current session data if it exists 40 | * 41 | * @access public 42 | * @return bool 43 | */ 44 | public function sess_read() 45 | { 46 | // Fetch the cookie 47 | $session = $_SESSION; 48 | 49 | // Is the session data we unserialized an array with the correct format? 50 | if ( ! is_array($session) OR ! isset($session['session_id']) OR ! isset($session['ip_address']) OR ! isset($session['user_agent']) OR ! isset($session['last_activity'])) 51 | { 52 | log_message('debug', 'A session was not found.'); 53 | $this->sess_destroy(FALSE); 54 | return FALSE; 55 | } 56 | 57 | // Is the session current? 58 | if (($session['last_activity'] + $this->sess_expiration) < $this->now) 59 | { 60 | $this->sess_destroy(FALSE); 61 | return FALSE; 62 | } 63 | 64 | // Does the IP Match? 65 | if ($this->sess_match_ip == TRUE AND $session['ip_address'] != $this->CI->input->ip_address()) 66 | { 67 | $this->sess_destroy(FALSE); 68 | return FALSE; 69 | } 70 | 71 | // Does the User Agent Match? 72 | if ($this->sess_match_useragent == TRUE AND trim($session['user_agent']) != trim(substr($this->CI->input->user_agent(), 0, 120))) 73 | { 74 | $this->sess_destroy(FALSE); 75 | return FALSE; 76 | } 77 | 78 | // Session is valid! 79 | $this->userdata = $session; 80 | unset($session); 81 | 82 | return TRUE; 83 | } 84 | 85 | // -------------------------------------------------------------------- 86 | 87 | /** 88 | * Write the session data 89 | * 90 | * @access public 91 | * @return void 92 | */ 93 | public function sess_write() 94 | { 95 | // set the custom userdata, the session data we will set in a second 96 | $_SESSION = array(); 97 | foreach($this->userdata as $key => $val) 98 | { 99 | $_SESSION[$key] = $val; 100 | } 101 | } 102 | 103 | // -------------------------------------------------------------------- 104 | 105 | /** 106 | * Create a new session 107 | * 108 | * @access public 109 | * @return void 110 | */ 111 | public function sess_create() 112 | { 113 | if(session_id() == '') 114 | { 115 | session_start(); 116 | } 117 | 118 | $_SESSION['session_id'] = session_id(); 119 | $_SESSION['ip_address'] = $this->CI->input->ip_address(); 120 | $_SESSION['user_agent'] = substr($this->CI->input->user_agent(), 0, 120); 121 | $_SESSION['last_activity'] = $this->now; 122 | 123 | $this->userdata = $_SESSION; 124 | } 125 | 126 | // -------------------------------------------------------------------- 127 | 128 | /** 129 | * Update an existing session 130 | * 131 | * @access public 132 | * @return void 133 | */ 134 | public function sess_update() 135 | { 136 | // We only update the session every five minutes by default 137 | if(($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) 138 | { 139 | return; 140 | } 141 | 142 | // Regenerate session id 143 | session_regenerate_id(); 144 | 145 | // Update the session data in the session data array 146 | $this->userdata['session_id'] = session_id(); 147 | $this->userdata['last_activity'] = $this->now; 148 | } 149 | 150 | // -------------------------------------------------------------------- 151 | 152 | /** 153 | * Destroy the current session 154 | * 155 | * @access public 156 | * @return void 157 | */ 158 | public function sess_destroy($destroy = TRUE) 159 | { 160 | session_unset(); 161 | session_regenerate_id(); 162 | 163 | if($destroy) 164 | { 165 | session_destroy(); 166 | } 167 | } 168 | 169 | // -------------------------------------------------------------------- 170 | 171 | /** 172 | * Does nothing for native sessions 173 | * 174 | * @access public 175 | * @return void 176 | */ 177 | public function _sess_gc(){} 178 | } 179 | 180 | // Customize the PHP Session handler 181 | // @see http://php.net/manual/en/function.session-set-save-handler.php 182 | class Cache_Session_Handler 183 | { 184 | private $_lifetime = 0; 185 | private $_CI; 186 | 187 | public function __construct() 188 | { 189 | session_name(config_item('sess_cookie_name')); 190 | 191 | session_set_save_handler( 192 | array($this, "open"), 193 | array($this, "close"), 194 | array($this, "read"), 195 | array($this, "write"), 196 | array($this, "destroy"), 197 | array($this, "gc") 198 | ); 199 | 200 | // Ref the 'CI' super global object 201 | $this->_CI = & get_instance(); 202 | 203 | if( ! $this->_CI->load->is_loaded('cache')) 204 | { 205 | // Make sure your 2.0+ CI Cache driver library is loaded 206 | // and set as the primary cache solution 207 | $this->_CI->load->driver( 208 | 'cache', 209 | array('adapter' => 'memcached', 'backup' => 'file' 210 | )); 211 | } 212 | } 213 | 214 | public function open() 215 | { 216 | $this->_lifetime = ini_get('session.gc_maxlifetime'); 217 | if($this->_lifetime <= config_item('sess_expiration')) 218 | { 219 | $this->_lifetime = config_item('sess_expiration'); 220 | } 221 | 222 | return TRUE; 223 | } 224 | 225 | public function read($id) 226 | { 227 | return $this->_CI->cache->get("sess_{$id}"); 228 | } 229 | 230 | public function write($id, $data) 231 | { 232 | return $this->_CI->cache->replace("sess_{$id}", $data, $this->_lifetime); 233 | } 234 | 235 | public function destroy($id) 236 | { 237 | return $this->_CI->cache->delete("sess_{$id}"); 238 | } 239 | 240 | public function gc() 241 | { 242 | return TRUE; 243 | } 244 | 245 | public function close() 246 | { 247 | return TRUE; 248 | } 249 | 250 | public function __destruct() 251 | { 252 | session_write_close(); 253 | } 254 | } -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2012 Situos Inc., http://www.situos.com 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the “Software”), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. -------------------------------------------------------------------------------- /system/libraries/Cache/Cache.php: -------------------------------------------------------------------------------- 1 | _initialize($config); 49 | } 50 | } 51 | 52 | // ------------------------------------------------------------------------ 53 | 54 | /** 55 | * Get 56 | * 57 | * Look for a value in the cache. If it exists, return the data 58 | * if not, return FALSE 59 | * 60 | * @param string 61 | * @return mixed value that is stored/FALSE on failure 62 | */ 63 | public function get($id) 64 | { 65 | return $this->{$this->_adapter}->get($id); 66 | } 67 | 68 | // ------------------------------------------------------------------------ 69 | 70 | /** 71 | * Cache Save 72 | * 73 | * @param string Unique Key 74 | * @param mixed Data to store 75 | * @param int Length of time (in seconds) to cache the data 76 | * 77 | * @return boolean true on success/false on failure 78 | */ 79 | public function save($id, $data, $ttl = 60) 80 | { 81 | return $this->{$this->_adapter}->save($id, $data, $ttl); 82 | } 83 | 84 | // ------------------------------------------------------------------------ 85 | 86 | /** 87 | * Cache Replace 88 | * 89 | * @param string Unique Key 90 | * @param mixed Data to store 91 | * @param int Length of time (in seconds) to cache the data 92 | * 93 | * @return boolean true on success/false on failure 94 | */ 95 | public function replace($id, $data, $ttl = 60) 96 | { 97 | return $this->{$this->_adapter}->replace($id, $data, $ttl); 98 | } 99 | 100 | // ------------------------------------------------------------------------ 101 | 102 | /** 103 | * Delete from Cache 104 | * 105 | * @param mixed unique identifier of the item in the cache 106 | * @return boolean true on success/false on failure 107 | */ 108 | public function delete($id) 109 | { 110 | return $this->{$this->_adapter}->delete($id); 111 | } 112 | 113 | // ------------------------------------------------------------------------ 114 | 115 | /** 116 | * Clean the cache 117 | * 118 | * @return boolean false on failure/true on success 119 | */ 120 | public function clean() 121 | { 122 | return $this->{$this->_adapter}->clean(); 123 | } 124 | 125 | // ------------------------------------------------------------------------ 126 | 127 | /** 128 | * Cache Info 129 | * 130 | * @param string user/filehits 131 | * @return mixed array on success, false on failure 132 | */ 133 | public function cache_info($type = 'user') 134 | { 135 | return $this->{$this->_adapter}->cache_info($type); 136 | } 137 | 138 | // ------------------------------------------------------------------------ 139 | 140 | /** 141 | * Get Cache Metadata 142 | * 143 | * @param mixed key to get cache metadata on 144 | * @return mixed return value from child method 145 | */ 146 | public function get_metadata($id) 147 | { 148 | return $this->{$this->_adapter}->get_metadata($id); 149 | } 150 | 151 | // ------------------------------------------------------------------------ 152 | 153 | /** 154 | * Initialize 155 | * 156 | * Initialize class properties based on the configuration array. 157 | * 158 | * @param array 159 | * @return void 160 | */ 161 | private function _initialize($config) 162 | { 163 | $default_config = array( 164 | 'adapter', 165 | 'memcached' 166 | ); 167 | 168 | foreach ($default_config as $key) 169 | { 170 | if (isset($config[$key])) 171 | { 172 | $param = '_'.$key; 173 | 174 | $this->{$param} = $config[$key]; 175 | } 176 | } 177 | 178 | if (isset($config['backup'])) 179 | { 180 | if (in_array('cache_'.$config['backup'], $this->valid_drivers)) 181 | { 182 | $this->_backup_driver = $config['backup']; 183 | } 184 | } 185 | 186 | if( ! $this->{$this->_adapter}->is_supported()) 187 | { 188 | $this->_adapter = $this->_backup_driver; 189 | } 190 | } 191 | 192 | // ------------------------------------------------------------------------ 193 | 194 | /** 195 | * Is the requested driver supported in this environment? 196 | * 197 | * @param string The driver to test. 198 | * @return array 199 | */ 200 | public function is_supported($driver) 201 | { 202 | static $support = array(); 203 | 204 | if ( ! isset($support[$driver])) 205 | { 206 | $support[$driver] = $this->{$driver}->is_supported(); 207 | } 208 | 209 | return $support[$driver]; 210 | } 211 | 212 | // ------------------------------------------------------------------------ 213 | 214 | /** 215 | * __get() 216 | * 217 | * @param child 218 | * @return object 219 | */ 220 | public function __get($child) 221 | { 222 | $obj = parent::__get($child); 223 | 224 | if ( ! $this->is_supported($child)) 225 | { 226 | $this->_adapter = $this->_backup_driver; 227 | } 228 | 229 | return $obj; 230 | } 231 | 232 | // ------------------------------------------------------------------------ 233 | } 234 | // End Class 235 | 236 | /* End of file Cache.php */ 237 | /* Location: ./system/libraries/Cache/Cache.php */ -------------------------------------------------------------------------------- /system/libraries/Cache/drivers/Cache_apc.php: -------------------------------------------------------------------------------- 1 | save($id, $data, $ttl); 89 | } 90 | 91 | // ------------------------------------------------------------------------ 92 | 93 | /** 94 | * Clean the cache 95 | * 96 | * @return boolean false on failure/true on success 97 | */ 98 | public function clean() 99 | { 100 | return apc_clear_cache('user'); 101 | } 102 | 103 | // ------------------------------------------------------------------------ 104 | 105 | /** 106 | * Cache Info 107 | * 108 | * @param string user/filehits 109 | * @return mixed array on success, false on failure 110 | */ 111 | public function cache_info($type = NULL) 112 | { 113 | return apc_cache_info($type); 114 | } 115 | 116 | // ------------------------------------------------------------------------ 117 | 118 | /** 119 | * Get Cache Metadata 120 | * 121 | * @param mixed key to get cache metadata on 122 | * @return mixed array on success/false on failure 123 | */ 124 | public function get_metadata($id) 125 | { 126 | $stored = apc_fetch($id); 127 | 128 | if (count($stored) !== 3) 129 | { 130 | return FALSE; 131 | } 132 | 133 | list($data, $time, $ttl) = $stored; 134 | 135 | return array( 136 | 'expire' => $time + $ttl, 137 | 'mtime' => $time, 138 | 'data' => $data 139 | ); 140 | } 141 | 142 | // ------------------------------------------------------------------------ 143 | 144 | /** 145 | * is_supported() 146 | * 147 | * Check to see if APC is available on this system, bail if it isn't. 148 | */ 149 | public function is_supported() 150 | { 151 | if ( ! extension_loaded('apc') OR ini_get('apc.enabled') != "1") 152 | { 153 | log_message('error', 'The APC PHP extension must be loaded to use APC Cache.'); 154 | return FALSE; 155 | } 156 | 157 | return TRUE; 158 | } 159 | 160 | // ------------------------------------------------------------------------ 161 | } 162 | // End Class 163 | 164 | /* End of file Cache_apc.php */ 165 | /* Location: ./system/libraries/Cache/drivers/Cache_apc.php */ 166 | -------------------------------------------------------------------------------- /system/libraries/Cache/drivers/Cache_dummy.php: -------------------------------------------------------------------------------- 1 | load->helper('file'); 39 | 40 | $path = $CI->config->item('cache_path'); 41 | 42 | $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path; 43 | } 44 | 45 | // ------------------------------------------------------------------------ 46 | 47 | /** 48 | * Fetch from cache 49 | * 50 | * @param mixed unique key id 51 | * @return mixed data on success/false on failure 52 | */ 53 | public function get($id) 54 | { 55 | if ( ! file_exists($this->_cache_path.$id)) 56 | { 57 | return FALSE; 58 | } 59 | 60 | $data = read_file($this->_cache_path.$id); 61 | $data = unserialize($data); 62 | 63 | if (time() > $data['time'] + $data['ttl']) 64 | { 65 | @unlink($this->_cache_path.$id); 66 | return FALSE; 67 | } 68 | 69 | return $data['data']; 70 | } 71 | 72 | // ------------------------------------------------------------------------ 73 | 74 | /** 75 | * Save into cache 76 | * 77 | * @param string unique key 78 | * @param mixed data to store 79 | * @param int length of time (in seconds) the cache is valid 80 | * - Default is 60 seconds 81 | * @return boolean true on success/false on failure 82 | */ 83 | public function save($id, $data, $ttl = 60) 84 | { 85 | $contents = array( 86 | 'time' => time(), 87 | 'ttl' => $ttl, 88 | 'data' => $data 89 | ); 90 | 91 | if (write_file($this->_cache_path.$id, serialize($contents))) 92 | { 93 | @chmod($this->_cache_path.$id, 0777); 94 | return TRUE; 95 | } 96 | 97 | return FALSE; 98 | } 99 | 100 | // ------------------------------------------------------------------------ 101 | 102 | /** 103 | * Replace the cache 104 | * 105 | * @param string unique key 106 | * @param mixed data to store 107 | * @param int length of time (in seconds) the cache is valid 108 | * - Default is 60 seconds 109 | * @return boolean true on success/false on failure 110 | */ 111 | public function replace($id, $data, $ttl = 60) 112 | { 113 | return $this->save($id, $data, $ttl); 114 | } 115 | 116 | // ------------------------------------------------------------------------ 117 | 118 | /** 119 | * Delete from Cache 120 | * 121 | * @param mixed unique identifier of item in cache 122 | * @return boolean true on success/false on failure 123 | */ 124 | public function delete($id) 125 | { 126 | return @unlink($this->_cache_path.$id); 127 | } 128 | 129 | // ------------------------------------------------------------------------ 130 | 131 | /** 132 | * Clean the Cache 133 | * 134 | * @return boolean false on failure/true on success 135 | */ 136 | public function clean() 137 | { 138 | return delete_files($this->_cache_path); 139 | } 140 | 141 | // ------------------------------------------------------------------------ 142 | 143 | /** 144 | * Cache Info 145 | * 146 | * Not supported by file-based caching 147 | * 148 | * @param string user/filehits 149 | * @return mixed FALSE 150 | */ 151 | public function cache_info($type = NULL) 152 | { 153 | return get_dir_file_info($this->_cache_path); 154 | } 155 | 156 | // ------------------------------------------------------------------------ 157 | 158 | /** 159 | * Get Cache Metadata 160 | * 161 | * @param mixed key to get cache metadata on 162 | * @return mixed FALSE on failure, array on success. 163 | */ 164 | public function get_metadata($id) 165 | { 166 | if ( ! file_exists($this->_cache_path.$id)) 167 | { 168 | return FALSE; 169 | } 170 | 171 | $data = read_file($this->_cache_path.$id); 172 | $data = unserialize($data); 173 | 174 | if (is_array($data)) 175 | { 176 | $data = $data['data']; 177 | $mtime = filemtime($this->_cache_path.$id); 178 | 179 | if ( ! isset($data['ttl'])) 180 | { 181 | return FALSE; 182 | } 183 | 184 | return array( 185 | 'expire' => $mtime + $data['ttl'], 186 | 'mtime' => $mtime 187 | ); 188 | } 189 | 190 | return FALSE; 191 | } 192 | 193 | // ------------------------------------------------------------------------ 194 | 195 | /** 196 | * Is supported 197 | * 198 | * In the file driver, check to see that the cache directory is indeed writable 199 | * 200 | * @return boolean 201 | */ 202 | public function is_supported() 203 | { 204 | return is_really_writable($this->_cache_path); 205 | } 206 | 207 | // ------------------------------------------------------------------------ 208 | } 209 | // End Class 210 | 211 | /* End of file Cache_file.php */ 212 | /* Location: ./system/libraries/Cache/drivers/Cache_file.php */ -------------------------------------------------------------------------------- /system/libraries/Cache/drivers/Cache_memcache.php: -------------------------------------------------------------------------------- 1 | 31 | * @link 32 | */ 33 | 34 | class CI_Cache_memcache extends CI_Driver { 35 | 36 | private $_memcache; // Holds the memcached object 37 | 38 | protected $_memcache_conf = array( 39 | 'default' => array( 40 | 'default_host' => '127.0.0.1', 41 | 'default_port' => 11211, 42 | 'default_weight' => 1 43 | ) 44 | ); 45 | 46 | // ------------------------------------------------------------------------ 47 | 48 | /** 49 | * Fetch from cache 50 | * 51 | * @param mixed unique key id 52 | * @return mixed data on success/false on failure 53 | */ 54 | public function get($id) 55 | { 56 | $data = $this->_memcache->get($id); 57 | 58 | return (is_array($data)) ? $data[0] : FALSE; 59 | } 60 | 61 | // ------------------------------------------------------------------------ 62 | 63 | /** 64 | * Save 65 | * 66 | * @param string unique identifier 67 | * @param mixed data being cached 68 | * @param int time to live 69 | * @return boolean true on success, false on failure 70 | */ 71 | public function save($id, $data, $ttl = 60) 72 | { 73 | return $this->_memcache->add($id, array($data, time(), $ttl), 0, $ttl); 74 | } 75 | 76 | // ------------------------------------------------------------------------ 77 | 78 | /** 79 | * Replace the cache 80 | * 81 | * @param string unique key 82 | * @param mixed data to store 83 | * @param int length of time (in seconds) the cache is valid 84 | * - Default is 60 seconds 85 | * @return boolean true on success/false on failure 86 | */ 87 | public function replace($id, $data, $ttl = 60) 88 | { 89 | $result = $this->_memcache->replace($id, array($data, time(), $ttl), 0, $ttl); 90 | if($result === FALSE) 91 | { 92 | $result = $this->save($id, $data, $ttl); 93 | } 94 | 95 | return $result; 96 | } 97 | 98 | // ------------------------------------------------------------------------ 99 | 100 | /** 101 | * Delete from Cache 102 | * 103 | * @param mixed key to be deleted. 104 | * @return boolean true on success, false on failure 105 | */ 106 | public function delete($id) 107 | { 108 | return $this->_memcache->delete($id); 109 | } 110 | 111 | // ------------------------------------------------------------------------ 112 | 113 | /** 114 | * Clean the Cache 115 | * 116 | * @return boolean false on failure/true on success 117 | */ 118 | public function clean() 119 | { 120 | return $this->_memcache->flush(); 121 | } 122 | 123 | // ------------------------------------------------------------------------ 124 | 125 | /** 126 | * Cache Info 127 | * 128 | * @param null type not supported in memcached 129 | * @return mixed array on success, false on failure 130 | */ 131 | public function cache_info($type = NULL) 132 | { 133 | return $this->_memcache->getStats(); 134 | } 135 | 136 | // ------------------------------------------------------------------------ 137 | 138 | /** 139 | * Get Cache Metadata 140 | * 141 | * @param mixed key to get cache metadata on 142 | * @return mixed FALSE on failure, array on success. 143 | */ 144 | public function get_metadata($id) 145 | { 146 | $stored = $this->_memcache->get($id); 147 | 148 | if (count($stored) !== 3) 149 | { 150 | return FALSE; 151 | } 152 | 153 | list($data, $time, $ttl) = $stored; 154 | 155 | return array( 156 | 'expire' => $time + $ttl, 157 | 'mtime' => $time, 158 | 'data' => $data 159 | ); 160 | } 161 | 162 | // ------------------------------------------------------------------------ 163 | 164 | /** 165 | * Setup memcached. 166 | */ 167 | private function _setup_memcached() 168 | { 169 | // Try to load memcached server info from the config file. 170 | $CI =& get_instance(); 171 | if ($CI->config->load('memcached', FALSE, TRUE)) 172 | { 173 | if (is_array($CI->config->config['memcached'])) 174 | { 175 | $this->_memcache_conf = NULL; 176 | 177 | foreach ($CI->config->config['memcached'] as $name => $conf) 178 | { 179 | $this->_memcache_conf[$name] = $conf; 180 | } 181 | } 182 | } 183 | 184 | $this->_memcache = new Memcache; 185 | 186 | foreach ($this->_memcache_conf as $name => $cache_server) 187 | { 188 | if ( ! array_key_exists('hostname', $cache_server)) 189 | { 190 | $cache_server['hostname'] = $this->_default_options['default_host']; 191 | } 192 | 193 | if ( ! array_key_exists('port', $cache_server)) 194 | { 195 | $cache_server['port'] = $this->_default_options['default_port']; 196 | } 197 | 198 | if ( ! array_key_exists('weight', $cache_server)) 199 | { 200 | $cache_server['weight'] = $this->_default_options['default_weight']; 201 | } 202 | 203 | $this->_memcache->addServer( 204 | $cache_server['hostname'], $cache_server['port'], $cache_server['weight'] 205 | ); 206 | } 207 | } 208 | 209 | // ------------------------------------------------------------------------ 210 | 211 | 212 | /** 213 | * Is supported 214 | * 215 | * Returns FALSE if memcached is not supported on the system. 216 | * If it is, we setup the memcached object & return TRUE 217 | */ 218 | public function is_supported() 219 | { 220 | if ( ! extension_loaded('memcache')) 221 | { 222 | log_message('debug', 'The Memcache Extension must be loaded to use Memcache Cache.'); 223 | 224 | return FALSE; 225 | } 226 | 227 | $this->_setup_memcached(); 228 | return TRUE; 229 | } 230 | 231 | // ------------------------------------------------------------------------ 232 | 233 | } 234 | // End Class 235 | 236 | /* End of file Cache_memcached.php */ 237 | /* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */ -------------------------------------------------------------------------------- /system/libraries/Cache/drivers/Cache_memcached.php: -------------------------------------------------------------------------------- 1 | array( 34 | 'default_host' => '127.0.0.1', 35 | 'default_port' => 11211, 36 | 'default_weight' => 1 37 | ) 38 | ); 39 | 40 | // ------------------------------------------------------------------------ 41 | 42 | /** 43 | * Fetch from cache 44 | * 45 | * @param mixed unique key id 46 | * @return mixed data on success/false on failure 47 | */ 48 | public function get($id) 49 | { 50 | $data = $this->_memcached->get($id); 51 | 52 | return (is_array($data)) ? $data[0] : FALSE; 53 | } 54 | 55 | // ------------------------------------------------------------------------ 56 | 57 | /** 58 | * Save 59 | * 60 | * @param string unique identifier 61 | * @param mixed data being cached 62 | * @param int time to live 63 | * @return boolean true on success, false on failure 64 | */ 65 | public function save($id, $data, $ttl = 60) 66 | { 67 | if (get_class($this->_memcached) == 'Memcached') 68 | { 69 | return $this->_memcached->set($id, array($data, time(), $ttl), $ttl); 70 | } 71 | else if (get_class($this->_memcached) == 'Memcache') 72 | { 73 | return $this->_memcached->set($id, array($data, time(), $ttl), 0, $ttl); 74 | } 75 | 76 | return FALSE; 77 | } 78 | 79 | // ------------------------------------------------------------------------ 80 | 81 | /** 82 | * Replace the cache 83 | * 84 | * @param string unique key 85 | * @param mixed data to store 86 | * @param int length of time (in seconds) the cache is valid 87 | * - Default is 60 seconds 88 | * @return boolean true on success/false on failure 89 | */ 90 | public function replace($id, $data, $ttl = 60) 91 | { 92 | $result = $this->_memcached->replace($id, array($data, time(), $ttl), $ttl); 93 | if($result === FALSE) 94 | { 95 | $result = $this->save($id, $data, $ttl); 96 | } 97 | 98 | return $result; 99 | } 100 | 101 | // ------------------------------------------------------------------------ 102 | 103 | /** 104 | * Delete from Cache 105 | * 106 | * @param mixed key to be deleted. 107 | * @return boolean true on success, false on failure 108 | */ 109 | public function delete($id) 110 | { 111 | return $this->_memcached->delete($id); 112 | } 113 | 114 | // ------------------------------------------------------------------------ 115 | 116 | /** 117 | * Clean the Cache 118 | * 119 | * @return boolean false on failure/true on success 120 | */ 121 | public function clean() 122 | { 123 | return $this->_memcached->flush(); 124 | } 125 | 126 | // ------------------------------------------------------------------------ 127 | 128 | /** 129 | * Cache Info 130 | * 131 | * @param null type not supported in memcached 132 | * @return mixed array on success, false on failure 133 | */ 134 | public function cache_info($type = NULL) 135 | { 136 | return $this->_memcached->getStats(); 137 | } 138 | 139 | // ------------------------------------------------------------------------ 140 | 141 | /** 142 | * Get Cache Metadata 143 | * 144 | * @param mixed key to get cache metadata on 145 | * @return mixed FALSE on failure, array on success. 146 | */ 147 | public function get_metadata($id) 148 | { 149 | $stored = $this->_memcached->get($id); 150 | 151 | if (count($stored) !== 3) 152 | { 153 | return FALSE; 154 | } 155 | 156 | list($data, $time, $ttl) = $stored; 157 | 158 | return array( 159 | 'expire' => $time + $ttl, 160 | 'mtime' => $time, 161 | 'data' => $data 162 | ); 163 | } 164 | 165 | // ------------------------------------------------------------------------ 166 | 167 | /** 168 | * Setup memcached. 169 | */ 170 | private function _setup_memcached() 171 | { 172 | // Try to load memcached server info from the config file. 173 | $CI =& get_instance(); 174 | if ($CI->config->load('memcached', TRUE, TRUE)) 175 | { 176 | if (is_array($CI->config->config['memcached'])) 177 | { 178 | $this->_memcache_conf = NULL; 179 | 180 | foreach ($CI->config->config['memcached'] as $name => $conf) 181 | { 182 | $this->_memcache_conf[$name] = $conf; 183 | } 184 | } 185 | } 186 | 187 | $this->_memcached = new Memcached(); 188 | 189 | foreach ($this->_memcache_conf as $name => $cache_server) 190 | { 191 | if ( ! array_key_exists('hostname', $cache_server)) 192 | { 193 | $cache_server['hostname'] = $this->_default_options['default_host']; 194 | } 195 | 196 | if ( ! array_key_exists('port', $cache_server)) 197 | { 198 | $cache_server['port'] = $this->_default_options['default_port']; 199 | } 200 | 201 | if ( ! array_key_exists('weight', $cache_server)) 202 | { 203 | $cache_server['weight'] = $this->_default_options['default_weight']; 204 | } 205 | 206 | $this->_memcached->addServer( 207 | $cache_server['hostname'], $cache_server['port'], $cache_server['weight'] 208 | ); 209 | } 210 | } 211 | 212 | // ------------------------------------------------------------------------ 213 | 214 | 215 | /** 216 | * Is supported 217 | * 218 | * Returns FALSE if memcached is not supported on the system. 219 | * If it is, we setup the memcached object & return TRUE 220 | */ 221 | public function is_supported() 222 | { 223 | if ( ! extension_loaded('memcached')) 224 | { 225 | log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.'); 226 | 227 | return FALSE; 228 | } 229 | 230 | $this->_setup_memcached(); 231 | return TRUE; 232 | } 233 | 234 | // ------------------------------------------------------------------------ 235 | 236 | } 237 | // End Class 238 | 239 | /* End of file Cache_memcached.php */ 240 | /* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */ 241 | --------------------------------------------------------------------------------