国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php操作memcache緩存方法分享

2024-05-04 23:35:52
字體:
供稿:網(wǎng)友

一般來說,如果并發(fā)量不大的情況,使不使用緩存技術(shù)并沒有什么影響,但如果高并發(fā)的情況,使用緩存技術(shù)就顯得很重要了,可以很好的減輕數(shù)據(jù)庫和服務(wù)器的壓力,當然解決高并發(fā)的技術(shù)有很多,這里只是以緩存的角度來說明使用memcache的便捷性和方便性,

使用memcache的前提是需要在服務(wù)端先配置好memcahche的環(huán)境!確認memcahce可以正常連接之后就可以在程序使用了!

 

 
  1. <?php 
  2. /** 
  3. * Memcache緩存操作 
  4. * @author hxm 
  5. * @version 1.0 
  6. * @since 2015.05.04 
  7. */ 
  8. class MCache extends Object implements CacheFace 
  9. private $mem = null; //Mem對象 
  10.  
  11. private $sId = 1; //servier服務(wù)ID 
  12.  
  13. /** 
  14. * 初始化Memcache 
  15. * 
  16. * @return Object 
  17. */ 
  18. public function __construct() 
  19. if ( !class_exists('Memcache') ) 
  20. throw new QException('PHP extension does not exist: Memcache'); 
  21. $this->mem = new Memcache(); 
  22.  
  23. /** 
  24. * 鏈接memcahce服務(wù) 
  25. * 
  26. * @access private 
  27. * @param string $key 關(guān)鍵字 
  28. * @param string $value 緩存內(nèi)容 
  29. * @return array 
  30. */ 
  31. private function connect( $sid ) 
  32. $file = $this->CacheFile(); 
  33. require $file
  34. if(! isset($cache) ) 
  35. throw new QException('緩存配置文件不存在'.$file); 
  36. $server = $cache[$this->cacheId]; 
  37. $sid = isset($sid) == 0 ? $this->sId : $sid;//memcache服務(wù)選擇 
  38. if ( ! $server[$sid]) 
  39. throw new QException('當前操作的緩存服務(wù)器配置文件不存在'); 
  40. $host = $server[$sid]['host']; 
  41. $port = $server[$sid]['port']; 
  42. try { 
  43. $this->mem->connect( $host , $port ); 
  44. } catch (Exception $e) { 
  45. exit('memecache連接失敗,錯誤信息:'$e->getMessage()); 
  46.  
  47. /** 
  48. * 寫入緩存 
  49. * 
  50. * @access private 
  51. * @param string $key 關(guān)鍵字 
  52. * @param string $value 緩存內(nèi)容 
  53. * @return array 
  54. */ 
  55. public function set( $key , $value , $sid , $expire = 0) 
  56. $data = $this->get($key , $sid); //如果已經(jīng)存在key值 
  57. if$data )  
  58. return $this->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire); 
  59. else { 
  60. return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire); 
  61.  
  62. /** 
  63. * 讀取緩存 
  64. * 
  65. * @access private 
  66. * @param string $key 關(guān)鍵字 
  67. * @param int $sid 選擇第幾臺memcache服務(wù)器 
  68. * @return array 
  69. */ 
  70. public function get( $key , $sid
  71. $this->connect( $sid ); 
  72. return $this->mem->get($key); 
  73.  
  74. /** 
  75. * 清洗(刪除)已經(jīng)存儲的所有的元素 
  76. * 
  77. * @access private 
  78. * @return array 
  79. */ 
  80. public function flush() 
  81. $this->connect(); 
  82. return $this->mem->flush(); 
  83. /** 
  84. * 刪除緩存 
  85. * 
  86. * @access private 
  87. * @param string $key 關(guān)鍵字 
  88. * @param int $sid 選擇第幾臺memcache服務(wù)器 
  89. * @return array 
  90. */ 
  91. public function remove( $key , $sid
  92. $this->connect(); 
  93. return $this->mem->delete($key); 
  94.  
  95. /** 
  96. * 析構(gòu)函數(shù) 
  97. * 最后關(guān)閉memcache 
  98. */ 
  99. public function __destruct() 
  100. /*if(! $this->mem) 
  101. { 
  102. $this->mem->close(); 
  103. }*/ 

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 抚顺市| 黔江区| 虎林市| 炎陵县| 习水县| 津市市| 桓台县| 韶关市| 兴义市| 江门市| 瓦房店市| 屏东市| 富蕴县| 宜城市| 巴彦淖尔市| 依兰县| 阜新| 罗平县| 方正县| 正定县| 双柏县| 沐川县| 滕州市| 舞阳县| 乾安县| 收藏| 醴陵市| 门源| 奈曼旗| 万源市| 罗平县| 永靖县| 乌鲁木齐市| 额尔古纳市| 阿图什市| 临泽县| 汝城县| 逊克县| 齐河县| 三台县| 灵武市|