CacheMemcachedService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class CacheMemcachedService extends CacheService
  10. {
  11. private $mem;
  12. private $dir; //模拟的目录,即前缀
  13. /**
  14. +----------------------------------------------------------
  15. * 架构函数
  16. +----------------------------------------------------------
  17. * @access public
  18. +----------------------------------------------------------
  19. */
  20. public function __construct()
  21. {
  22. if(!class_exists("Memcache"))
  23. return false;
  24. $this->mem = new Memcache;
  25. $host = $GLOBALS['distribution_cfg']['CACHE_CLIENT'];
  26. $port = $GLOBALS['distribution_cfg']['CACHE_PORT']; //默认端口为11211
  27. $this->mem->connect($host, $port); //此处为memcache的连接主机与端口
  28. $this->dir = "";
  29. }
  30. /**
  31. +----------------------------------------------------------
  32. * 读取缓存
  33. +----------------------------------------------------------
  34. * @access public
  35. +----------------------------------------------------------
  36. * @param string $name 缓存变量名
  37. +----------------------------------------------------------
  38. * @return mixed
  39. +----------------------------------------------------------
  40. */
  41. public function get($name)
  42. {
  43. if(!$this->mem)return false;
  44. if(IS_DEBUG)return false;
  45. $var_name = md5($this->dir.$name);
  46. global $$var_name;
  47. if($$var_name)
  48. {
  49. return $$var_name;
  50. }
  51. $data = $this->mem->get($var_name);
  52. if($data)
  53. {
  54. $$var_name = $data;
  55. }
  56. else
  57. {
  58. $data = false;
  59. }
  60. return $data;
  61. }
  62. /**
  63. +----------------------------------------------------------
  64. * 写入缓存
  65. +----------------------------------------------------------
  66. * @access public
  67. +----------------------------------------------------------
  68. * @param string $name 缓存变量名
  69. * @param mixed $value 存储数据
  70. +----------------------------------------------------------
  71. * @return boolen
  72. +----------------------------------------------------------
  73. */
  74. public function set($name, $value,$expire ="-1")
  75. {
  76. if(IS_DEBUG)return false;
  77. if(!$this->mem)return false;
  78. if($expire=='-1') $expire = 3600*24;
  79. $key = md5($this->dir.$name);
  80. $this->log_names($key);
  81. return $this->mem->set($key,$value,0,$expire);
  82. }
  83. /**
  84. +----------------------------------------------------------
  85. * 删除缓存
  86. +----------------------------------------------------------
  87. * @access public
  88. +----------------------------------------------------------
  89. * @param string $name 缓存变量名
  90. +----------------------------------------------------------
  91. * @return boolen
  92. +----------------------------------------------------------
  93. */
  94. public function rm($name)
  95. {
  96. if(!$this->mem)return false;
  97. $key = md5($this->dir.$name);
  98. return $this->mem->delete($key);
  99. }
  100. public function clear()
  101. {
  102. $names = $this->get_names();
  103. foreach($names as $name)
  104. {
  105. $this->mem->delete($name);
  106. }
  107. $this->del_name_logs();
  108. }
  109. public function set_dir($dir='')
  110. {
  111. if($dir!='')
  112. {
  113. $this->dir = md5($dir);
  114. }
  115. }
  116. }//类定义结束
  117. ?>