Cache refactoring

Contacts settings fixes
This commit is contained in:
RainLoop Team 2015-02-24 20:26:33 +04:00
parent 35ce0be771
commit e1ab11a9b0
8 changed files with 326 additions and 247 deletions

View file

@ -1,85 +1,107 @@
<?php
/*
* This file is part of MailSo.
*
* (c) 2014 Usenko Timur
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MailSo\Cache\Drivers;
/**
* @category MailSo
* @package Cache
* @subpackage Drivers
*/
class APC implements \MailSo\Cache\DriverInterface
{
/**
* @return \MailSo\Cache\Drivers\APC
*/
public static function NewInstance()
{
return new self();
}
/**
* @param string $sKey
* @param string $sValue
*
* @return bool
*/
public function Set($sKey, $sValue)
{
return \apc_store($this->generateCachedKey($sKey), (string) $sValue);
}
/**
* @param string $sKey
*
* @return string
*/
public function Get($sKey)
{
$sValue = \apc_fetch($this->generateCachedKey($sKey));
return \is_string($sValue) ? $sValue : '';
}
/**
* @param string $sKey
*
* @return void
*/
public function Delete($sKey)
{
\apc_delete($this->generateCachedKey($sKey));
}
/**
* @param int $iTimeToClearInHours = 24
*
* @return bool
*/
public function GC($iTimeToClearInHours = 24)
{
if (0 === $iTimeToClearInHours)
{
return \apc_clear_cache('user');
}
return false;
}
/**
* @param string $sKey
*
* @return string
*/
private function generateCachedKey($sKey)
{
return \sha1($sKey);
}
}
<?php
/*
* This file is part of MailSo.
*
* (c) 2014 Usenko Timur
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MailSo\Cache\Drivers;
/**
* @category MailSo
* @package Cache
* @subpackage Drivers
*/
class APC implements \MailSo\Cache\DriverInterface
{
/**
* @var string
*/
private $sKeyPrefix;
/**
* @access private
*
* @param string $sKeyPrefix = ''
*/
private function __construct($sKeyPrefix = '')
{
$this->sKeyPrefix = $sKeyPrefix;
if (!empty($this->sKeyPrefix))
{
$this->sKeyPrefix =
\preg_replace('/[^a-zA-Z0-9_]/', '_', rtrim(trim($this->sKeyPrefix), '\\/')).'/';
}
}
/**
* @param string $sKeyPrefix = ''
*
* @return \MailSo\Cache\Drivers\APC
*/
public static function NewInstance($sKeyPrefix = '')
{
return new self($sKeyPrefix);
}
/**
* @param string $sKey
* @param string $sValue
*
* @return bool
*/
public function Set($sKey, $sValue)
{
return \apc_store($this->generateCachedKey($sKey), (string) $sValue);
}
/**
* @param string $sKey
*
* @return string
*/
public function Get($sKey)
{
$sValue = \apc_fetch($this->generateCachedKey($sKey));
return \is_string($sValue) ? $sValue : '';
}
/**
* @param string $sKey
*
* @return void
*/
public function Delete($sKey)
{
\apc_delete($this->generateCachedKey($sKey));
}
/**
* @param int $iTimeToClearInHours = 24
*
* @return bool
*/
public function GC($iTimeToClearInHours = 24)
{
if (0 === $iTimeToClearInHours)
{
return \apc_clear_cache('user');
}
return false;
}
/**
* @param string $sKey
*
* @return string
*/
private function generateCachedKey($sKey)
{
return $this->sKeyPrefix.\sha1($sKey);
}
}

View file

