-
php缓存数据功能的mysqli类
- <?php
- /**
- * Mysqli类
- *
- * @author 废墟
- * @version v1.0 2009-08-18
- */
- class db_mysqli {
- protected $mysqli;
- protected $sql;
- protected $rs;
- protected $query_num = 0;
- protected $fetch_mode = MYSQLI_ASSOC;
- protected $cache_dir = './cache/';
- protected $cache_time = 1800;
- public function __construct($dbhost, $dbuser, $dbpass, $dbname) {
- $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
- if(mysqli_connect_errno()) {
- $this->mysqli = false;
- echo '<h2>'.mysqli_connect_error().'</h2>';
- die();
- } else {
- $this->mysqli->set_charset("utf8");
- }
- }
- public function __destruct() {
- $this->free();
- $this->close();
- }
- protected function free() {
- @$this->rs->free();
- }
- protected function close() {
- $this->mysqli->close();
- }
- protected function fetch() {
- return $this->rs->fetch_array($this->fetch_mode);
- }
- protected function getQuerySql($sql, $limit = null) {
- if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) {
- $sql .= " LIMIT " . $limit;
- }
- return $sql;
- }
- protected function get_cache($sql,$method) {
- include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件
- $cache = new cache($this->cache_dir,$this->cache_time);
- $cache_file = md5($sql.$method);
- $res = $cache->get_cache($cache_file);
- if(!$res) {
- $res = $this->$method($sql);
- $cache->set_cache($cache_file, $res);
- }
- return $res;
- }
- public function query_num() {
- return $this->query_num;
- }
- public function set_cache_dir($cache_dir) {
- $this->cache_dir = $cache_dir;
- }
- public function set_cache_time($cache_time) {
- $this->cache_time = $cache_time;
- }
- public function query($sql, $limit = null) {
- $sql = $this->getQuerySql($sql, $limit);
- $this->sql = $sql;
- $this->rs = $this->mysqli->query($sql);
- if (!$this->rs) {
- echo "<h2>".$this->mysqli->error."</h2>";
- die();
- } else {
- $this->query_num++;
- return $this->rs;
- }
- }
- public function getOne($sql) {
- $this->query($sql, 1);
- $this->fetch_mode = MYSQLI_NUM;
- $row = $this->fetch();
- $this->free();
- return $row[0];
- }
- public function get_one($sql) {
- return $this->getOne($sql);
- }
- public function cache_one($sql) {
- $sql = $this->getQuerySql($sql, 1);
- return $this->get_cache($sql, 'getOne');
- }
- public function getRow($sql, $fetch_mode = MYSQLI_ASSOC) {
- $this->query($sql, 1);
- $this->fetch_mode = $fetch_mode;
- $row = $this->fetch();
- $this->free();
- return $row;
- }
- public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) {
- return $this->getRow($sql);
- }
- public function cache_row($sql) {
- $sql = $this->getQuerySql($sql, 1);
- return $this->get_cache($sql, 'getRow');
- }
- public function getAll($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
- $this->query($sql, $limit);
- $all_rows = array();
- $this->fetch_mode = $fetch_mode;
- while($rows = $this->fetch()) {
- $all_rows[] = $rows;
- }
- $this->free();
- return $all_rows;
- }
- public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
- return $this->getAll($sql);
- }
- public function cache_all($sql, $limit = null) {
- $sql = $this->getQuerySql($sql, $limit);
- return $this->get_cache($sql, 'getAll');
- }
- public function insert_id() {
- return $this->mysqli->insert_id();
- }
- public function escape($str) {
- if(is_array($str)) {
- foreach($str as $key=>$val) {
- $str[$key] = $this->escape($val);
- }
- } else {
- $str = addslashes(trim($str));
- } //开源代码phpfensi.com
- return $str;
- }
- }
- //用法
- $db = new db_mysqli('localhost', 'root', 111222, 'dict');
- $db->set_cache_time(10);
- $db->set_cache_dir('./cache/sql/');
- $sql = "select * from words order by word_id limit 10,10";
- $res1 = $db->get_all($sql);
- $res2 = $db->cache_all($sql);
- echo $db->query_num(),'<br>';
- ?>
出处:http://www.phpfensi.com/php/20140910/5295.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式