You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.9 KiB

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