php写的带缓存数据功能的mysqli类_php技巧

复制代码 代码如下:

<?php
/**
* Mysqli类
*/
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));
}
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>';
?>

时间: 2024-09-10 00:43:30

php写的带缓存数据功能的mysqli类_php技巧的相关文章

一个带缓存数据功能的mysqli类

 代码如下 复制代码   <?php       /**      * Mysqli类      *      * @author 废墟      * @version v1.0 2009-08-18      */       class db_mysqli {           protected $mysqli;           protected $sql;           protected $rs;           protected $query_num    = 0

php实现带读写分离功能的MySQL类完整实例_php技巧

本文实例讲述了php实现带读写分离功能的MySQL类.分享给大家供大家参考,具体如下: 概述: 1. 根据sql语句判断是连接读库还是写库 2. 链式调用$this->where()->get() 3. 不同的主机对应不同的实例, 不再多次new 具体代码如下: <?php class DBRWmysql { private static $Instance = null; private $links = array();//链接数组 private $link = null; //当

js带闹铃功能的倒计时代码_javascript技巧

Js倒计时代码,带闹铃功能,自定义闹钟倒计时功能,点击开始按钮,即可开始倒数,代码不是太复杂,新手应该能看懂,代码分享给大家. 效果图: 源码: <html> <head> <title>Js倒计时,闹铃功能</title> <script language="javascript"> function $(id){ return document.getElementById(id); } function down(){

PHP缓存技术的多种方法小结_php技巧

1.普遍缓存技术: 数据缓存:这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得. 用的最广的例子看Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先搜索缓存表. 举个常用的方法,多表关联的时候,把附表中的内容生成数组保存到主表的一个字段中,需要的时候数组分解一下,这样的好处是只读一个表,坏处就是两个数据同步会

PHP文件缓存内容保存格式实例分析_php技巧

本文实例讲述了PHP文件缓存内容保存格式,对于进行PHP项目开发非常具有实用价值.分享给大家供大家参考借鉴.具体分析如下: 1.PHP文件缓存内容保存格式 PHP文件缓存内容保存格式主要有三种: (1)变量 var_export 格式化成PHP正常的赋值书写格式: (2)变量 serialize 序列化之后保存,用的时候反序列化: (3)变量 json_encode格式化之后保存,用的时候json_decode 互联网上测试结果是:serialize格式的文件解析效率大于Json,Json的解析

读jQuery之六 缓存数据功能介绍_jquery

很多同学在项目中都喜欢将数据存储在HTMLElement属性上,如 复制代码 代码如下: <div data="some data">Test</div> <script> div.getAttribute('data'); // some data </script> 给页面中div添加了自定义属性"data"及值"some data".后续JS代码中使用getAttribute获取. jQuer

php cache类代码(php数据缓存类)_php技巧

如果访问量大的话会给数据库造成很大的负担,所以对于变化不经常的内容要做好php 数据cache(缓存)是十分必要的,我做了一个简单的php"文件缓存"的类,希望对大家有所帮助. 思路是这样的: 对于一般的变量,把该变量变成php语言的格式,写到文件中,用时只要include这个文件就相当于加载了cache了: 对于array型的变量,把array转化为php语言定义array的字符串,写到文件中,用时也只要include就相当于加载了cache了: 缓存cache时间上的控制,通过获取

功能强大的PHP POST提交数据类_php技巧

本文实例为大家分享了PHP功能强大的 POST提交数据类,供大家参考,具体内容如下 <?php class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_POST, 1); if($post_data != ''){

解析PHP的Yii框架中cookie和session功能的相关操作_php技巧

Sessions 和 请求 和 响应类似, 默认可通过为yii\web\Session 实例的session 应用组件 来访问sessions. 开启和关闭 Sessions 可使用以下代码来开启和关闭session. $session = Yii::$app->session; // 检查session是否开启 if ($session->isActive) ... // 开启session $session->open(); // 关闭session $session->clo