PDO Support data Caching _php tutorial

Source: Internet
Author: User
/**
* 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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.