-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3eb942a
commit b1442bc
Showing
10 changed files
with
182 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/bitrix-expert/tools | ||
* @copyright Copyright © 2015 Nik Samokhvalov | ||
* @license MIT | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Bex\Tools; | ||
|
@@ -12,10 +11,10 @@ | |
use Bitrix\Main\Data\Cache; | ||
|
||
/** | ||
* Finder performs a fast search for data in standard structures of the Bitrix CMS with subsequent caching to speed | ||
* Finder performs a fast search for data in standard structures of the Bitrix CMS with subsequent caching to speed | ||
* up repeated queries. | ||
* | ||
* Finder uses the term "shard of the cache". The shards are used to separate the large volume cache for the logical | ||
* Finder uses the term "shard of the cache". The shards are used to separate the large volume cache for the logical | ||
* part. Each shard of the cache is stored separately. | ||
* | ||
* The basic Finder methods: | ||
|
@@ -26,9 +25,9 @@ | |
* Any instance Finder you can configure: | ||
* * `setCacheTime()` — time life of the cache. | ||
* * `setCacheDir()` — directory for cache. | ||
* | ||
* | ||
* The logic reset of the cache by tag will be defined in a concrete implementation of Finder. | ||
* | ||
* | ||
* @author Nik Samokhvalov <[email protected]> | ||
*/ | ||
abstract class Finder | ||
|
@@ -39,16 +38,16 @@ abstract class Finder | |
|
||
/** | ||
* Constructor with filter parameters for Finder. | ||
* | ||
* | ||
* Should not be called directly! Use *Tools classes (IblockTools, GroupTools, etc.) | ||
* | ||
* | ||
* @param array $filter Filter paramters. | ||
* @param bool $silenceMode When you use silence mode instead of an exception \Bex\Tools\ValueNotFoundException | ||
* @param bool $silenceMode When you use silence mode instead of an exception \Bex\Tools\ValueNotFoundException | ||
* (if value was be not found) is returned null. | ||
*/ | ||
public function __construct(array $filter, $silenceMode = false) | ||
{ | ||
$this->silenceMode = (bool) $silenceMode; | ||
$this->silenceMode = (bool)$silenceMode; | ||
} | ||
|
||
/** | ||
|
@@ -105,7 +104,7 @@ public static function setCacheDir($directory) | |
|
||
/** | ||
* Returns value by filter from cache. | ||
* | ||
* | ||
* @param array $cache All items for shard of the cache. | ||
* @param array $filter Parameters for filter. | ||
* @param string $shard Shard of the cache. | ||
|
@@ -116,7 +115,7 @@ abstract protected function getValue(array $cache, array $filter, $shard); | |
|
||
/** | ||
* Returns all items for shard of the cache. | ||
* | ||
* | ||
* @param string $shard Shard of the cache. | ||
* | ||
* @return mixed | ||
|
@@ -125,12 +124,12 @@ abstract protected function getItems($shard); | |
|
||
/** | ||
* Returns items from cache. If cache expired will be executed request to DB (method $this->getItems()). | ||
* | ||
* | ||
* @param array $filter Parameters for filter. | ||
* @param string $shard Shard of the cache. | ||
* | ||
* @return mixed | ||
* | ||
* | ||
* @throws ValueNotFoundException Value was not found. | ||
* @throws \InvalidArgumentException Invalid type on filter. | ||
*/ | ||
|
@@ -140,44 +139,37 @@ protected function getFromCache($filter = [], $shard = null) | |
|
||
$cache = Cache::createInstance(); | ||
|
||
if ($cache->initCache(static::getCacheTime(), null, static::getCacheDir() . '/' . $shard)) | ||
{ | ||
if ($cache->initCache(static::getCacheTime(), null, static::getCacheDir() . '/' . $shard)) { | ||
$items = $cache->getVars(); | ||
} | ||
else | ||
{ | ||
} else { | ||
$cache->startDataCache(); | ||
Application::getInstance()->getTaggedCache()->startTagCache(static::getCacheDir() . '/' . $shard); | ||
|
||
$items = $this->getItems($shard); | ||
|
||
if (!empty($items)) | ||
{ | ||
if (!empty($items)) { | ||
Application::getInstance()->getTaggedCache()->endTagCache(); | ||
|
||
$cache->endDataCache($items); | ||
} | ||
else | ||
{ | ||
} else { | ||
$cache->abortDataCache(); | ||
} | ||
} | ||
|
||
try { | ||
return $this->getValue($items, $filter, $shard); | ||
} catch (ValueNotFoundException $e) { | ||
if ($this->silenceMode) | ||
{ | ||
if ($this->silenceMode) { | ||
return null; | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* Registration of the tag cache. | ||
* | ||
* | ||
* @param string $tag | ||
*/ | ||
protected function registerCacheTag($tag) | ||
|
@@ -187,7 +179,7 @@ protected function registerCacheTag($tag) | |
|
||
/** | ||
* Deletes all cache by tag. | ||
* | ||
* | ||
* @param string $tag | ||
*/ | ||
protected static function deleteCacheByTag($tag) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/bitrix-expert/tools | ||
* @copyright Copyright © 2015 Nik Samokhvalov | ||
* @license MIT | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Bex\Tools\Group; | ||
|
@@ -15,7 +14,7 @@ | |
|
||
/** | ||
* Finder of the users groups. | ||
* | ||
* | ||
* @author Nik Samokhvalov <[email protected]> | ||
*/ | ||
class GroupFinder extends Finder | ||
|
@@ -33,23 +32,19 @@ class GroupFinder extends Finder | |
public function __construct(array $filter, $silenceMode = false) | ||
{ | ||
parent::__construct($filter, $silenceMode); | ||
|
||
$filter = $this->prepareFilter($filter); | ||
|
||
if (isset($filter['id'])) | ||
{ | ||
if (isset($filter['id'])) { | ||
$this->id = $filter['id']; | ||
} | ||
|
||
if (isset($filter['code'])) | ||
{ | ||
if (isset($filter['code'])) { | ||
$this->code = $filter['code']; | ||
} | ||
|
||
if (!isset($this->id)) | ||
{ | ||
if (!isset($this->code)) | ||
{ | ||
if (!isset($this->id)) { | ||
if (!isset($this->code)) { | ||
throw new ArgumentNullException('code'); | ||
} | ||
|
||
|
@@ -61,7 +56,7 @@ public function __construct(array $filter, $silenceMode = false) | |
|
||
/** | ||
* Gets group ID. | ||
* | ||
* | ||
* @return integer | ||
*/ | ||
public function id() | ||
|
@@ -73,7 +68,7 @@ public function id() | |
|
||
/** | ||
* Gets group code. | ||
* | ||
* | ||
* @return string | ||
*/ | ||
public function code() | ||
|
@@ -85,28 +80,22 @@ public function code() | |
|
||
/** | ||
* @inheritdoc | ||
* | ||
* | ||
* @throws ArgumentNullException | ||
*/ | ||
protected function prepareFilter(array $filter) | ||
{ | ||
foreach ($filter as $code => &$value) | ||
{ | ||
if ($code === 'id') | ||
{ | ||
foreach ($filter as $code => &$value) { | ||
if ($code === 'id') { | ||
intval($value); | ||
|
||
if ($value <= 0) | ||
{ | ||
if ($value <= 0) { | ||
throw new ArgumentNullException($code); | ||
} | ||
} | ||
else | ||
{ | ||
} else { | ||
trim(htmlspecialchars($value)); | ||
|
||
if (strlen($value) <= 0) | ||
{ | ||
if (strlen($value) <= 0) { | ||
throw new ArgumentNullException($code); | ||
} | ||
} | ||
|
@@ -120,27 +109,23 @@ protected function prepareFilter(array $filter) | |
*/ | ||
protected function getValue(array $cache, array $filter, $shard) | ||
{ | ||
switch ($filter['type']) | ||
{ | ||
switch ($filter['type']) { | ||
case 'id': | ||
if (isset($this->id)) | ||
{ | ||
if (isset($this->id)) { | ||
return $this->id; | ||
} | ||
|
||
$value = (int) $cache['GROUPS_ID'][$this->code]; | ||
$value = (int)$cache['GROUPS_ID'][$this->code]; | ||
|
||
if ($value <= 0) | ||
{ | ||
if ($value <= 0) { | ||
throw new ValueNotFoundException('Group ID', 'group code "' . $this->code . '"'); | ||
} | ||
break; | ||
|
||
case 'code': | ||
$value = $cache['GROUPS_CODE'][$this->id]; | ||
|
||
if (strlen($value) <= 0) | ||
{ | ||
if (strlen($value) <= 0) { | ||
throw new ValueNotFoundException('Group code', 'ID #' . $this->id); | ||
} | ||
break; | ||
|
@@ -159,22 +144,19 @@ protected function getValue(array $cache, array $filter, $shard) | |
protected function getItems($shard) | ||
{ | ||
$items = []; | ||
|
||
$rsGroups = GroupTable::query() | ||
->setSelect(['ID', 'STRING_ID']) | ||
->exec(); | ||
|
||
while ($group = $rsGroups->fetch()) | ||
{ | ||
if ($group['STRING_ID']) | ||
{ | ||
while ($group = $rsGroups->fetch()) { | ||
if ($group['STRING_ID']) { | ||
$items['GROUPS_ID'][$group['STRING_ID']] = $group['ID']; | ||
$items['GROUPS_CODE'][$group['ID']] = $group['STRING_ID']; | ||
} | ||
} | ||
|
||
if (!empty($items)) | ||
{ | ||
if (!empty($items)) { | ||
Application::getInstance()->getTaggedCache()->registerTag(static::getCacheTag()); | ||
} | ||
|
||
|
@@ -183,7 +165,7 @@ protected function getItems($shard) | |
|
||
/** | ||
* Gets tag of cache. | ||
* | ||
* | ||
* @return string | ||
*/ | ||
public static function getCacheTag() | ||
|
@@ -193,7 +175,7 @@ public static function getCacheTag() | |
|
||
/** | ||
* Sets tag for cache. | ||
* | ||
* | ||
* @param string $tag | ||
*/ | ||
public static function setCacheTag($tag) | ||
|
@@ -231,7 +213,7 @@ public static function onAfterGroupUpdate($id, &$fields) | |
{ | ||
static::deleteCacheByTag(static::getCacheTag()); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
* @see Finder::deleteCacheByTag() | ||
|
@@ -240,8 +222,7 @@ protected static function deleteGroupCache() | |
{ | ||
global $CACHE_MANAGER; | ||
|
||
if (defined('BX_COMP_MANAGED_CACHE')) | ||
{ | ||
if (defined('BX_COMP_MANAGED_CACHE')) { | ||
$CACHE_MANAGER->ClearByTag(static::getCacheTag()); | ||
} | ||
} | ||
|
Oops, something went wrong.