CacheDb.class.php 5.0 KB

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