CacheDbService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 CacheDbService extends CacheService
  10. {
  11. private $db;
  12. private $dir; //模拟的目录,即前缀
  13. private $table; //缓存表名
  14. /**
  15. +----------------------------------------------------------
  16. * 架构函数
  17. +----------------------------------------------------------
  18. * @access public
  19. +----------------------------------------------------------
  20. */
  21. public function __construct()
  22. {
  23. $pconnect = false;
  24. $cache_client = $GLOBALS['distribution_cfg']['CACHE_CLIENT']==""?app_conf('DB_HOST'):$GLOBALS['distribution_cfg']['CACHE_CLIENT'];
  25. $cache_port = $GLOBALS['distribution_cfg']['CACHE_PORT']==""?app_conf('DB_PORT'):$GLOBALS['distribution_cfg']['CACHE_PORT'];
  26. $cache_username = $GLOBALS['distribution_cfg']['CACHE_USERNAME']==""?app_conf('DB_USER'):$GLOBALS['distribution_cfg']['CACHE_USERNAME'];
  27. $cache_password = $GLOBALS['distribution_cfg']['CACHE_PASSWORD']==""?app_conf('DB_PWD'):$GLOBALS['distribution_cfg']['CACHE_PASSWORD'];
  28. $cache_db = $GLOBALS['distribution_cfg']['CACHE_DB']==""?app_conf('DB_NAME'):$GLOBALS['distribution_cfg']['CACHE_DB'];
  29. $this->db = new mysql_db($cache_client.":".$cache_port, $cache_username,$cache_password,$cache_db,'utf8',$pconnect);
  30. $this->table = $GLOBALS['distribution_cfg']['CACHE_TABLE']==""?DB_PREFIX."auto_cache":$GLOBALS['distribution_cfg']['CACHE_TABLE'];
  31. $this->db->query("delete from ".$this->table." where cache_time < ".NOW_TIME);
  32. }
  33. /**
  34. +----------------------------------------------------------
  35. * 读取缓存
  36. +----------------------------------------------------------
  37. * @access public
  38. +----------------------------------------------------------
  39. * @param string $name 缓存变量名
  40. +----------------------------------------------------------
  41. * @return mixed
  42. +----------------------------------------------------------
  43. */
  44. public function get($name)
  45. {
  46. if(!$this->db)return false;
  47. if(IS_DEBUG)return false;
  48. $var_name = md5($this->dir.$name);
  49. global $$var_name;
  50. if($$var_name)
  51. {
  52. return $$var_name;
  53. }
  54. $key = $var_name;
  55. $tmp_data = $this->db->getRow("select cache_data,cache_time from ".$this->table." where cache_key = '".$key."'",true);
  56. if($tmp_data['cache_time']>NOW_TIME)
  57. {
  58. $data = unserialize($tmp_data['cache_data']);
  59. }
  60. else
  61. {
  62. $this->db->query("delete from ".$this->table." where cache_key = '".$key."'");
  63. $data = false;
  64. }
  65. $$var_name = $data;
  66. return $data;
  67. }
  68. /**
  69. +----------------------------------------------------------
  70. * 写入缓存
  71. +----------------------------------------------------------
  72. * @access public
  73. +----------------------------------------------------------
  74. * @param string $name 缓存变量名
  75. * @param mixed $value 存储数据
  76. +----------------------------------------------------------
  77. * @return boolen
  78. +----------------------------------------------------------
  79. */
  80. public function set($name, $value,$expire ="-1")
  81. {
  82. if(IS_DEBUG)return false;
  83. if(!$this->db)return false;
  84. if($expire=='-1') $expire = 3600*24;
  85. $cache_data['cache_data'] = serialize($value);
  86. $cache_data['cache_key'] = md5($this->dir.$name);
  87. $cache_data['cache_type'] = $this->dir;
  88. $cache_data['cache_time'] = NOW_TIME+$expire;
  89. $this->db->query("delete from ".$this->table." where cache_key = '".$cache_data['cache_key']."'");
  90. $this->db->autoExecute($this->table, $cache_data);
  91. }
  92. /**
  93. +----------------------------------------------------------
  94. * 删除缓存
  95. +----------------------------------------------------------
  96. * @access public
  97. +----------------------------------------------------------
  98. * @param string $name 缓存变量名
  99. +----------------------------------------------------------
  100. * @return boolen
  101. +----------------------------------------------------------
  102. */
  103. public function rm($name)
  104. {
  105. if(!$this->db)return false;
  106. $key = md5($this->dir.$name);
  107. $this->db->query("delete from ".$this->table." where cache_key = '".$key."'");
  108. }
  109. public function clear()
  110. {
  111. if($this->dir)
  112. $this->db->query("delete from ".$this->table." where cache_type = '".$this->dir."'");
  113. else
  114. $this->db->query("truncate table ".$this->table);
  115. }
  116. public function set_dir($dir='')
  117. {
  118. if($dir!='')
  119. {
  120. $this->dir = md5($dir);
  121. }
  122. }
  123. }//类定义结束
  124. ?>