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

@ -1315,6 +1315,7 @@
require('Stores/User/Settings').populate(); require('Stores/User/Settings').populate();
require('Stores/User/Notification').populate(); require('Stores/User/Notification').populate();
require('Stores/User/Account').populate(); require('Stores/User/Account').populate();
require('Stores/User/Contact').populate();
var var
self = this, self = this,

View file

@ -122,7 +122,7 @@
*/ */
AttachmentModel.prototype.isText = function () AttachmentModel.prototype.isText = function ()
{ {
return -1 < Utils.inArray(this.mimeType, ['application/pgp-signature']) || return -1 < Utils.inArray(this.mimeType, ['application/pgp-signature', 'message/delivery-status', 'message/rfc822']) ||
('text/' === this.mimeType.substr(0, 5) && -1 === Utils.inArray(this.mimeType, ['text/html'])); ('text/' === this.mimeType.substr(0, 5) && -1 === Utils.inArray(this.mimeType, ['text/html']));
}; };
@ -316,6 +316,12 @@
{ {
sClass = 'icon-file-certificate'; sClass = 'icon-file-certificate';
} }
else if (-1 < Utils.inArray(sMimeType, [
'message/delivery-status', 'message/rfc822'
]))
{
sClass = 'icon-file-text';
}
else if (-1 < Utils.inArray(aParts[1], [ else if (-1 < Utils.inArray(aParts[1], [
'rtf', 'msword', 'vnd.msword', 'vnd.openxmlformats-officedocument.wordprocessingml.document', 'rtf', 'msword', 'vnd.msword', 'vnd.openxmlformats-officedocument.wordprocessingml.document',
'vnd.openxmlformats-officedocument.wordprocessingml.template', 'vnd.openxmlformats-officedocument.wordprocessingml.template',

View file

@ -2,7 +2,7 @@
"name": "RainLoop", "name": "RainLoop",
"title": "RainLoop Webmail", "title": "RainLoop Webmail",
"version": "1.8.1", "version": "1.8.1",
"release": "271", "release": "272",
"description": "Simple, modern & fast web-based email client", "description": "Simple, modern & fast web-based email client",
"homepage": "http://rainloop.net", "homepage": "http://rainloop.net",
"main": "gulpfile.js", "main": "gulpfile.js",

View file

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

View file

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

View file

@ -508,8 +508,9 @@ abstract class NetClient
} }
else else
{ {
// $this->writeLog('Stream Meta: '. $this->writeLog('Stream Meta: '.
// \print_r($aSocketStatus, true), \MailSo\Log\Enumerations\Type::ERROR); \print_r($aSocketStatus, true), \MailSo\Log\Enumerations\Type::ERROR);
$this->writeLogException( $this->writeLogException(
new Exceptions\SocketReadException(), new Exceptions\SocketReadException(),
\MailSo\Log\Enumerations\Type::ERROR, true); \MailSo\Log\Enumerations\Type::ERROR, true);

View file

@ -51,9 +51,9 @@ class Actions
private $oSocial; private $oSocial;
/** /**
* @var \MailSo\Cache\CacheClient * @var array of \MailSo\Cache\CacheClient
*/ */
private $oCacher; private $aCachers;
/** /**
* @var \RainLoop\Providers\Storage * @var \RainLoop\Providers\Storage
@ -133,7 +133,7 @@ class Actions
$this->oMailClient = null; $this->oMailClient = null;
$this->oSocial = null; $this->oSocial = null;
$this->oConfig = null; $this->oConfig = null;
$this->oCacher = null; $this->aCachers = array();
$this->oStorageProvider = null; $this->oStorageProvider = null;
$this->oLocalStorageProvider = null; $this->oLocalStorageProvider = null;
@ -623,7 +623,7 @@ class Actions
'token' === $aAdminHash[0] && \md5(APP_SALT) === $aAdminHash[1] 'token' === $aAdminHash[0] && \md5(APP_SALT) === $aAdminHash[1]
) )
{ {
$this->Cacher()->Delete(\RainLoop\KeyPathHelper::SessionAdminKey($aAdminHash[2])); $this->Cacher(null, true)->Delete(\RainLoop\KeyPathHelper::SessionAdminKey($aAdminHash[2]));
} }
\RainLoop\Utils::ClearCookie(self::AUTH_ADMIN_TOKEN_KEY); \RainLoop\Utils::ClearCookie(self::AUTH_ADMIN_TOKEN_KEY);
@ -845,24 +845,44 @@ class Actions
} }
/** /**
* @param \RainLoop\Model\Account $oAccount = null
* @param bool $bForceFile = false
*
* @return \MailSo\Cache\CacheClient * @return \MailSo\Cache\CacheClient
*/ */
public function Cacher() public function Cacher($oAccount = null, $bForceFile = false)
{ {
if (null === $this->oCacher) $sKey = '';
if ($oAccount)
{ {
$this->oCacher = \MailSo\Cache\CacheClient::NewInstance(); $sKey = $oAccount->ParentEmailHelper();
}
$sIndexKey = empty($sKey) ? '_default_' : $sKey;
if ($bForceFile)
{
$sIndexKey .= '/_files_';
}
if (!isset($this->aCachers[$sIndexKey]))
{
$this->aCachers[$sIndexKey] = \MailSo\Cache\CacheClient::NewInstance();
$oDriver = null; $oDriver = null;
$sDriver = \strtoupper(\trim($this->Config()->Get('cache', 'fast_cache_driver', 'files'))); $sDriver = \strtoupper(\trim($this->Config()->Get('cache', 'fast_cache_driver', 'files')));
switch (true) switch (true)
{ {
default:
case $bForceFile:
$oDriver = \MailSo\Cache\Drivers\File::NewInstance(APP_PRIVATE_DATA.'cache', $sKey);
break;
case ('APC' === $sDriver || 'APCU' === $sDriver) && case ('APC' === $sDriver || 'APCU' === $sDriver) &&
\MailSo\Base\Utils::FunctionExistsAndEnabled(array( \MailSo\Base\Utils::FunctionExistsAndEnabled(array(
'apc_store', 'apc_fetch', 'apc_delete', 'apc_clear_cache')): 'apc_store', 'apc_fetch', 'apc_delete', 'apc_clear_cache')):
$oDriver = \MailSo\Cache\Drivers\APC::NewInstance(); $oDriver = \MailSo\Cache\Drivers\APC::NewInstance($sKey);
break; break;
case ('MEMCACHE' === $sDriver || 'MEMCACHED' === $sDriver) && case ('MEMCACHE' === $sDriver || 'MEMCACHED' === $sDriver) &&
@ -870,24 +890,21 @@ class Actions
$oDriver = \MailSo\Cache\Drivers\Memcache::NewInstance( $oDriver = \MailSo\Cache\Drivers\Memcache::NewInstance(
$this->Config()->Get('labs', 'fast_cache_memcache_host', '127.0.0.1'), $this->Config()->Get('labs', 'fast_cache_memcache_host', '127.0.0.1'),
(int) $this->Config()->Get('labs', 'fast_cache_memcache_port', 11211) (int) $this->Config()->Get('labs', 'fast_cache_memcache_port', 11211),
43200, $sKey
); );
break; break;
default:
$oDriver = \MailSo\Cache\Drivers\File::NewInstance(APP_PRIVATE_DATA.'cache');
break;
} }
if ($oDriver) if ($oDriver)
{ {
$this->oCacher->SetDriver($oDriver); $this->aCachers[$sIndexKey]->SetDriver($oDriver);
} }
$this->oCacher->SetCacheIndex($this->Config()->Get('cache', 'fast_cache_index', '')); $this->aCachers[$sIndexKey]->SetCacheIndex($this->Config()->Get('cache', 'fast_cache_index', ''));
} }
return $this->oCacher; return $this->aCachers[$sIndexKey];
} }
/** /**
@ -1011,7 +1028,7 @@ class Actions
private function getAdminToken() private function getAdminToken()
{ {
$sRand = \MailSo\Base\Utils::Md5Rand(); $sRand = \MailSo\Base\Utils::Md5Rand();
if (!$this->Cacher()->Set(\RainLoop\KeyPathHelper::SessionAdminKey($sRand), \time())) if (!$this->Cacher(null, true)->Set(\RainLoop\KeyPathHelper::SessionAdminKey($sRand), \time()))
{ {
$this->oLogger->Write('Cannot store an admin token', $this->oLogger->Write('Cannot store an admin token',
\MailSo\Log\Enumerations\Type::WARNING); \MailSo\Log\Enumerations\Type::WARNING);
@ -1035,7 +1052,7 @@ class Actions
$aAdminHash = \RainLoop\Utils::DecodeKeyValues($this->getAdminAuthToken()); $aAdminHash = \RainLoop\Utils::DecodeKeyValues($this->getAdminAuthToken());
if (!empty($aAdminHash[0]) && !empty($aAdminHash[1]) && !empty($aAdminHash[2]) && if (!empty($aAdminHash[0]) && !empty($aAdminHash[1]) && !empty($aAdminHash[2]) &&
'token' === $aAdminHash[0] && \md5(APP_SALT) === $aAdminHash[1] && 'token' === $aAdminHash[0] && \md5(APP_SALT) === $aAdminHash[1] &&
'' !== $this->Cacher()->Get(\RainLoop\KeyPathHelper::SessionAdminKey($aAdminHash[2]), '') '' !== $this->Cacher(null, true)->Get(\RainLoop\KeyPathHelper::SessionAdminKey($aAdminHash[2]), '')
) )
{ {
$bResult = true; $bResult = true;
@ -3348,7 +3365,7 @@ class Actions
{ {
$sDomain = \trim(APP_SITE); $sDomain = \trim(APP_SITE);
$oCacher = $this->Cacher(); $oCacher = $this->Cacher(null, true);
$oHttp = \MailSo\Base\Http::SingletonInstance(); $oHttp = \MailSo\Base\Http::SingletonInstance();
if (0 === \strlen($sDomain) || $oHttp->CheckLocalhost($sDomain) || !$oCacher) if (0 === \strlen($sDomain) || $oHttp->CheckLocalhost($sDomain) || !$oCacher)
@ -6115,7 +6132,7 @@ class Actions
$sFolderFullName = $this->GetActionParam('MessageFolder', ''); $sFolderFullName = $this->GetActionParam('MessageFolder', '');
$sUid = $this->GetActionParam('MessageUid', ''); $sUid = $this->GetActionParam('MessageUid', '');
$this->Cacher()->Set(\RainLoop\KeyPathHelper::ReadReceiptCache($oAccount->Email(), $sFolderFullName, $sUid), '1'); $this->Cacher($oAccount)->Set(\RainLoop\KeyPathHelper::ReadReceiptCache($oAccount->Email(), $sFolderFullName, $sUid), '1');
if (0 < \strlen($sFolderFullName) && 0 < \strlen($sUid)) if (0 < \strlen($sFolderFullName) && 0 < \strlen($sUid))
{ {
@ -8749,7 +8766,9 @@ class Actions
*/ */
private function cacherForUids() private function cacherForUids()
{ {
return ($this->Config()->Get('cache', 'enable', true) && $this->Config()->Get('cache', 'server_uids', false)) ? $this->Cacher() : null; $oAccount = $this->getAccountFromToken(false);
return ($this->Config()->Get('cache', 'enable', true) &&
$this->Config()->Get('cache', 'server_uids', false)) ? $this->Cacher($oAccount) : null;
} }
/** /**
@ -8757,7 +8776,8 @@ class Actions
*/ */
private function cacherForThreads() private function cacherForThreads()
{ {
return !!$this->Config()->Get('labs', 'use_imap_thread', false) ? $this->Cacher() : null; $oAccount = $this->getAccountFromToken(false);
return !!$this->Config()->Get('labs', 'use_imap_thread', false) ? $this->Cacher($oAccount) : null;
} }
/** /**
@ -9005,7 +9025,7 @@ class Actions
catch (\Exception $oException) { unset($oException); } catch (\Exception $oException) { unset($oException); }
} }
if (0 < \strlen($mResult['ReadReceipt']) && '1' === $this->Cacher()->Get( if (0 < \strlen($mResult['ReadReceipt']) && '1' === $this->Cacher($oAccount)->Get(
\RainLoop\KeyPathHelper::ReadReceiptCache($oAccount->Email(), $mResult['Folder'], $mResult['Uid']), '0')) \RainLoop\KeyPathHelper::ReadReceiptCache($oAccount->Email(), $mResult['Folder'], $mResult['Uid']), '0'))
{ {
$mResult['ReadReceipt'] = ''; $mResult['ReadReceipt'] = '';