Added apc cache for config files

Loggin improvements
This commit is contained in:
RainLoop Team 2014-10-14 15:04:11 +04:00
parent ef7f8bddb3
commit 0af6fbadaa
5 changed files with 149 additions and 18 deletions

View file

@ -14,22 +14,30 @@ abstract class AbstractConfig
*/
private $aData;
/**
* @var bool
*/
private $bUseApcCache;
/**
* @var string
*/
protected $sFilePrefix;
private $sFileHeader;
/**
* @param string $sFileName
* @param string $sFilePrefix = ''
* @param string $sFileHeader = ''
*
* @return void
*/
public function __construct($sFileName, $sFilePrefix = '')
public function __construct($sFileName, $sFileHeader = '')
{
$this->sFile = \APP_PRIVATE_DATA.'configs/'.$sFileName;
$this->sFilePrefix = $sFilePrefix;
$this->sFileHeader = $sFileHeader;
$this->aData = $this->defaultValues();
$this->bUseApcCache = APP_USE_APC_CACHE &&
\MailSo\Base\Utils::FunctionExistsAndEnabled(array('apc_fetch', 'apc_store'));
}
/**
@ -42,7 +50,7 @@ abstract class AbstractConfig
*/
public function IsInited()
{
return is_array($this->aData) && 0 < count($this->aData);
return \is_array($this->aData) && 0 < \count($this->aData);
}
/**
@ -73,7 +81,7 @@ abstract class AbstractConfig
{
if (isset($this->aData[$sSectionKey][$sParamKey][0]))
{
$sType = gettype($this->aData[$sSectionKey][$sParamKey][0]);
$sType = \gettype($this->aData[$sSectionKey][$sParamKey][0]);
switch ($sType)
{
default:
@ -96,6 +104,82 @@ abstract class AbstractConfig
}
}
/**
* @return string
*/
private function cacheKey()
{
return 'config:'.\sha1($this->sFile).':';
}
/**
* @return bool
*/
private function loadDataFromCache()
{
if ($this->bUseApcCache)
{
$iMTime = @\filemtime($this->sFile);
if (\is_int($iMTime) && 0 < $iMTime)
{
$sKey = $this->cacheKey();
$iTime = \apc_fetch($sKey.'time');
if ($iTime && $iMTime === (int) $iTime)
{
$aFetchData = \apc_fetch($sKey.'data');
if (\is_array($aFetchData))
{
$this->aData = $aFetchData;
return true;
}
}
}
}
return false;
}
/**
* @return bool
*/
private function storeDataToCache()
{
if ($this->bUseApcCache)
{
$iMTime = @\filemtime($this->sFile);
if (\is_int($iMTime) && 0 < $iMTime)
{
$sKey = $this->cacheKey();
\apc_store($sKey.'time', $iMTime);
\apc_store($sKey.'data', $this->aData);
return true;
}
}
return false;
}
/**
* @return bool
*/
private function clearCache()
{
if ($this->bUseApcCache)
{
$sKey = $this->cacheKey();
\apc_delete($sKey.'time');
\apc_delete($sKey.'data');
return true;
}
return false;
}
/**
* @return bool
*/
@ -103,6 +187,11 @@ abstract class AbstractConfig
{
if (\file_exists($this->sFile) && \is_readable($this->sFile))
{
if ($this->loadDataFromCache())
{
return true;
}
$aData = @\parse_ini_file($this->sFile, true);
if (\is_array($aData) && 0 < count($aData))
{
@ -117,6 +206,8 @@ abstract class AbstractConfig
}
}
$this->storeDataToCache();
return true;
}
}
@ -191,8 +282,9 @@ abstract class AbstractConfig
}
}
$this->clearCache();
return false !== \file_put_contents($this->sFile,
(0 < \strlen($this->sFilePrefix) ? $this->sFilePrefix : '').
(0 < \strlen($this->sFileHeader) ? $this->sFileHeader : '').
$sNewLine.\implode($sNewLine, $aResultLines));
}
}