CacheMemcachesaslService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 CacheMemcachesaslService extends CacheService
  10. {
  11. private $mem;
  12. private $dir; //模拟的目录,即前缀
  13. /**
  14. +----------------------------------------------------------
  15. * 架构函数
  16. +----------------------------------------------------------
  17. * @access public
  18. +----------------------------------------------------------
  19. */
  20. public function __construct()
  21. {
  22. require_once APP_ROOT_PATH."system/cache/MemcacheSASL/MemcacheSASL.php";
  23. $this->mem = new MemcacheSASL;
  24. $this->mem->addServer($GLOBALS['distribution_cfg']['CACHE_CLIENT'], $GLOBALS['distribution_cfg']['CACHE_PORT']);
  25. $this->mem->setSaslAuthData($GLOBALS['distribution_cfg']['CACHE_USERNAME'],$GLOBALS['distribution_cfg']['CACHE_PASSWORD']);
  26. }
  27. /**
  28. +----------------------------------------------------------
  29. * 读取缓存
  30. +----------------------------------------------------------
  31. * @access public
  32. +----------------------------------------------------------
  33. * @param string $name 缓存变量名
  34. +----------------------------------------------------------
  35. * @return mixed
  36. +----------------------------------------------------------
  37. */
  38. public function get($name)
  39. {
  40. if(!$this->mem)return false;
  41. if(IS_DEBUG)return false;
  42. $var_name = md5($this->dir.$name);
  43. global $$var_name;
  44. if($$var_name)
  45. {
  46. return $$var_name;
  47. }
  48. $data = $this->mem->get($var_name);
  49. if($data)
  50. {
  51. $$var_name = $data;
  52. }
  53. else
  54. {
  55. $data = false;
  56. }
  57. return $data;
  58. }
  59. /**
  60. +----------------------------------------------------------
  61. * 写入缓存
  62. +----------------------------------------------------------
  63. * @access public
  64. +----------------------------------------------------------
  65. * @param string $name 缓存变量名
  66. * @param mixed $value 存储数据
  67. +----------------------------------------------------------
  68. * @return boolen
  69. +----------------------------------------------------------
  70. */
  71. public function set($name, $value,$expire ="-1")
  72. {
  73. if(app_conf("CACHE_ON")==0||IS_DEBUG)return false;
  74. if(!$this->mem)return false;
  75. if($expire=='-1') $expire = 3600*24;
  76. $key = md5($this->dir.$name);
  77. $this->log_names($key);
  78. return $this->mem->set($key,$value,$expire);
  79. }
  80. /**
  81. +----------------------------------------------------------
  82. * 删除缓存
  83. +----------------------------------------------------------
  84. * @access public
  85. +----------------------------------------------------------
  86. * @param string $name 缓存变量名
  87. +----------------------------------------------------------
  88. * @return boolen
  89. +----------------------------------------------------------
  90. */
  91. public function rm($name)
  92. {
  93. if(!$this->mem)return false;
  94. $key = md5($this->dir.$name);
  95. return $this->mem->delete($key);
  96. }
  97. public function clear()
  98. {
  99. if(!$this->mem)return false;
  100. $this->mem->flush(0);
  101. }
  102. public function set_dir($dir='')
  103. {
  104. if($dir!='')
  105. {
  106. $this->dir = md5($dir);
  107. }
  108. }
  109. }//类定义结束
  110. ?>