Skip to content

Commit ea1ea67

Browse files
authored
Merge pull request #47 from topcoder-platform/issues-313
Issues-315: fixed performance issues with Category/Group permissions
2 parents 3b27e18 + fb2c372 commit ea1ea67

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

models/class.groupmodel.php

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ public function join($GroupID, $UserID, $watched = true, $followed = true ){
836836
}
837837
$this->followGroup($GroupID, $UserID, $followed);
838838
$this->watchGroup($GroupID, $UserID, $watched);
839-
839+
self::clearUserGroupCache($UserID);
840840
}
841841

842842
/**
@@ -860,7 +860,7 @@ public function accept($groupID, $userID){
860860
}
861861

862862
/**
863-
* Returntur if user is a member of the group
863+
* Return true if user is a member of the group
864864
*
865865
*/
866866
public function isMemberOfGroup($userID, $groupID) {
@@ -882,6 +882,7 @@ public function isMemberOfGroup($userID, $groupID) {
882882
* @throws Exception
883883
*/
884884
public function setRole($GroupID, $MemberID, $Role){
885+
self::clearUserGroupCache($MemberID);
885886
return $this->SQL->update('UserGroup')
886887
->set('Role' , $Role)
887888
->where('GroupID' , $GroupID)
@@ -897,10 +898,12 @@ public function setRole($GroupID, $MemberID, $Role){
897898
* @return bool|Gdn_DataSet|object|string|void
898899
*/
899900
public function removeMember($GroupID, $MemberID){
900-
$result = $this->unbookmarkGroupDiscussions($GroupID, $MemberID);
901+
$this->unbookmarkGroupDiscussions($GroupID, $MemberID);
901902
$this->unwatchGroup($GroupID, $MemberID);
902903
$this->unfollowGroup($GroupID, $MemberID);
903-
return $this->SQL->delete('UserGroup', ['GroupID' => $GroupID, 'UserID' => $MemberID]);
904+
$result = $this->SQL->delete('UserGroup', ['GroupID' => $GroupID, 'UserID' => $MemberID]);
905+
self::clearUserGroupCache($MemberID);
906+
return $result;
904907

905908
}
906909

@@ -1124,25 +1127,33 @@ public function countOfMembers($groupId, $role = null){
11241127
* @return array|mixed|null
11251128
*/
11261129
public function memberOf($userID){
1127-
$sql = $this->SQL;
1128-
$result = $sql->select('ug.Role, ug.GroupID')
1129-
->from('UserGroup ug')
1130-
->where('UserID', $userID)
1131-
->get();
1132-
return $result->result();
1130+
$key = 'UserGroup_'.$userID;
1131+
$result = Gdn::cache()->get($key);
1132+
if ($result === Gdn_Cache::CACHEOP_FAILURE) {
1133+
$sql = clone $this->SQL;
1134+
$sql->reset();
1135+
$result = $sql->select('ug.Role, ug.GroupID')
1136+
->from('UserGroup ug')
1137+
->where('UserID', $userID)
1138+
->get()->result();
1139+
Gdn::cache()->store($key, $result);
1140+
return $result;
1141+
} else {
1142+
return $result;
1143+
}
11331144
}
11341145

11351146
/**
11361147
* Get a group role
11371148
*/
11381149
public function getGroupRoleFor($userID, $groupID) {
1139-
$sql = $this->SQL;
1140-
$result = $sql->select('ug.Role')
1141-
->from('UserGroup ug')
1142-
->where('UserID', $userID)
1143-
->where('GroupID', $groupID)
1144-
->get()->firstRow();
1145-
return $result;
1150+
$groups = $this->memberOf($userID);
1151+
foreach ($groups as $group) {
1152+
if ($group->GroupID == $groupID) {
1153+
return $group;
1154+
}
1155+
}
1156+
return false;
11461157
}
11471158

11481159
/**
@@ -1857,4 +1868,18 @@ public function getRootGroupCategory($group){
18571868

18581869
return -1; //return Vanilla root
18591870
}
1871+
1872+
/**
1873+
* Clear the cached UserGroup data for a specific user.
1874+
*
1875+
* @param int|null $userID The user to clear. Use `null` for the current user.
1876+
*/
1877+
public static function clearUserGroupCache($userID = null) {
1878+
if ($userID === null) {
1879+
$userID = Gdn::session()->UserID;
1880+
}
1881+
1882+
$key = 'UserGroup_'.$userID;
1883+
Gdn::cache()->remove($key);
1884+
}
18601885
}

0 commit comments

Comments
 (0)