php memcached緩衝類代碼執行個體

來源:互聯網
上載者:User
  1. class MemcacheModel {
  2. private $mc = null;
  3. /**
  4. * 構造方法,用於添加伺服器並建立memcahced對象
  5. */
  6. function __construct(){
  7. $params = func_get_args();
  8. $mc = new Memcache;
  9. //如果有多個memcache伺服器
  10. if( count($params) > 1){
  11. foreach ($params as $v){
  12. call_user_func_array(array($mc, 'addServer'), $v);
  13. }
  14. //如果只有一個memcache伺服器
  15. } else {
  16. call_user_func_array(array($mc, 'addServer'), $params[0]);
  17. }
  18. $this->mc=$mc;
  19. }
  20. /**
  21. * 擷取memcached對象
  22. * @return object memcached對象
  23. */
  24. function getMem(){
  25. return $this->mc;
  26. }
  27. /**
  28. * 檢查mem是否串連成功
  29. * @return bool 串連成功返回true,否則返回false
  30. */
  31. function mem_connect_error(){
  32. $stats=$this->mc->getStats();
  33. if(emptyempty($stats)){
  34. return false;
  35. }else{
  36. return true;
  37. }
  38. }
  39. private function addKey($tabName, $key){
  40. $keys=$this->mc->get($tabName);
  41. if(emptyempty($keys)){
  42. $keys=array();
  43. }
  44. //如果key不存在,就添加一個
  45. if(!in_array($key, $keys)) {
  46. $keys[]=$key; //將新的key添加到本表的keys中
  47. $this->mc->set($tabName, $keys, MEMCACHE_COMPRESSED, 0);
  48. return true; //不存在返回true
  49. }else{
  50. return false; //存在返回false
  51. }
  52. }
  53. /**
  54. * 向memcache中添加資料
  55. * @param string $tabName 需要快取資料表的表名
  56. * @param string $sql 使用sql作為memcache的key
  57. * @param mixed $data 需要緩衝的資料
  58. */
  59. function addCache($tabName, $sql, $data){
  60. $key=md5($sql);
  61. //如果不存在
  62. if($this->addKey($tabName, $key)){
  63. $this->mc->set($key, $data, MEMCACHE_COMPRESSED, 0);
  64. }
  65. }
  66. /**
  67. * 擷取memcahce中儲存的資料
  68. * @param string $sql 使用SQL的key
  69. * @return mixed 返回緩衝中的資料
  70. */
  71. function getCache($sql){
  72. $key=md5($sql);
  73. return $this->mc->get($key);
  74. }
  75. /**
  76. * 刪除和同一個表相關的所有緩衝
  77. * @param string $tabName 資料表的表名
  78. */
  79. function delCache($tabName){
  80. $keys=$this->mc->get($tabName);
  81. //刪除同一個表的所有緩衝
  82. if(!emptyempty($keys)){
  83. foreach($keys as $key){
  84. $this->mc->delete($key, 0); //0 表示立刻刪除
  85. }
  86. }
  87. //刪除表的所有sql的key
  88. $this->mc->delete($tabName, 0);
  89. }
  90. /**
  91. * 刪除單獨一個語句的緩衝
  92. * @param string $sql 執行的SQL語句
  93. */
  94. function delone($sql){
  95. $key=md5($sql);
  96. $this->mc->delete($key, 0); //0 表示立刻刪除
  97. }
  98. }
  99. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.