<转>Memcache 操作类

之前用CI框架时候用的memcache操作类,因为最近开发框架换成了其他的。所以也对这个类进行了修改。希望可以帮到大家!

 

 

  1. <?php  
  2. /** 
  3.  * Memcache 操作类 
  4.  * 
  5.  * 在config文件中 添加  
  6.      相应配置(可扩展为多memcache server) 
  7.     define('MEMCACHE_HOST', '10.35.52.33'); 
  8.     define('MEMCACHE_PORT', 11211); 
  9.     define('MEMCACHE_EXPIRATION', 0); 
  10.     define('MEMCACHE_PREFIX', 'licai'); 
  11.     define('MEMCACHE_COMPRESSION', FALSE); 
  12.     demo: 
  13.         $cacheObj = new framework_base_memcached();          
  14.         $cacheObj -> set('keyName','this is value'); 
  15.         $cacheObj -> get('keyName'); 
  16.         exit; 
  17.  * @access  public 
  18.  * @return  object 
  19.  * @date    2012-07-02 
  20.  */  
  21. class framework_base_memcached{  
  22.   
  23.   
  24.     private $local_cache = array();  
  25.     private $m;  
  26.     private $client_type;  
  27.     protected $errors = array();  
  28.       
  29.       
  30.     public function __construct()  
  31.     {  
  32.         $this->client_type = class_exists('Memcache') ? "Memcache" : (class_exists('Memcached') ? "Memcached" : FALSE);  
  33.           
  34.         if($this->client_type)  
  35.         {  
  36.             // 判断引入类型  
  37.             switch($this->client_type)  
  38.             {  
  39.                 case 'Memcached':  
  40.                     $this->m = new Memcached();  
  41.                     break;  
  42.                 case 'Memcache':  
  43.                     $this->m = new Memcache();  
  44.                     // if (auto_compress_tresh){  
  45.                         // $this->setcompressthreshold(auto_compress_tresh, auto_compress_savings);  
  46.                     // }  
  47.                     break;  
  48.             }  
  49.             $this->auto_connect();     
  50.         }  
  51.         else  
  52.         {  
  53.             echo 'ERROR: Failed to load Memcached or Memcache Class (∩_∩)';  
  54.             exit;  
  55.         }  
  56.     }  
  57.       
  58.     /** 
  59.      * @Name: auto_connect 
  60.      * @param:none 
  61.      * @todu 连接memcache server 
  62.      * @return : none 
  63.      * add by cheng.yafei 
  64.     **/  
  65.     private function auto_connect()  
  66.     {  
  67.         $configServer = array(  
  68.                                 'host' => MEMCACHE_HOST,   
  69.                                 'port' => MEMCACHE_PORT,   
  70.                                 'weight' => 1,   
  71.                             );  
  72.         if(!$this->add_server($configServer)){  
  73.             echo 'ERROR: Could not connect to the server named '.MEMCACHE_HOST;  
  74.         }else{  
  75.             //echo 'SUCCESS:Successfully connect to the server named '.MEMCACHE_HOST;     
  76.         }  
  77.     }  
  78.       
  79.     /** 
  80.      * @Name: add_server 
  81.      * @param:none 
  82.      * @todu 连接memcache server 
  83.      * @return : TRUE or FALSE 
  84.      * add by cheng.yafei 
  85.     **/  
  86.     public function add_server($server){  
  87.         extract($server);  
  88.         return $this->m->addServer($host, $port, $weight);  
  89.     }  
  90.       
  91.     /** 
  92.      * @Name: add_server 
  93.      * @todu 添加 
  94.      * @param:$key key 
  95.      * @param:$value 值 
  96.      * @param:$expiration 过期时间 
  97.      * @return : TRUE or FALSE 
  98.      * add by cheng.yafei 
  99.     **/  
  100.     public function add($key = NULL, $value = NULL, $expiration = 0)  
  101.     {  
  102.         if(is_null($expiration)){  
  103.             $expiration = MEMCACHE_EXPIRATION;  
  104.         }  
  105.         if(is_array($key))  
  106.         {  
  107.             foreach($key as $multi){  
  108.                 if(!isset($multi['expiration']) || $multi['expiration'] == ''){  
  109.                     $multi['expiration'] = MEMCACHE_EXPIRATION;  
  110.                 }  
  111.                 $this->add($this->key_name($multi['key']), $multi['value'], $multi['expiration']);  
  112.             }  
  113.         }else{  
  114.             $this->local_cache[$this->key_name($key)] = $value;  
  115.             switch($this->client_type){  
  116.                 case 'Memcache':  
  117.                     $add_status = $this->m->add($this->key_name($key), $value, MEMCACHE_COMPRESSION, $expiration);  
  118.                     break;  
  119.                       
  120.                 default:  
  121.                 case 'Memcached':  
  122.                     $add_status = $this->m->add($this->key_name($key), $value, $expiration);  
  123.                     break;  
  124.             }  
  125.               
  126.             return $add_status;  
  127.         }  
  128.     }  
  129.       
  130.     /** 
  131.      * @Name   与add类似,但服务器有此键值时仍可写入替换 
  132.      * @param  $key key 
  133.      * @param  $value 值 
  134.      * @param  $expiration 过期时间 
  135.      * @return TRUE or FALSE 
  136.      * add by cheng.yafei 
  137.     **/  
  138.     public function set($key = NULL, $value = NULL, $expiration = NULL)  
  139.     {  
  140.         if(is_null($expiration)){  
  141.             $expiration = MEMCACHE_EXPIRATION;  
  142.         }  
  143.         if(is_array($key))  
  144.         {  
  145.             foreach($key as $multi){  
  146.                 if(!isset($multi['expiration']) || $multi['expiration'] == ''){  
  147.                     $multi['expiration'] = $this->config['config']['expiration'];  
  148.                 }  
  149.                 $this->set($this->key_name($multi['key']), $multi['value'], $multi['expiration']);  
  150.             }  
  151.         }else{  
  152.             $this->local_cache[$this->key_name($key)] = $value;  
  153.             switch($this->client_type){  
  154.                 case 'Memcache':  
  155.                     $add_status = $this->m->set($this->key_name($key), $value, MEMCACHE_COMPRESSION, $expiration);  
  156.                     break;  
  157.                 case 'Memcached':  
  158.                     $add_status = $this->m->set($this->key_name($key), $value, $expiration);  
  159.                     break;  
  160.             }  
  161.             return $add_status;  
  162.         }  
  163.     }  
  164.       
  165.     /** 
  166.      * @Name   get 根据键名获取值 
  167.      * @param  $key key 
  168.      * @return array OR json object OR string... 
  169.      * add by cheng.yafei 
  170.     **/  
  171.     public function get($key = NULL)  
  172.     {  
  173.         if($this->m)  
  174.         {  
  175.             if(isset($this->local_cache[$this->key_name($key)]))  
  176.             {  
  177.                 return $this->local_cache[$this->key_name($key)];  
  178.             }  
  179.             if(is_null($key)){  
  180.                 $this->errors[] = 'The key value cannot be NULL';  
  181.                 return FALSE;  
  182.             }  
  183.               
  184.             if(is_array($key)){  
  185.                 foreach($key as $n=>$k){  
  186.                     $key[$n] = $this->key_name($k);  
  187.                 }  
  188.                 return $this->m->getMulti($key);  
  189.             }else{  
  190.                 return $this->m->get($this->key_name($key));  
  191.             }  
  192.         }else{  
  193.             return FALSE;  
  194.         }         
  195.     }  
  196.       
  197.     /** 
  198.      * @Name   delete 
  199.      * @param  $key key 
  200.      * @param  $expiration 服务端等待删除该元素的总时间 
  201.      * @return true OR false 
  202.      * add by cheng.yafei 
  203.     **/  
  204.     public function delete($key, $expiration = NULL)  
  205.     {  
  206.         if(is_null($key))  
  207.         {  
  208.             $this->errors[] = 'The key value cannot be NULL';  
  209.             return FALSE;  
  210.         }  
  211.           
  212.         if(is_null($expiration))  
  213.         {  
  214.             $expiration = MEMCACHE_EXPIRATION;  
  215.         }  
  216.           
  217.         if(is_array($key))  
  218.         {  
  219.             foreach($key as $multi)  
  220.             {  
  221.                 $this->delete($multi, $expiration);  
  222.             }  
  223.         }  
  224.         else  
  225.         {  
  226.             unset($this->local_cache[$this->key_name($key)]);  
  227.             return $this->m->delete($this->key_name($key), $expiration);  
  228.         }  
  229.     }  
  230.       
  231.     /** 
  232.      * @Name   replace 
  233.      * @param  $key 要替换的key 
  234.      * @param  $value 要替换的value 
  235.      * @param  $expiration 到期时间 
  236.      * @return none 
  237.      * add by cheng.yafei 
  238.     **/  
  239.     public function replace($key = NULL, $value = NULL, $expiration = NULL)  
  240.     {  
  241.         if(is_null($expiration)){  
  242.             $expiration = MEMCACHE_EXPIRATION;  
  243.         }  
  244.         if(is_array($key)){  
  245.             foreach($key as $multi) {  
  246.                 if(!isset($multi['expiration']) || $multi['expiration'] == ''){  
  247.                     $multi['expiration'] = $this->config['config']['expiration'];  
  248.                 }  
  249.                 $this->replace($multi['key'], $multi['value'], $multi['expiration']);  
  250.             }  
  251.         }else{  
  252.             $this->local_cache[$this->key_name($key)] = $value;  
  253.               
  254.             switch($this->client_type){  
  255.                 case 'Memcache':  
  256.                     $replace_status = $this->m->replace($this->key_name($key), $value, MEMCACHE_COMPRESSION, $expiration);  
  257.                     break;  
  258.                 case 'Memcached':  
  259.                     $replace_status = $this->m->replace($this->key_name($key), $value, $expiration);  
  260.                     break;  
  261.             }  
  262.               
  263.             return $replace_status;  
  264.         }  
  265.     }  
  266.       
  267.     /** 
  268.      * @Name   replace 清空所有缓存 
  269.      * @return none 
  270.      * add by cheng.yafei 
  271.     **/  
  272.     public function flush()  
  273.     {  
  274.         return $this->m->flush();  
  275.     }  
  276.       
  277.     /** 
  278.      * @Name   获取服务器池中所有服务器的版本信息 
  279.     **/  
  280.     public function getversion()  
  281.     {  
  282.         return $this->m->getVersion();  
  283.     }  
  284.       
  285.       
  286.     /** 
  287.      * @Name   获取服务器池的统计信息 
  288.     **/  
  289.     public function getstats($type="items")  
  290.     {  
  291.         switch($this->client_type)  
  292.         {  
  293.             case 'Memcache':  
  294.                 $stats = $this->m->getStats($type);  
  295.                 break;  
  296.               
  297.             default:  
  298.             case 'Memcached':  
  299.                 $stats = $this->m->getStats();  
  300.                 break;  
  301.         }  
  302.         return $stats;  
  303.     }  
  304.       
  305.     /** 
  306.      * @Name: 开启大值自动压缩 
  307.      * @param:$tresh 控制多大值进行自动压缩的阈值。 
  308.      * @param:$savings 指定经过压缩实际存储的值的压缩率,值必须在0和1之间。默认值0.2表示20%压缩率。 
  309.      * @return : true OR false 
  310.      * add by cheng.yafei 
  311.     **/  
  312.     public function setcompressthreshold($tresh, $savings=0.2)  
  313.     {  
  314.         switch($this->client_type)  
  315.         {  
  316.             case 'Memcache':  
  317.                 $setcompressthreshold_status = $this->m->setCompressThreshold($tresh, $savings=0.2);  
  318.                 break;  
  319.                   
  320.             default:  
  321.                 $setcompressthreshold_status = TRUE;  
  322.                 break;  
  323.         }  
  324.         return $setcompressthreshold_status;  
  325.     }  
  326.       
  327.     /** 
  328.      * @Name: 生成md5加密后的唯一键值 
  329.      * @param:$key key 
  330.      * @return : md5 string 
  331.      * add by cheng.yafei 
  332.     **/  
  333.     private function key_name($key)  
  334.     {  
  335.         return md5(strtolower(MEMCACHE_PREFIX.$key));  
  336.     }  
  337.       
  338.     /** 
  339.      * @Name: 向已存在元素后追加数据 
  340.      * @param:$key key 
  341.      * @param:$value value 
  342.      * @return : true OR false 
  343.      * add by cheng.yafei 
  344.     **/  
  345.     public function append($key = NULL, $value = NULL)  
  346.     {  
  347.   
  348.   
  349. //      if(is_array($key))  
  350. //      {  
  351. //          foreach($key as $multi)  
  352. //          {  
  353. //  
  354. //              $this->append($multi['key'], $multi['value']);  
  355. //          }  
  356. //      }  
  357. //      else  
  358. //      {  
  359.             $this->local_cache[$this->key_name($key)] = $value;  
  360.               
  361.             switch($this->client_type)  
  362.             {  
  363.                 case 'Memcache':  
  364.                     $append_status = $this->m->append($this->key_name($key), $value);  
  365.                     break;  
  366.                   
  367.                 default:  
  368.                 case 'Memcached':  
  369.                     $append_status = $this->m->append($this->key_name($key), $value);  
  370.                     break;  
  371.             }  
  372.               
  373.             return $append_status;  
  374. //      }  
  375.     }//END append  
  376.   
  377.   
  378. }// END class  
  379. ?> 

posted on 2015-11-20 23:52  hahahahahai12  阅读(293)  评论(0)    收藏  举报

导航