mysqlcache.init.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Dao-mysqlcache 数据库缓存
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * $Author:zhuli
  10. * $Dtime:2013-5-29
  11. *************************************************************************
  12. * CREATE TABLE `initphp_mysqlcache` (
  13. * `id` int(10) NOT NULL auto_increment,
  14. * `k` varchar(255) NOT NULL default '',
  15. * `v` text NOT NULL default '',
  16. * `dtime` int(10) NOT NULL default '0',
  17. * `cachetime` int(10) NOT NULL default '0',
  18. * PRIMARY KEY (`id`),
  19. * KEY `k` (`k`)
  20. * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  21. ***********************************************************************************/
  22. class mysqlcacheInit {
  23. private $sql;
  24. /**
  25. * MYSQL缓存-设置缓存
  26. * 检查缓存是否已经存在,如果不存在,则设置,存在的话则更新缓存
  27. * @param string $key 缓存名
  28. * @param array $value 缓存数据
  29. * @return
  30. */
  31. public function set_cache($key, $value, $cachetime) {
  32. $key = $this->get_cache_key($key);
  33. $time = time();
  34. $select_sql = sprintf("SELECT * FROM initphp_mysqlcache WHERE k = %s LIMIT 1", $this->sql->build_escape($key));
  35. $query = $this->sql->query($select_sql);
  36. $result = $this->sql->fetch_assoc($query);
  37. if ($result) {
  38. $sql = sprintf("UPDATE initphp_mysqlcache set v = %s, dtime = %d, cachetime = %d WHERE k = %s",
  39. $this->sql->build_escape(base64_encode(@serialize($value))),
  40. $time,
  41. (int) $cachetime,
  42. $this->sql->build_escape($key)
  43. );
  44. } else {
  45. $sql = sprintf("INSERT INTO initphp_mysqlcache (k, v, dtime, cachetime) VALUES (%s, %s, %d, %d)",
  46. $this->sql->build_escape($key),
  47. $this->sql->build_escape(base64_encode(@serialize($value))),
  48. $time,
  49. (int) $cachetime
  50. );
  51. }
  52. return $this->sql->query($sql);
  53. }
  54. /**
  55. * MYSQL缓存-获取缓存
  56. * 从数据库中读取缓存数据,如果缓存数据不存在,则返回false
  57. * 缓存数据存在,如果过期则删除缓存,返回false,没过期或者永久,返回值
  58. * @param string $key 缓存名
  59. * @param array $value 缓存数据
  60. * @return
  61. */
  62. public function get_cache($key) {
  63. $key = $this->get_cache_key($key);
  64. $select_sql = sprintf("SELECT * FROM initphp_mysqlcache WHERE k = %s LIMIT 1", $this->sql->build_escape($key));
  65. $query = $this->sql->query($select_sql);
  66. $result = $this->sql->fetch_assoc($query);
  67. if (!$result) return false;
  68. if ($result['cachetime'] == 0) return @unserialize(base64_decode($result['v']));
  69. if ((time() > ($result['dtime'] + $result['cachetime']))) {
  70. $delete_sql = sprintf("DELETE FROM initphp_mysqlcache WHERE k = %s ", $this->sql->build_escape($key));
  71. $this->sql->query($delete_sql); //缓存过期,则删除缓存
  72. return false; //过期
  73. }
  74. return @unserialize(base64_decode($result['v']));
  75. }
  76. /**
  77. * MYSQL缓存-清除缓存
  78. * @param string $key 缓存名
  79. * @return
  80. */
  81. public function clear($key) {
  82. $key = $this->get_cache_key($key);
  83. $sql = sprintf("DELETE FROM initphp_mysqlcache WHERE k = %s", $this->sql->build_escape($key));
  84. return $this->sql->query($sql);
  85. }
  86. /**
  87. * MYSQL缓存-清除所有缓存
  88. * 一般情况下,请慎用清除所有缓存功能
  89. * @return
  90. */
  91. public function clear_all() {
  92. $sql = "TRUNCATE TABLE initphp_mysqlcache";
  93. return $this->sql->query($sql);
  94. }
  95. /**
  96. * MYSQL缓存-获取DB handler
  97. * DB handler 是根据InitPHP开源框架自带的SQL连接来执行
  98. * @return
  99. */
  100. public function set_sql_handler($obj) {
  101. $this->sql = $obj;
  102. }
  103. /**
  104. * MYSQL缓存-获取缓存KEY值
  105. * @param string $key 缓存名
  106. * @return string
  107. */
  108. private function get_cache_key($key) {
  109. return md5(trim($key));
  110. }
  111. }