Доброго времени суток.
Возникла потребность в кешировании, как можно упростить данных клас и ускорить его работу?
PHP код:
<?php
Class DF_cache{
public static function Create(){
static $instance;
if($instance==null){
$instance = new DF_cache;
}
return $instance;
}
private $_data;
public function SetData($data){
$this->_data = $data;
return $this;
}
private $_resource;
public function SetResource($resource){
$this->_resource = $resource;
return $this;
}
private $_type;
public function SetType($type){
$this->_type = $type;
return $this;
}
private $_name;
public function SetName($name){
$this->_name = $name;
return $this;
}
private $_Serialize = 'true';
public function Serialize($stat = 'true'){
$this->_Serialize = $stat;
return $this;
}
public function Get(){
$data = NULL;
Switch($this->_resource)
{
case('Page'):
$file = CACHE_PATH.'/page_'.$this->_name.'.tmp';
if($this->Check($file)==1){
$data = file_get_contents($file);
}
break;
case('Config'):
Switch($this->_type)
{
case('Session'):
if(isset($_SESSION[$this->_name])){
$data = $_SESSION[$this->_name];
}
break;
case('File'):
$file = CACHE_PATH.'/config_'.$this->_name.'.tmp';
if($this->Check($file)==1){
switch($this->_Serialize){
case('true'):
$data = unserialize(file_get_contents($file));
break;
case('false'):
$data = file_get_contents($file);
break;
}
}
break;
}
break;
case('Object');
$file = CACHE_PATH.'/object_'.$this->_name.'.tmp';
if($this->Check($file)==1){
switch($this->_Serialize){
case('true'):
$data = unserialize(file_get_contents($file));
break;
case('false'):
$data = file_get_contents($file);
break;
}
}
break;
}
$this->_name = NULL;
$this->_data = NULL;
$this->_type = NULL;
$this->_resource = NULL;
return $data;
}
public function Save(){
$data = NULL;
Switch($this->_resource)
{
case('Page'):
file_put_contents(CACHE_PATH.'/page_'.$this->_name.'.tmp', $this->_data);
break;
case('Config'):
Switch($this->_type)
{
case('Session'):
$_SESSION[$this->_name] = $this->_data;
break;
case('File'):
file_put_contents(CACHE_PATH.'/config_'.$this->_name.'.tmp', serialize($this->_data));
break;
}
break;
case('Object');
file_put_contents(CACHE_PATH.'/object_'.$this->_name.'.tmp', serialize($this->_data));
break;
}
$this->_name = NULL;
$this->_data = NULL;
$this->_type = NULL;
$this->_resource = NULL;
return $data;
}
private $_time;
public function TimeOut($time){
$this->_time = $time;
return $this;
}
private function Check($file){
if(file_exists($file)){
$mtime = filemtime($file);
$time = time()-$mtime;
$minut = $time/60;
$timeout = $this->_time;
if($minut>=$timeout){
unlink($file);
return 2;
}else{
return 1;
}
return 1;
}else{
return 2;
}
}
}
Пример использования:
PHP код:
$cache = DF_cache::Create();
$cache->SetResource('Object');
$cache->SetName("page1");
$result = $cache->Get();
if(empty($result)){
result = ...// тут какой то код на получение данных
$this->cache->SetResource('Object');
$this->cache->SetName("page1");
$this->cache->SetData($result);
$this->cache->Save();
}
Данный клас работает очень долго, около 0.2 с ((((