/**
* Author: decade
* qq:345610000
*/
Class Mypdo extends PDO
{
Public $cache _dir = null; Cache Directory
Public $cache _expiretime = 7200; Cache time, default two hours
Queries with a cache
Public Function Cquery ($sql)
{
Cache Storage Total Directory
if ($this->cache_dir = = NULL | |!is_dir ($this->cache_dir)) {
Exit ("The cache directory is wrong!") ");
} else {
$this->cache_dir = str_replace ("\", "/", $this->cache_dir);
$FileName = Trim ($this->cache_dir, "/"). '/' . UrlEncode (Trim ($sql)). '. SQL ';
}
Determine the build cache
if (!file_exists ($FileName) | | time ()-Filemtime ($FileName) > $this->cache_expiretime) {
if ($tmpRS = Parent::query ($sql)) {
$data = Serialize ($tmpRS->fetchall ());
Self::createfile ($FileName, $data);
} else {
Exit ("SQL syntax error
");
}
}
return $this->readcache ($FileName);
}
Read Cache file
private static function Readcache ($FilePath)
{
if (Is_file ($FilePath) && $Data = file_get_contents ($FilePath)) {
return new Cache_pdostatement (Unserialize ($Data));
}
return false;
}
Generating files
public static function CreateFile ($FilePath, $Data = ")
{
if (File_put_contents ($FilePath, $Data)) {
return true;
} else {
return false;
}
}
}
Cache is used in the statement class
Class Cache_pdostatement
{
Private $RECORDARR = Array ();
Private $cursorId = 0;
Private $recordCount = 0;
Public function __construct ($arr)
{
$this->recordarr = $arr;
$this->recordcount = count ($arr);
}
Returns a record with the pointer moving down one line
Public function Fetch ()
{
if ($this->cursorid = = $this->recordcount) {
return false;
} else if ($this->cursorid = = 0) {
$this->cursorid++;
Return current ($this->recordarr);
} else {
$this->cursorid++;
Return next ($this->recordarr);
}
}
Return all results
Public Function Fetchall ()
{
return $this->recordarr;
}
Single-row query
Public Function Fetchcolumn ()
{
$TMPARR = current ($this->recordarr);
return $TMPARR [0];
}
}
How to use
$db = new Mypdo (' Mysql:host = Localhost;dbname=news ', ' newsadmin ', ' 123456 ');
$db->cache_dir = "cache"; Set the cache directory
$db->cache_expiretime = 7200; Set Cache time
$rs = $db->cquery ("SELECT * FROM News limit 0,10"); Using cache Query method Cquery instead of query
while ($row = $rs->fetch ()) {
echo $row ["F_title"]. "
";
}
$rs = null;
$DB = null;
http://www.bkjia.com/PHPjc/630439.html www.bkjia.com true http://www.bkjia.com/PHPjc/630439.html techarticle /** * Author: Decade * qq:345610000 */class Mypdo extends PDO {public $cache _dir = null;//cache directory public $cache _expiretime = 72 00; Cache time, default two hours//slow ...