PHP Classes

File: chache_class.php

Recommend this page to a friend!
  Classes of Pashkov Denis Igorevich   MySQL cache class   chache_class.php   Download  
File: chache_class.php
Role: Class source
Content type: text/plain
Description: cache class
Class: MySQL cache class
Cache MySQL query results in session variables
Author: By
Last change: now possible return as object
Date: 13 years ago
Size: 4,759 bytes
 

Contents

Class file image Download
<?php

// Кэширование MySQL запросов посредством сериализации, добавлен страховочный метод на случай если сервер mysql будет не доступен!?// Caching , return from alert cahce if mysql server FAILD;
// BY server@2010 pashkovdenis@gmail.com
class cacher {
    private
$cahcedir;
    private
$cahcetime;
    private
$sqlhadler;
    private
$maxmemoryusage;
    private
$userescape;
     private
$handlinm;
    private
$dir;
     private
$hdl;
    private
$flag;
    private
$cutime;
    private
$usecachesystem;


function
__construct($server=array("localhost","root","","base"),$useescape=true,$cp="cp1251",$time=60,$maxmermo=2024){
       
session_id()==false?session_start():null;
       
$this->handlinm=0;
         !empty(
$server[0])?$this->handling_db($server):null;
       
$cp1251=true?@mysql_query("SET NAMES {$cp}"):null;
       
$this->cahcetime=$time;
       
$this->maxmemoryusage=$maxmermo;
       
$this->userescape=$useescape;
       
$this->dir="work_dumps/";
       
$this->cutime=strftime("%J-%H",time());


   


       
//echo $this->cutime;
 
}




private function
handling_db($server){

if(
$this->sqlhadler=@mysql_connect($server[0],$server[1],$server[2])){
        @
mysql_select_db($server[3]); }else{
           
$this->flag='stop';
           
//throw new Exception("Msql Faild");
}
$this->handlinm = 1;


}




private function
bindArrayToObject($array) {

$return = new stdClass();

foreach (
$array as $k => $v) {

if (
is_array($v)) {

$return->$k = $this->bindArrayToObject($v);

}

else {

$return->{$k} = $v;

}

}
return
$return;
}















public function
getpanic($sql){
   
$sql_R='';
   
$result=array();


if(
file_exists($this->dir.substr($sql,0,50).$this->cutime.$this->cutime.".dmps") && strftime("%Y %m %d",filectime($this->dir.substr($sql,0,50).$this->cutime.".dmps"))!==date("Y m d")) $this->clear();


if (
$sql_R=@mysql_query($sql)){

while ($
$sql_R=@mysql_fetch_array($sql_R)){
       
$result[]=$$sql_R;
}
if(!
file_exists($this->dir.substr($sql,0,50).$this->cutime.$this->cutime.".dmps"))
file_put_contents($this->dir.substr($sql,0,50).$this->cutime.".dmps",serialize($result));
return
$result;
////////////

}elseif(file_exists($this->dir.substr($sql,0,50).$this->cutime.".dmps")){

return
unserialize(file_get_contents($this->dir.substr($sql,0,50).$this->cutime.".dmps"));
}else{

throw new
exception("Faild");
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public function get($mysql_req,$obj=null){
    if(
$this->flag=='stop') return ;
   
$rest_array=array();
$sql=$mysql_req;

//0637766991

   
if(isset($_SESSION["<sqlc>".$mysql_req]) AND strftime("%M",strtotime(date("h:i:0")))-strftime("%M",$_SESSION[$mysql_req."time"] ) <$this->cahcetime ){

       
$rest=unserialize($_SESSION["<sqlc>".$mysql_req]);
       
//$rest=array_walk($rest,'expld');
          

   
if($obj==1){
    return
$this->bindArrayToObject($rest);
        }else return
$rest;




    }else
    {

        if(
file_exists($this->dir.substr($sql,0,50).$this->cutime.".dmps")){
 
                
                    if(
$obj==1){
    return
$this->bindArrayToObject(unserialize(file_get_contents($this->dir.substr($sql,0,50).$this->cutime.".dmps")));
        }else return
unserialize(file_get_contents($this->dir.substr($sql,0,50).$this->cutime.".dmps"));


            }else{

       


   
$sql= $this->userescape & $this->handlinm ?mysql_real_escape_string($mysql_req):$mysql_req;
    if(
$ret=mysql_query($sql)){
    while(
$reslt=mysql_fetch_array($ret)) $rest_array[]= $reslt;
   
$_SESSION["<sqlc>".$mysql_req]= serialize($rest_array);
   
$_SESSION[$mysql_req."time"] = strtotime(date("h:i:00"));
   
round(memory_get_usage()/1024) >= $this->maxmemoryusage ? $this->clean():null;
   
mysql_free_result($ret) ;
   
file_put_contents($this->dir.substr($sql,0,50).$this->cutime.".dmps",serialize($rest_array));

   
        if(
$obj==1){
    return
$this->bindArrayToObject($rest_array);
        }else return
$rest_array;





    }else
    {
    return
false;
    }
    }
    }




}



public static function
excpth($e){
echo
"<h3 style='color:red;'> Warning New Exception :(( </h3>";
    echo
"<h3> {$e->getMessage() } </h3>";
    echo
"<h3> {$e->getLine() } </h3>";
/////////////////////////////////////////////........

}




public function
sql($mysql_req){
$sql= $this->userescape & $this->handlinm?mysql_real_escape_string($mysql_req):$mysql_req;
if(
$reslt=mysql_query($sql)){
return
$reslt;
}else
{
return
false;
}

}



public function
clean(){

$f=scandir($this->dir);
foreach(
$f as $k){
strstr($k,".dmps")?unlink($this->dir.$k):null;
}

    while(list(
$key,$value)=each($_SESSION)){
        if(
strstr($value,"<sqlc>")){
        unset(
$_SESSION[$key]);}
        }
@
mysql_close($this->sqlhadler);
}
}
 
set_exception_handler("cacher::excpth");








 

?>