From 642b1044cc5326a627c5291ccfb5c14768f83835 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 4 Jul 2012 14:11:42 -0400 Subject: [PATCH 1/4] Better user group member queries --- classes/Profile.php | 4 +++ classes/User_group.php | 76 ++++++++++++++++++++++++++++++------------ 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/classes/Profile.php b/classes/Profile.php index cefcaf90b5..f0a326e212 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -595,6 +595,8 @@ class Profile extends Managed_DataObject if (Event::handle('StartJoinGroup', array($group, $this))) { $join = Group_member::join($group->id, $this->id); self::blow('profile:groups:%d', $this->id); + self::blow('group:members:%d', $group->id); + self::blow('group:member_count:%d', $group->id); Event::handle('EndJoinGroup', array($group, $this)); } } @@ -615,6 +617,8 @@ class Profile extends Managed_DataObject if (Event::handle('StartLeaveGroup', array($group, $this))) { Group_member::leave($group->id, $this->id); self::blow('profile:groups:%d', $this->id); + self::blow('group:members:%d', $group->id); + self::blow('group:member_count:%d', $group->id); Event::handle('EndLeaveGroup', array($group, $this)); } } diff --git a/classes/User_group.php b/classes/User_group.php index 0f1cc40b28..d343c622e3 100644 --- a/classes/User_group.php +++ b/classes/User_group.php @@ -7,6 +7,7 @@ class User_group extends Managed_DataObject { const JOIN_POLICY_OPEN = 0; const JOIN_POLICY_MODERATE = 1; + const CACHE_WINDOW = 201; ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -141,26 +142,50 @@ class User_group extends Managed_DataObject return !in_array($nickname, $blacklist); } - function getMembers($offset=0, $limit=null) - { - $qry = - 'SELECT profile.* ' . - 'FROM profile JOIN group_member '. - 'ON profile.id = group_member.profile_id ' . - 'WHERE group_member.group_id = %d ' . - 'ORDER BY group_member.created DESC '; + function getMembers($offset=0, $limit=null) { + if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) { + return $this->realGetMembers($offset, + $limit); + } else { + $key = sprintf('group:members:%d', $this->id); + $window = self::cacheGet($key); + if ($window === false) { + $members = $this->realGetMembers(0, + User_group::CACHE_WINDOW); + $window = $members->fetchAll(); + self::cacheSet($key, $window); + } + return new ArrayWrapper(array_slice($window, + $offset, + $limit)); + } + } - if ($limit != null) { - if (common_config('db','type') == 'pgsql') { - $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset; - } else { - $qry .= ' LIMIT ' . $offset . ', ' . $limit; + function realGetMembers($offset=0, $limit=null) + { + $gm = new Group_member(); + + $gm->selectAdd(); + $gm->selectAdd('profile_id'); + + $gm->group_id = $this->id; + + $gm->orderBy('created DESC'); + + if (!is_null($limit)) { + $gm->limit($offset, $limit); + } + + $ids = array(); + + if ($gm->find()) { + while ($gm->fetch()) { + $ids[] = $gm->profile_id; } } - $members = new Profile(); + $members = Profile::multiGet('id', $ids); - $members->query(sprintf($qry, $this->id)); return $members; } @@ -196,17 +221,24 @@ class User_group extends Managed_DataObject function getMemberCount() { - // XXX: WORM cache this + $key = sprintf("group:member_count:%d", $this->id); - $members = $this->getMembers(); - $member_count = 0; + $cnt = self::cacheGet($key); - /** $member->count() doesn't work. */ - while ($members->fetch()) { - $member_count++; + if (is_integer($cnt)) { + return (int) $cnt; } - return $member_count; + $mem = new Group_member(); + $mem->group_id = $this->id; + + // XXX: why 'distinct'? + + $cnt = (int) $mem->count('distinct profile_id'); + + self::cacheSet($key, $cnt); + + return $cnt; } function getBlockedCount() From 69fb79caaed1dee682959544123ed64bbf506213 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 4 Jul 2012 14:38:06 -0400 Subject: [PATCH 2/4] Cache IDs rather than profile objects --- classes/User_group.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/classes/User_group.php b/classes/User_group.php index d343c622e3..f99feeb874 100644 --- a/classes/User_group.php +++ b/classes/User_group.php @@ -143,25 +143,28 @@ class User_group extends Managed_DataObject } function getMembers($offset=0, $limit=null) { + $ids = null; if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) { - return $this->realGetMembers($offset, - $limit); + $ids = $this->getMemberIDs($offset, + $limit); } else { $key = sprintf('group:members:%d', $this->id); $window = self::cacheGet($key); if ($window === false) { - $members = $this->realGetMembers(0, - User_group::CACHE_WINDOW); - $window = $members->fetchAll(); + $window = $this->getMemberIDs(0, + User_group::CACHE_WINDOW); self::cacheSet($key, $window); } - return new ArrayWrapper(array_slice($window, - $offset, - $limit)); + + $ids = array_slice($window, + $offset, + $limit); } + + return Profile::multiGet('id', $ids); } - function realGetMembers($offset=0, $limit=null) + function getMemberIDs($offset=0, $limit=null) { $gm = new Group_member(); @@ -184,9 +187,7 @@ class User_group extends Managed_DataObject } } - $members = Profile::multiGet('id', $ids); - - return $members; + return $ids; } /** From 1c3c269ab460345d2d201bff5d3e72bfd16291d9 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 4 Jul 2012 14:39:26 -0400 Subject: [PATCH 3/4] cache key for member_ids --- classes/Profile.php | 4 ++-- classes/User_group.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/Profile.php b/classes/Profile.php index f0a326e212..7005e776a5 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -595,7 +595,7 @@ class Profile extends Managed_DataObject if (Event::handle('StartJoinGroup', array($group, $this))) { $join = Group_member::join($group->id, $this->id); self::blow('profile:groups:%d', $this->id); - self::blow('group:members:%d', $group->id); + self::blow('group:member_ids:%d', $group->id); self::blow('group:member_count:%d', $group->id); Event::handle('EndJoinGroup', array($group, $this)); } @@ -617,7 +617,7 @@ class Profile extends Managed_DataObject if (Event::handle('StartLeaveGroup', array($group, $this))) { Group_member::leave($group->id, $this->id); self::blow('profile:groups:%d', $this->id); - self::blow('group:members:%d', $group->id); + self::blow('group:member_ids:%d', $group->id); self::blow('group:member_count:%d', $group->id); Event::handle('EndLeaveGroup', array($group, $this)); } diff --git a/classes/User_group.php b/classes/User_group.php index f99feeb874..406537dd8a 100644 --- a/classes/User_group.php +++ b/classes/User_group.php @@ -148,7 +148,7 @@ class User_group extends Managed_DataObject $ids = $this->getMemberIDs($offset, $limit); } else { - $key = sprintf('group:members:%d', $this->id); + $key = sprintf('group:member_ids:%d', $this->id); $window = self::cacheGet($key); if ($window === false) { $window = $this->getMemberIDs(0, From 46f6b2c1196575a6eb5baf35c3016ff99dfba891 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 4 Jul 2012 14:45:50 -0400 Subject: [PATCH 4/4] Don't call free on the results of a member query --- actions/groupmembers.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/actions/groupmembers.php b/actions/groupmembers.php index 088b171956..52979101fd 100644 --- a/actions/groupmembers.php +++ b/actions/groupmembers.php @@ -104,8 +104,6 @@ class GroupmembersAction extends GroupAction $cnt = $member_list->show(); } - $members->free(); - $this->pagination($this->page > 1, $cnt > PROFILES_PER_PAGE, $this->page, 'groupmembers', array('nickname' => $this->group->nickname));