@ -23,29 +23,43 @@ class File implements \MailSo\Cache\DriverInterface
*/
private $sCacheFolder;
/**
* @var string
*/
private $sKeyPrefix;
/**
* @access private
*
* @param string $sCacheFolder
* @param string $sKeyPrefix = ''
*/
private function __construct($sCacheFolder)
private function __construct($sCacheFolder, $sKeyPrefix = '')
{
$this->sCacheFolder = $sCacheFolder;
$this->sCacheFolder = rtrim(trim($this->sCacheFolder), '\\/').'/';
if (!\is_dir($this->sCacheFolder))
$this->sKeyPrefix = $sKeyPrefix;
if (!empty($this->sKeyPrefix))
{
@\mkdir($this->sCacheFolder, 0755);
$this->sKeyPrefix = \str_pad(\preg_replace('/[^a-zA-Z0-9_]/', '_',
rtrim(trim($this->sKeyPrefix), '\\/')), 5, '_');
$this->sKeyPrefix = '__/'.
\substr($this->sKeyPrefix, 0, 2).'/'.\substr($this->sKeyPrefix, 2, 2).'/'.
$this->sKeyPrefix.'/';
}
}
/**
* @param string $sCacheFolder
* @param string $sKeyPrefix = ''
*
* @return \MailSo\Cache\Drivers\File
*/
public static function NewInstance($sCacheFolder)
public static function NewInstance($sCacheFolder, $sKeyPrefix = '')
{
return new self($sCacheFolder);
return new self($sCacheFolder, $sKeyPrefix);
}
/**
@ -121,7 +135,7 @@ class File implements \MailSo\Cache\DriverInterface
$sKeyPath = \sha1($sKey);
$sKeyPath = \substr($sKeyPath, 0, 2).'/'.\substr($sKeyPath, 2, 2).'/'.$sKeyPath;
$sFilePath = $this->sCacheFolder.$sKeyPath;
$sFilePath = $this->sCacheFolder.$this->sKeyPrefix.$sKeyPath;
if ($bMkDir && !\is_dir(\dirname($sFilePath)))
{
if (!\mkdir(\dirname($sFilePath), 0755, true))

View file

@ -1,129 +1,144 @@
<?php
/*
* This file is part of MailSo.
*
* (c) 2014 Usenko Timur
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MailSo\Cache\Drivers;
/**
* @category MailSo
* @package Cache
* @subpackage Drivers
*/
class Memcache implements \MailSo\Cache\DriverInterface
{
/**
* @var string
*/
private $sHost;
/**
* @var int
*/
private $iPost;
/**
* @var int
*/
private $iExpire;
/**
* @var \Memcache|null
*/
private $oMem;
/**
* @param string $sHost = '127.0.0.1'
* @param int $iPost = 11211
* @param int $iExpire = 43200
*/
private function __construct($sHost = '127.0.0.1', $iPost = 11211, $iExpire = 43200)
{
$this->sHost = $sHost;
$this->iPost = $iPost;
$this->iExpire = 0 < $iExpire ? $iExpire : 43200;
$this->oMem = new \Memcache();
if (!$this->oMem->connect($this->sHost, $this->iPost))
{
$this->oMem = null;
}
}
/**
* @param string $sHost = '127.0.0.1'
* @param int $iPost = 11211
*
* @return \MailSo\Cache\Drivers\APC
*/
public static function NewInstance($sHost = '127.0.0.1', $iPost = 11211)
{
return new self($sHost, $iPost);
}
/**
* @param string $sKey
* @param string $sValue
*
* @return bool
*/
public function Set($sKey, $sValue)
{
return $this->oMem ? $this->oMem->set($this->generateCachedKey($sKey), $sValue, 0, $this->iExpire) : false;
}
/**
* @param string $sKey
*
* @return string
*/
public function Get($sKey)
{
$sValue = $this->oMem ? $this->oMem->get($this->generateCachedKey($sKey)) : '';
return \is_string($sValue) ? $sValue : '';
}
/**
* @param string $sKey
*
* @return void
*/
public function Delete($sKey)
{
if ($this->oMem)
{
$this->oMem->delete($this->generateCachedKey($sKey));
}
}
/**
* @param int $iTimeToClearInHours = 24
*
* @return bool
*/
public function GC($iTimeToClearInHours = 24)
{
if (0 === $iTimeToClearInHours && $this->oMem)
{
return $this->oMem->flush();
}
return false;
}
/**
* @param string $sKey
*
* @return string
*/
private function generateCachedKey($sKey)
{
return \sha1($sKey);
}
}
<?php
/*
* This file is part of MailSo.
*
* (c) 2014 Usenko Timur
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MailSo\Cache\Drivers;
/**
* @category MailSo
* @package Cache
* @subpackage Drivers
*/
class Memcache implements \MailSo\Cache\DriverInterface
{
/**
* @var string
*/
private $sHost;
/**
* @var int
*/
private $iPost;
/**
* @var int
*/
private $iExpire;
/**
* @var \Memcache|null
*/
private $oMem;
/**
* @var string
*/
private $sKeyPrefix;
/**
* @param string $sHost = '127.0.0.1'
* @param int $iPost = 11211
* @param int $iExpire = 43200
* @param string $sKeyPrefix = ''
*/
private function __construct($sHost = '127.0.0.1', $iPost = 11211, $iExpire = 43200, $sKeyPrefix = '')
{
$this->sHost = $sHost;
$this->iPost = $iPost;
$this->iExpire = 0 < $iExpire ? $iExpire : 43200;
$this->oMem = new \Memcache();
if (!$this->oMem->connect($this->sHost, $this->iPost))
{
$this->oMem = null;
}
$this->sKeyPrefix = $sKeyPrefix;
if (!empty($this->sKeyPrefix))
{
$this->sKeyPrefix =
\preg_replace('/[^a-zA-Z0-9_]/', '_', rtrim(trim($this->sKeyPrefix), '\\/')).'/';
}
}
/**
* @param string $sHost = '127.0.0.1'
* @param int $iPost = 11211
* @param int $iExpire = 43200
* @param string $sKeyPrefix = ''
*
* @return \MailSo\Cache\Drivers\APC
*/
public static function NewInstance($sHost = '127.0.0.1', $iPost = 11211, $iExpire = 43200, $sKeyPrefix = '')
{
return new self($sHost, $iPost, $iExpire, $sKeyPrefix);
}
/**
* @param string $sKey
* @param string $sValue
*
* @return bool
*/
public function Set($sKey, $sValue)
{
return $this->oMem ? $this->oMem->set($this->generateCachedKey($sKey), $sValue, 0, $this->iExpire) : false;
}
/**
* @param string $sKey
*
* @return string
*/
public function Get($sKey)
{
$sValue = $this->oMem ? $this->oMem->get($this->generateCachedKey($sKey)) : '';
return \is_string($sValue) ? $sValue : '';
}
/**
* @param string $sKey
*
* @return void
*/
public function Delete($sKey)
{
if ($this->oMem)
{
$this->oMem->delete($this->generateCachedKey($sKey));
}
}
/**
* @param int $iTimeToClearInHours = 24
*
* @return bool
*/
public function GC($iTimeToClearInHours = 24)
{
if (0 === $iTimeToClearInHours && $this->oMem)
{
return $this->oMem->flush();
}
return false;
}
/**
* @param string $sKey
*
* @return string
*/
private function generateCachedKey($sKey)
{
return $this->sKeyPrefix.\sha1($sKey);
}
}