]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Better user group member queries
authorEvan Prodromou <evan@status.net>
Wed, 4 Jul 2012 18:11:42 +0000 (14:11 -0400)
committerEvan Prodromou <evan@status.net>
Wed, 4 Jul 2012 18:12:11 +0000 (14:12 -0400)
classes/Profile.php
classes/User_group.php

index cefcaf90b5b6a31823163bf27cfd50c8dd873ff6..f0a326e21236735637be86f1e5978627a7199b2f 100644 (file)
@@ -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));
         }
     }
index 0f1cc40b28f38e42a50b4e93323144962a40a021..d343c622e3733279daaa3ff82ab84d5bc736231f 100644 (file)
@@ -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)
+    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));
+        }
+    }
+
+    function realGetMembers($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 ';
+        $gm = new Group_member();
 
-        if ($limit != null) {
-            if (common_config('db','type') == 'pgsql') {
-                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-            } else {
-                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+        $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()