Db.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Think\Cache\Driver;
  12. use Think\Cache;
  13. /**
  14. * 数据库方式缓存驱动
  15. * CREATE TABLE think_cache (
  16. * cachekey varchar(255) NOT NULL,
  17. * expire int(11) NOT NULL,
  18. * data blob,
  19. * datacrc int(32),
  20. * UNIQUE KEY `cachekey` (`cachekey`)
  21. * );
  22. */
  23. class Db extends Cache
  24. {
  25. /**
  26. * 架构函数
  27. * @param array $options 缓存参数
  28. * @access public
  29. */
  30. public function __construct($options = array())
  31. {
  32. if (empty($options)) {
  33. $options = array(
  34. 'table' => C('DATA_CACHE_TABLE'),
  35. );
  36. }
  37. $this->options = $options;
  38. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  39. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  40. $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
  41. $this->handler = \Think\Db::getInstance();
  42. }
  43. /**
  44. * 读取缓存
  45. * @access public
  46. * @param string $name 缓存变量名
  47. * @return mixed
  48. */
  49. public function get($name)
  50. {
  51. $name = $this->options['prefix'] . addslashes($name);
  52. N('cache_read', 1);
  53. $result = $this->handler->query('SELECT `data`,`datacrc` FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $name . '\' AND (`expire` =0 OR `expire`>' . time() . ') LIMIT 0,1');
  54. if (false !== $result) {
  55. $result = $result[0];
  56. if (C('DATA_CACHE_CHECK')) {
  57. //开启数据校验
  58. if (md5($result['data']) != $result['datacrc']) { //校验错误
  59. return false;
  60. }
  61. }
  62. $content = $result['data'];
  63. if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  64. //启用数据压缩
  65. $content = gzuncompress($content);
  66. }
  67. $content = unserialize($content);
  68. return $content;
  69. } else {
  70. return false;
  71. }
  72. }
  73. /**
  74. * 写入缓存
  75. * @access public
  76. * @param string $name 缓存变量名
  77. * @param mixed $value 存储数据
  78. * @param integer $expire 有效时间(秒)
  79. * @return boolean
  80. */
  81. public function set($name, $value, $expire = null)
  82. {
  83. $data = serialize($value);
  84. $name = $this->options['prefix'] . addslashes($name);
  85. N('cache_write', 1);
  86. if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  87. //数据压缩
  88. $data = gzcompress($data, 3);
  89. }
  90. if (C('DATA_CACHE_CHECK')) {
  91. //开启数据校验
  92. $crc = md5($data);
  93. } else {
  94. $crc = '';
  95. }
  96. if (is_null($expire)) {
  97. $expire = $this->options['expire'];
  98. }
  99. $expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存
  100. $result = $this->handler->query('select `cachekey` from `' . $this->options['table'] . '` where `cachekey`=\'' . $name . '\' limit 0,1');
  101. if (!empty($result)) {
  102. //更新记录
  103. $result = $this->handler->execute('UPDATE ' . $this->options['table'] . ' SET data=\'' . $data . '\' ,datacrc=\'' . $crc . '\',expire=' . $expire . ' WHERE `cachekey`=\'' . $name . '\'');
  104. } else {
  105. //新增记录
  106. $result = $this->handler->execute('INSERT INTO ' . $this->options['table'] . ' (`cachekey`,`data`,`datacrc`,`expire`) VALUES (\'' . $name . '\',\'' . $data . '\',\'' . $crc . '\',' . $expire . ')');
  107. }
  108. if ($result) {
  109. if ($this->options['length'] > 0) {
  110. // 记录缓存队列
  111. $this->queue($name);
  112. }
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * 删除缓存
  120. * @access public
  121. * @param string $name 缓存变量名
  122. * @return boolean
  123. */
  124. public function rm($name)
  125. {
  126. $name = $this->options['prefix'] . addslashes($name);
  127. return $this->handler->execute('DELETE FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $name . '\'');
  128. }
  129. /**
  130. * 清除缓存
  131. * @access public
  132. * @return boolean
  133. */
  134. public function clear()
  135. {
  136. return $this->handler->execute('TRUNCATE TABLE `' . $this->options['table'] . '`');
  137. }
  138. }