Skip to content

Commit

Permalink
Ezp 27427 improving cache generation performance (ezsystems#68)
Browse files Browse the repository at this point in the history
* Cache generation performance improvement

* Load correct groups/roles in ezuser
  • Loading branch information
pkamps authored Sep 15, 2017
1 parent 5c089f6 commit 53d22fe
Showing 1 changed file with 47 additions and 48 deletions.
95 changes: 47 additions & 48 deletions kernel/classes/datatypes/ezuser/ezuser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1415,16 +1415,20 @@ private static function generateUserCacheData( $userId )
'password_hash' => $user->attribute( 'password_hash' ),
'password_hash_type' => $user->attribute( 'password_hash_type' ) );

// function groups relies on caching. The function generateUserCacheData relies on function groups.
// To avoid an infinitive loop, the code is disabling caching in function generateUserCacheData.
// At the end of this method flag is set to previous value again
$previousCachingState = $user->setCachingEnabled( false );
// user groups list (session: eZUserGroupsCache)
$groups = $user->generateGroupIdList();
$groups = $user->groups();
$data['groups'] = $groups;

// role list (session: eZRoleIDList)
$groups[] = $userId;
$data['roles'] = eZRole::fetchIDListByUser( $groups );

// role limitation list (session: eZRoleLimitationValueList)
$limitList = $user->limitList( false );
$limitList = $user->limitList();
foreach ( $limitList as $limit )
{
$data['role_limitations'][] = $limit['limit_value'];
Expand All @@ -1436,6 +1440,8 @@ private static function generateUserCacheData( $userId )
// discount rules (session: eZUserDiscountRules<userId>)
$data['discount_rules'] = eZUserDiscountRule::generateIDListByUserID( $userId );

$user->setCachingEnabled( $previousCachingState );

return $data;
}

Expand Down Expand Up @@ -1920,7 +1926,7 @@ function accessArray()
*/
function generateAccessArray()
{
$idList = $this->generateGroupIdList();
$idList = $this->groups();
$idList[] = $this->attribute( 'contentobject_id' );

return eZRole::accessArrayByUserID( $idList );
Expand Down Expand Up @@ -2470,64 +2476,29 @@ function groups( $asObject = false )
{
if ( !isset( $this->GroupsAsObjects ) )
{
$db = eZDB::instance();
$contentobjectID = $this->attribute( 'contentobject_id' );
$userGroups = $db->arrayQuery( "SELECT d.*, c.path_string
FROM ezcontentobject_tree b,
ezcontentobject_tree c,
ezcontentobject d
WHERE b.contentobject_id='$contentobjectID' AND
b.parent_node_id = c.node_id AND
d.id = c.contentobject_id
ORDER BY c.contentobject_id ");
$userGroupArray = array();
$pathArray = array();
foreach ( $userGroups as $group )
{
$pathItems = explode( '/', $group["path_string"] );
array_pop( $pathItems );
array_pop( $pathItems );
foreach ( $pathItems as $pathItem )
{
if ( $pathItem != '' && $pathItem > 1 )
$pathArray[] = $pathItem;
}
$userGroupArray[] = new eZContentObject( $group );
}
$pathArray = array_unique( $pathArray );
$this->GroupsAsObjects = array();

if ( !empty( $pathArray ) )
foreach ( $this->groups() as $group )
{
$extraGroups = $db->arrayQuery( "SELECT d.*
FROM ezcontentobject_tree c,
ezcontentobject d
WHERE c.node_id in ( " . implode( ', ', $pathArray ) . " ) AND
d.id = c.contentobject_id
ORDER BY c.contentobject_id ");
foreach ( $extraGroups as $group )
{
$userGroupArray[] = new eZContentObject( $group );
}
$this->GroupsAsObjects[] = new eZContentObject( $group );
}

$this->GroupsAsObjects = $userGroupArray;
}

return $this->GroupsAsObjects;
}
else
{
if ( !isset( $this->Groups ) )
{
if ( eZINI::instance()->variable( 'RoleSettings', 'EnableCaching' ) === 'true' )
{
$userCache = $this->getUserCache();
$this->Groups = $userCache['groups'];
}
else
$this->fetchUserCacheIfNeeded();
if ( !isset($this->UserCache['groups']) )
{
$this->Groups = $this->generateGroupIdList();
$this->UserCache['groups'] = $this->generateGroupIdList();
}

$this->Groups = $this->UserCache['groups'];
}

return $this->Groups;
}
}
Expand Down Expand Up @@ -2896,6 +2867,32 @@ public function canLoginToSiteAccess( $access )
return $hasAccessToSite;
}

/**
* Fetches user cache when given preconditions are met
*/
protected function fetchUserCacheIfNeeded()
{
if ( null === $this->UserCache && true === $this->CachingEnabled && 'true' === eZINI::instance()->variable( 'RoleSettings', 'EnableCaching' ))
{
$this->UserCache = $this->getUserCache();
}
}

/**
* Sets cachingEnabled flag to given value. Returns previous value of cachingEnabled flag
*
* @param bool $cachingEnabled
*
* @return bool
*/
protected function setCachingEnabled( $cachingEnabled )
{
$previousValue = $this->CachingEnabled;
$this->CachingEnabled = $cachingEnabled;

return $previousValue;
}

/// \privatesection
public $Login;
public $Email;
Expand All @@ -2921,6 +2918,8 @@ public function canLoginToSiteAccess( $access )
* @since 4.3
*/
protected static $userHasLoggedOut = false;

private $CachingEnabled = true;
}

?>

0 comments on commit 53d22fe

Please sign in to comment.