Cache.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 CacheService
  10. {//类定义开始
  11. /**
  12. +----------------------------------------------------------
  13. * 是否连接
  14. +----------------------------------------------------------
  15. * @var string
  16. * @access protected
  17. +----------------------------------------------------------
  18. */
  19. protected $connected ;
  20. /**
  21. +----------------------------------------------------------
  22. * 操作句柄
  23. +----------------------------------------------------------
  24. * @var string
  25. * @access protected
  26. +----------------------------------------------------------
  27. */
  28. protected $handler ;
  29. /**
  30. +----------------------------------------------------------
  31. * 缓存存储前缀
  32. +----------------------------------------------------------
  33. * @var string
  34. * @access protected
  35. +----------------------------------------------------------
  36. */
  37. protected $prefix='~@';
  38. /**
  39. +----------------------------------------------------------
  40. * 缓存连接参数
  41. +----------------------------------------------------------
  42. * @var integer
  43. * @access protected
  44. +----------------------------------------------------------
  45. */
  46. protected $options = array();
  47. /**
  48. +----------------------------------------------------------
  49. * 缓存类型
  50. +----------------------------------------------------------
  51. * @var integer
  52. * @access protected
  53. +----------------------------------------------------------
  54. */
  55. protected $type ;
  56. /**
  57. +----------------------------------------------------------
  58. * 缓存过期时间
  59. +----------------------------------------------------------
  60. * @var integer
  61. * @access protected
  62. +----------------------------------------------------------
  63. */
  64. protected $expire ;
  65. /**
  66. +----------------------------------------------------------
  67. * 取得缓存类实例
  68. +----------------------------------------------------------
  69. * @static
  70. * @access public
  71. +----------------------------------------------------------
  72. * @return mixed
  73. +----------------------------------------------------------
  74. */
  75. static function getInstance()
  76. {
  77. global $distribution_cfg;
  78. $type = $distribution_cfg["CACHE_TYPE"];
  79. $cacheClass = 'Cache'.ucwords(strtolower(strim($type)))."Service";
  80. if(file_exists(APP_ROOT_PATH."system/cache/".$cacheClass.".php"))
  81. {
  82. require_once $cacheClass.'.php';
  83. if(class_exists($cacheClass))
  84. {
  85. $cache = new $cacheClass();
  86. }
  87. else
  88. throw_exception('缓存初始化失败:'.$type);
  89. return $cache;
  90. }
  91. else
  92. {
  93. require_once 'CacheFileService.php';
  94. if(class_exists("CacheFileService"))
  95. $cache = new CacheFileService();
  96. else
  97. throw_exception('缓存初始化失败:'.$type);
  98. return $cache;
  99. }
  100. }
  101. protected function log_names($name)
  102. {
  103. if(!$GLOBALS['distribution_cfg']['CACHE_LOG'])return;
  104. $name_logs_files = APP_ROOT_PATH."public/runtime/app/~cache_name.log"; //记录被缓存的名称
  105. if(!file_exists($name_logs_files))
  106. {
  107. @file_put_contents($name_logs_files,'');
  108. }
  109. else
  110. {
  111. if($name!='')
  112. {
  113. $names = @file_get_contents($name_logs_files);
  114. $names = unserialize($names);
  115. if(is_array($names)&&!in_array($name,$names))
  116. {
  117. array_push($names,$name);
  118. }
  119. elseif(!is_array($names))
  120. {
  121. $names = array();
  122. array_push($names,$name);
  123. }
  124. $names = serialize($names);
  125. @file_put_contents($name_logs_files,$names);
  126. }
  127. }
  128. }
  129. protected function get_names()
  130. {
  131. if(!$GLOBALS['distribution_cfg']['CACHE_LOG'])return;
  132. $name_logs_files = APP_ROOT_PATH."public/runtime/app/~cache_name.log"; //记录被缓存的名称
  133. if(file_exists($name_logs_files))
  134. {
  135. $names = @file_get_contents($name_logs_files);
  136. $names = unserialize($names);
  137. if(is_array($names))
  138. {
  139. return $names;
  140. }
  141. else
  142. {
  143. return array();
  144. }
  145. }
  146. else
  147. {
  148. return array();
  149. }
  150. }
  151. protected function del_name_logs()
  152. {
  153. if(!$GLOBALS['distribution_cfg']['CACHE_LOG'])return;
  154. $name_logs_files = APP_ROOT_PATH."public/runtime/app/~cache_name.log"; //记录被缓存的名称
  155. @unlink($name_logs_files);
  156. $GLOBALS['db']->query("update ".DB_PREFIX."conf set `value` = 0 where name = 'PROMOTE_MSG_LOCK' or name ='DEAL_MSG_LOCK'");
  157. }
  158. public function is_redis(){
  159. if($GLOBALS['distribution_cfg']['CACHE_TYPE']=="Rediscache"||$GLOBALS['distribution_cfg']['CACHE_CLIENT']!="")
  160. {
  161. return true;
  162. }else{
  163. return false;
  164. }
  165. }
  166. }//类定义结束
  167. ?>