/Backend/ThinkPHP/Lib/Think/Util/Cache/CacheDb.class.php

https://github.com/qhwang0427/backend_php · PHP · 205 lines · 92 code · 11 blank · 102 comment · 14 complexity · 626bac6397455c6e303e047946e292b5 MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * 数据库类型缓存类
  15. CREATE TABLE THINK_CACHE (
  16. id int(11) unsigned NOT NULL auto_increment,
  17. cachekey varchar(255) NOT NULL,
  18. expire int(11) NOT NULL,
  19. data blob,
  20. datasize int(11),
  21. datacrc int(32),
  22. PRIMARY KEY (id)
  23. );
  24. +------------------------------------------------------------------------------
  25. * @category Think
  26. * @package Think
  27. * @subpackage Util
  28. * @author liu21st <liu21st@gmail.com>
  29. * @version $Id$
  30. +------------------------------------------------------------------------------
  31. */
  32. class CacheDb extends Cache
  33. {//类定义开始
  34. /**
  35. +----------------------------------------------------------
  36. * 缓存数据库对象 采用数据库方式有效
  37. +----------------------------------------------------------
  38. * @var string
  39. * @access protected
  40. +----------------------------------------------------------
  41. */
  42. var $db ;
  43. /**
  44. +----------------------------------------------------------
  45. * 架构函数
  46. +----------------------------------------------------------
  47. * @access public
  48. +----------------------------------------------------------
  49. */
  50. function __construct($options='')
  51. {
  52. if(empty($options)){
  53. $options= array
  54. (
  55. 'db' => C('DB_NAME'),
  56. 'table' => C('DATA_CACHE_TABLE'),
  57. 'expire' => C('DATA_CACHE_TIME'),
  58. );
  59. }
  60. $this->options = $options;
  61. import('Db');
  62. $this->db = DB::getInstance();
  63. $this->connected = is_resource($this->db);
  64. $this->type = strtoupper(substr(__CLASS__,6));
  65. }
  66. /**
  67. +----------------------------------------------------------
  68. * 是否连接
  69. +----------------------------------------------------------
  70. * @access private
  71. +----------------------------------------------------------
  72. * @return boolen
  73. +----------------------------------------------------------
  74. */
  75. private function isConnected()
  76. {
  77. return $this->connected;
  78. }
  79. /**
  80. +----------------------------------------------------------
  81. * 读取缓存
  82. +----------------------------------------------------------
  83. * @access public
  84. +----------------------------------------------------------
  85. * @param string $name 缓存变量名
  86. +----------------------------------------------------------
  87. * @return mixed
  88. +----------------------------------------------------------
  89. */
  90. public function get($name)
  91. {
  92. $name = addslashes($name);
  93. $this->Q(1);
  94. $result = $this->db->getRow('select `data`,`datacrc`,`datasize` from `'.$this->options['table'].'` where `cachekey`=\''.$name.'\' and (`expire` =-1 OR `expire`>'.time().') limit 0,1');
  95. if(false !== $result ) {
  96. if(is_object($result)) {
  97. $result = get_object_vars($result);
  98. }
  99. if(C('DATA_CACHE_CHECK')) {//开启数据校验
  100. if($result['datacrc'] != md5($result['data'])) {//校验错误
  101. return false;
  102. }
  103. }
  104. $content = $result['data'];
  105. if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  106. //启用数据压缩
  107. $content = gzuncompress($content);
  108. }
  109. $content = unserialize($content);
  110. return $content;
  111. }
  112. else {
  113. return false;
  114. }
  115. }
  116. /**
  117. +----------------------------------------------------------
  118. * 写入缓存
  119. +----------------------------------------------------------
  120. * @access public
  121. +----------------------------------------------------------
  122. * @param string $name 缓存变量名
  123. * @param mixed $value 存储数据
  124. * @param integer $expire 有效时间(秒)
  125. +----------------------------------------------------------
  126. * @return boolen
  127. +----------------------------------------------------------
  128. */
  129. public function set($name, $value,$expireTime=0)
  130. {
  131. $data = serialize($value);
  132. $name = addslashes($name);
  133. $this->W(1);
  134. if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  135. //数据压缩
  136. $data = gzcompress($data,3);
  137. }
  138. if(C('DATA_CACHE_CHECK')) {//开启数据校验
  139. $crc = md5($data);
  140. }else {
  141. $crc = '';
  142. }
  143. $expire = !empty($expireTime)? $expireTime : $this->options['expire'];
  144. $map = array();
  145. $map['cachekey'] = $name;
  146. $map['data'] = $data ;
  147. $map['datacrc'] = $crc;
  148. $map['expire'] = ($expire==-1)?-1: (time()+$expire) ;//缓存有效期为-1表示永久缓存
  149. $map['datasize'] = strlen($data);
  150. $result = $this->db->getRow('select `id` from `'.$this->options['table'].'` where `cachekey`=\''.$name.'\' limit 0,1');
  151. if(false !== $result ) {
  152. //更新记录
  153. $result = $this->db->save($map,$this->options['table'],'`cachekey`=\''.$name.'\'');
  154. }else {
  155. //新增记录
  156. $result = $this->db->add($map,$this->options['table']);
  157. }
  158. if($result) {
  159. return true;
  160. }else {
  161. return false;
  162. }
  163. }
  164. /**
  165. +----------------------------------------------------------
  166. * 删除缓存
  167. +----------------------------------------------------------
  168. * @access public
  169. +----------------------------------------------------------
  170. * @param string $name 缓存变量名
  171. +----------------------------------------------------------
  172. * @return boolen
  173. +----------------------------------------------------------
  174. */
  175. public function rm($name)
  176. {
  177. $name = addslashes($name);
  178. return $this->db->_execute('delete from `'.$this->options['table'].'` where `cachekey`=\''.$name.'\'');
  179. }
  180. /**
  181. +----------------------------------------------------------
  182. * 清除缓存
  183. +----------------------------------------------------------
  184. * @access public
  185. +----------------------------------------------------------
  186. * @return boolen
  187. +----------------------------------------------------------
  188. */
  189. public function clear()
  190. {
  191. return $this->db->_execute('truncate table `'.$this->options['table'].'`');
  192. }
  193. }//类定义结束
  194. ?>