• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/service/cacheService.php

00001 <?php
00025 class cacheService
00026 {  
00027     public    $m_Cache = "Disbaled";
00028     public    $m_CacheEngine = "File";
00029 
00030     protected $m_CacheOptions = array();
00031     protected $m_CacheEngineOptions  = array();
00032     protected $m_CacheObj = null;
00033 
00040     function __construct(&$xmlArr)
00041     {
00042         $this->readMetadata($xmlArr);
00043     }
00044 
00050     public function destroy()
00051     {
00052         //$this->m_Cache = null;
00053         //$this->m_CacheEngine = null;
00054         //$this->m_CacheObj = null;
00055         //$this->m_CacheOptions = null;
00056         //$this->m_CacheEngineOptions = null;
00057     }
00058 
00065     protected function readMetadata(&$xmlArr)
00066     {
00067         $this->m_Cache      = isset($xmlArr["PLUGINSERVICE"]["CACHESETTING"]["ATTRIBUTES"]["MODE"]) ? $xmlArr["PLUGINSERVICE"]["CACHESETTING"]["ATTRIBUTES"]["MODE"] : "Enabled";
00068         $this->m_CacheEngine = isset($xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["ATTRIBUTES"]["TYPE"]) ? $xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["ATTRIBUTES"]["TYPE"] : "FileCache";
00069         // process Cache settings
00070         if(strtoupper($this->m_Cache)=="ENABLED")
00071         {
00072             $this->_loadConfig($xmlArr["PLUGINSERVICE"]["CACHESETTING"]["CONFIG"],$this->m_CacheOptions);
00073         }
00074         switch(strtoupper($this->m_CacheEngine))
00075         {
00076             case "FILE":
00077                 $this->_loadConfig($xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["FILE"]["CONFIG"],$this->m_CacheEngineOptions);
00078                 //no break there , because all other engine is inherit from FileCache
00079                 break;
00080 
00081             case "SQLITE":
00082                 $this->_loadConfig($xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["SQLITE"]["CONFIG"],$this->m_CacheEngineOptions);
00083                 break;
00084 
00085             case "MEMCACHED":
00086                 $this->_loadConfig($xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["MEMCACHED"]["CONFIG"],$this->m_CacheEngineOptions);
00087                 break;
00088 
00089             case "XCACHE":
00090                 $this->_loadConfig($xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["XCACHE"]["CONFIG"],$this->m_CacheEngineOptions);
00091                 break;
00092 
00093             case "APC":
00094                 $this->_loadConfig($xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["APC"]["CONFIG"],$this->m_CacheEngineOptions);
00095                 break;
00096 
00097             case "ZENDPLATFORM":
00098                 $this->_loadConfig($xmlArr["PLUGINSERVICE"]["CACHEENGINE"]["ZENDPLATFORM"]["CONFIG"],$this->m_CacheEngineOptions);
00099                 break;
00100 
00101         }
00102     }
00103 
00111     private function _loadConfig(&$configs, &$options)
00112     {
00113         foreach($configs as $config)
00114         {
00115             if(strtoupper($config["ATTRIBUTES"]["VALUE"])=="Y")
00116             {
00117                 $config["ATTRIBUTES"]["VALUE"]=true;
00118             }elseif(strtoupper($config["ATTRIBUTES"]["VALUE"])=="N")
00119             {
00120                 $config["ATTRIBUTES"]["VALUE"]=false;
00121             }
00122             $options[$config["ATTRIBUTES"]["NAME"]] = $config["ATTRIBUTES"]["VALUE"];
00123             if($config["ATTRIBUTES"]["NAME"]=='cache_dir')
00124             {
00125                 $options[$config["ATTRIBUTES"]["NAME"]] = CACHE_PATH."/".$config["ATTRIBUTES"]["VALUE"];
00126             }
00127         }
00128     }
00129 
00137     public function init($objName = "", $lifeTime = 0)
00138     {
00139         if(strtoupper($this->m_Cache)=="ENABLED")
00140         {
00141             if(strtoupper($this->m_CacheEngine)=="FILE" && $objName!="" )
00142             {
00143                 $objfolder=str_replace(".","/",$objName)."/";
00144                 $objfolder=str_replace(array(':',' '),'_',$objfolder);
00145                 if(!strpos($this->m_CacheEngineOptions['cache_dir'],$objfolder))
00146                 {
00147                     $this->m_CacheEngineOptions['cache_dir'].=$objfolder;
00148                 }
00149             }
00150 
00151             if (!file_exists($this->m_CacheEngineOptions['cache_dir']))
00152             {
00153                 //mkdir($this->m_CacheEngineOptions['cache_dir'], 0777, true);
00154                 $this->_makeDirectory($this->m_CacheEngineOptions['cache_dir'], 0777);
00155             }
00156 
00157             $this->m_CacheOptions['automatic_serialization']=true;
00158 
00159             if((int)$lifeTime>0)
00160             {
00161                 $this->m_CacheOptions['lifetime']=(int)$lifeTime;
00162             }
00163             require_once 'Zend/Cache.php';
00164             $this->m_CacheObj = Zend_Cache::factory('Core',
00165                     $this->m_CacheEngine,
00166                     $this->m_CacheOptions,
00167                     $this->m_CacheEngineOptions);
00168             return $this->m_CacheObj;
00169         }
00170         else
00171         {
00172             return false;
00173         }
00174     }
00175 
00183     public function save($data, $id)
00184     {
00185         if($this->m_CacheObj && strtoupper($this->m_Cache)=="ENABLED" )
00186         {
00187             return $this->m_CacheObj->save($data, $id);
00188         }
00189         else
00190         {
00191             return false;
00192         }
00193     }
00194 
00201     public function load($id)
00202     {
00203         if($this->m_CacheObj && strtoupper($this->m_Cache)=="ENABLED")
00204         {
00205             return $this->m_CacheObj->load($id);
00206         }else
00207         {
00208             return false;
00209         }
00210     }
00211 
00218     public function test($id)
00219     {
00220         if($this->m_CacheObj && strtoupper($this->m_Cache)=="ENABLED")
00221         {
00222             return $this->m_CacheObj->test($id);
00223         }else
00224         {
00225             return false;
00226         }
00227     }
00228 
00235     public function remove($id)
00236     {
00237         if($this->m_CacheObj && strtoupper($this->m_Cache)=="ENABLED")
00238         {
00239             return $this->m_CacheObj->remove($id);
00240         }
00241         else
00242         {
00243             return false;
00244         }
00245     }
00246      
00252     public function getIds()
00253     {
00254         if($this->m_CacheObj && strtoupper($this->m_Cache)=="ENABLED")
00255         {
00256             return $this->m_CacheObj->getIds();
00257         }
00258         else
00259         {
00260             return false;
00261         }
00262     }    
00263 
00269     public function cleanAll()
00270     {
00271         if($this->m_CacheObj && strtoupper($this->m_Cache)=="ENABLED")
00272         {
00273             return $this->m_CacheObj->clean(Zend_Cache::CLEANING_MODE_ALL);
00274         }
00275         else
00276         {
00277             return false;
00278         }
00279     }
00280 
00286     public function cleanExpired()
00287     {
00288         if($this->m_CacheObj && strtoupper($this->m_Cache) == "ENABLED")
00289         {
00290             return $this->m_CacheObj->clean(Zend_Cache::CLEANING_MODE_OLD);
00291         }
00292         else
00293         {
00294             return false;
00295         }
00296     }
00297 
00298 
00310     private function _makeDirectory($pathName, $mode)
00311     {
00312         is_dir(dirname($pathName)) || $this->_makeDirectory(dirname($pathName), $mode);
00313         return is_dir($pathName) || @mkdir($pathName, $mode);
00314     }
00315 }
00316 
00317 ?>

Generated on Thu Apr 19 2012 17:09:13 for openbiz by  doxygen 1.7.2