<?PHP/** * Memcache Operation implementation * @author Timeless*/classMemcache_manage {//CI Original information Private $_ci; Private $_memcache_prefix; Private $host; Private $port; Private $expire; Private $weight; /** * Read profile information and update * @access Public*/ Public functionMemcache$flag= ' Default ') { //to access CodeIgniter's original resources in your custom class library, you must use the Get_instance () function. This function returns a CodeIgniter Super object. $this->_ci = &get_instance (); //record the data in the Memcache cache configuration//memcached
/* configuration information in the memcahed.php file in the CI framework$config=Array( //It is now a separate Memcache server that can add more than just the Mem object to add Addserver' Default ' =Array( ' Hostname ' = ' 127.0.0.1 ', ' port ' = ' 11211 ', ' weight ' = ' 1 ',//100 minutes' Expire ' = ' 6000 ', ' memcache_prefix ' and ', ' ),); */ $this->_ci->config->load (' memcached ',FALSE,TRUE); //Get configuration file $default _conf=$this->_ci->config->item (' Default '); $this->host =$default _conf[' hostname ']; $this->port =$default _conf[' Port ']; $this->expire =$default _conf[' Expire ']; $this->weight =$default _conf[' Weight ']; $this->_memcache_prefix =$default _conf[' Memcache_prefix ']; $this->connected_server =Array(); $this-_connect (); } /** * Connect memcache database * @access Private*/ Private function_connect () {if(function_exists(' Memcache_connect ')) { $this->cache =NewMemcache; $this-_connect_memcached (); } } /** * Add memcache Server * @access private*/ Private function_connect_memcached () {$error _display=Ini_get(' Display_errors '); $error _reporting=Ini_get(' error_reporting '); if($this->cache->addserver ($this->host,$this->port,TRUE,$this-weight)) { $this->connected_server[] =$this-host; } Ini_set(' error_reporting ',$error _reporting); } Public functionGet$key) { if(Empty($this-connected_server)) { return false; } return $this->cache->get ($this->key_name ($key)); } Public functionSet$key,$data) { if(Empty($this-connected_server)) { return false; } return $this->cache->set ($this->key_name ($key),$data, 0,$this-expire); } Public functionSet_expire ($key,$data,$expire) { if(Empty($this-connected_server)) { return false; } return $this->cache->set ($this->key_name ($key),$data, 0,$expire); } Public functionReplace$key,$data) { if(Empty($this-connected_server)) { return false; } return $this->cache->replace ($this->key_name ($key),$data, 0,$this-expire); } Public functionDelete$key,$when= 0) { if(Empty($this-connected_server)) { return false; } return $this->cache->delete ($this->key_name ($key),$when); } Public function Flush() { return $this->cache->Flush(); } /** * @Name: Unique key value after generating MD5 encryption * @param: $key key * @return: MD5 String **/ Private functionKey_name ($key) { return MD5(Strtolower($this->_memcache_prefix.$key)); }}
Memcache Operation class