CacheRediscacheService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 CacheRediscacheService
  10. {
  11. private $mem;
  12. private $dir; //模拟的目录,即前缀
  13. var $prefix;
  14. var $file_prefix;
  15. // var $redis_distribution;
  16. /**
  17. +----------------------------------------------------------
  18. * 架构函数
  19. +----------------------------------------------------------
  20. * @access public
  21. +----------------------------------------------------------
  22. */
  23. public function __construct($is_read=false)
  24. {
  25. // $count = intval($_REQUEST['count']);
  26. // $count = $count?$count: count($GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION']);
  27. //
  28. //// $count = count($GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION']);
  29. if($GLOBALS['distribution_cfg']['RDB_CLIENT']==$GLOBALS['distribution_cfg']['CACHE_CLIENT']){
  30. $this->mem = $GLOBALS['redisdb'];
  31. }else{
  32. $this->mem = new Rediscache($GLOBALS['distribution_cfg']['CACHE_CLIENT'], $GLOBALS['distribution_cfg']['CACHE_PORT'],$GLOBALS['distribution_cfg']['CACHE_PASSWORD']);
  33. }
  34. $this->prefix = $GLOBALS['distribution_cfg']['REDIS_PREFIX'];
  35. $this->file_prefix = $GLOBALS['distribution_cfg']['REDIS_PREFIX'].'filecache:';
  36. //
  37. // $count = count($GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION']);
  38. // if($count){
  39. // $rid = hash_table(es_session::id(),$count);
  40. // $redis_distribution = $GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION'][$rid];
  41. // $this->redis_distribution = new Rediscache($redis_distribution['CACHE_CLIENT'], $redis_distribution['CACHE_PORT'],$redis_distribution['CACHE_PASSWORD']);
  42. // }else{
  43. // $this->redis_distribution = $this->mem;
  44. // }
  45. }
  46. /**
  47. +----------------------------------------------------------
  48. * 读取缓存
  49. +----------------------------------------------------------
  50. * @access public
  51. +----------------------------------------------------------
  52. * @param string $name 缓存变量名
  53. +----------------------------------------------------------
  54. * @return mixed
  55. +----------------------------------------------------------
  56. */
  57. public function get($name,$readonly=false)
  58. {
  59. if(!$this->mem)return false;
  60. //if(IS_DEBUG)return false;
  61. $var_name = $this->file_prefix.($this->dir.$name);
  62. global $$var_name;
  63. if($$var_name)
  64. {
  65. return $$var_name;
  66. }
  67. $data = $this->mem->get($var_name);
  68. if($data)
  69. {
  70. $data = unserialize($data);
  71. $$var_name = $data;
  72. }
  73. else
  74. {
  75. $data = false;
  76. }
  77. return $data;
  78. }
  79. /**
  80. +----------------------------------------------------------
  81. * 写入缓存
  82. +----------------------------------------------------------
  83. * @access public
  84. +----------------------------------------------------------
  85. * @param string $name 缓存变量名
  86. * @param mixed $value 存储数据
  87. +----------------------------------------------------------
  88. * @return boolen
  89. +----------------------------------------------------------
  90. */
  91. public function set($name, $value,$expire ="-1",$readonly=false)
  92. {
  93. //if(IS_DEBUG)return false;
  94. if(!$this->mem)return false;
  95. if($expire=='-1') $expire = 3600*24;
  96. $value = serialize($value);
  97. $key = $this->file_prefix.($this->dir.$name);
  98. return $this->mem->set($key,$value,$expire);
  99. }
  100. public function set_lock($name,$exp=10){
  101. if(!$this->mem)return false;
  102. $rand_num = rand();
  103. $key = $this->file_prefix.($this->dir.$name).'_lock';
  104. $ok = $this->mem->set_lock($key,$rand_num,$exp);
  105. return $ok;
  106. }
  107. public function del_lock($name){
  108. if(!$this->mem)return false;
  109. $key = $this->file_prefix.($this->dir.$name).'_lock';
  110. $ok = $this->mem->delete($key);
  111. return $ok;
  112. }
  113. /**
  114. +----------------------------------------------------------
  115. * 删除缓存
  116. +----------------------------------------------------------
  117. * @access public
  118. +----------------------------------------------------------
  119. * @param string $name 缓存变量名
  120. +----------------------------------------------------------
  121. * @return boolen
  122. +----------------------------------------------------------
  123. */
  124. public function rm($name)
  125. {
  126. if(!$this->mem)return false;
  127. $key = $this->file_prefix.($this->dir.$name);
  128. return $this->mem->delete($key);
  129. }
  130. public function clear()
  131. {
  132. if(!$this->mem)return false;
  133. //获取所有的数据缓存文件
  134. $keys = $this->mem->keys($this->file_prefix,true);
  135. if(!$keys){
  136. return true;
  137. }
  138. $this->mem->delete($keys);
  139. //$this->mem->flush(0);
  140. }
  141. public function clear_by_name($name){
  142. if(!$this->mem)return false;
  143. //获取所有的数据缓存文件
  144. $keys = $this->mem->keys($this->file_prefix.$name,true);
  145. if(!$keys){
  146. return true;
  147. }
  148. $this->mem->delete($keys);
  149. }
  150. public function celar_con(){
  151. if(!$this->mem)return false;
  152. $keys = array();
  153. //获取所有的数据缓存文件
  154. // $keys = $this->mem->keys('fanwe0000001:user_contribution:',true);
  155. // $keys1 = $this->mem->keys('fanwe0000001:video_contribution:',true);
  156. // $keys2 = $this->mem->keys('fanwe0000001:video:',true);
  157. // $keys3 = $this->mem->keys('fanwe0000001:video_gift:',true);
  158. // $keys4 = $this->mem->keys('fanwe0000001:user_winning:',true);
  159. // $keys5 = $this->mem->keys('fanwe0000001:video_viewer:',true);
  160. // $keys6 = $this->mem->keys('fanwe0000001:video_viewer_level:',true);
  161. // $keys7 = $this->mem->keys('fanwe0000001:user_followed_by:',true);
  162. //$keys8 = $this->mem->keys('fanwe0000001:cate:',true);
  163. // $keys9 = $this->mem->keys('fanwe0000001:video_condition',true);
  164. // $keys = array_merge($keys8);
  165. //$keys = $keys8;
  166. // $keys[] = 'fanwe0000001:video_vote_number';
  167. if(!$keys){
  168. return true;
  169. }
  170. $this->mem->delete($keys);
  171. }
  172. public function set_dir($dir='')
  173. {
  174. if($dir!='')
  175. {
  176. $this->dir =$this->prefix. ($dir);
  177. }
  178. }
  179. public function close(){
  180. $this->mem->close();
  181. }
  182. }//类定义结束
  183. ?>