Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Скриптовые языки программирования > PHP
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 30.10.2012, 11:10   #1
dem66
Форумчанин
 
Регистрация: 31.05.2011
Сообщений: 316
Восклицание Клас кэширования страниц, конфигов и объектов

Доброго времени суток.
Возникла потребность в кешировании, как можно упростить данных клас и ускорить его работу?

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 с ((((
dem66 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Указатель на базовый клас OTAMAH Общие вопросы C/C++ 5 22.04.2012 08:08
Перехватчик кэширования misher Работа с сетью в Delphi 0 11.12.2010 23:17
Простенький клас Chartvit Общие вопросы C/C++ 2 07.01.2010 20:43
как избежать кэширования в Опере rolling PHP 3 01.12.2009 18:34