]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
cache frequently-used subscriber, subscription, notice and fave count values
[quix0rs-gnu-social.git] / classes / Profile.php
index 6b27c80cb5528e549126e4b84294f142f0c54cfb..224b61bd2ee3508f19789923879b07f5ae3582e5 100644 (file)
@@ -289,4 +289,180 @@ class Profile extends Memcached_DataObject
             return Avatar::defaultImage($size);
         }
     }
+
+    function getSubscriptions($offset=0, $limit=null)
+    {
+        $qry =
+          'SELECT profile.* ' .
+          'FROM profile JOIN subscription ' .
+          'ON profile.id = subscription.subscribed ' .
+          'WHERE subscription.subscriber = %d ' .
+          'AND subscription.subscribed != subscription.subscriber ' .
+          'ORDER BY subscription.created DESC ';
+
+        if (common_config('db','type') == 'pgsql') {
+            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+        } else {
+            $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+        }
+
+        $profile = new Profile();
+
+        $profile->query(sprintf($qry, $this->id));
+
+        return $profile;
+    }
+
+    function getSubscribers($offset=0, $limit=null)
+    {
+        $qry =
+          'SELECT profile.* ' .
+          'FROM profile JOIN subscription ' .
+          'ON profile.id = subscription.subscriber ' .
+          'WHERE subscription.subscribed = %d ' .
+          'AND subscription.subscribed != subscription.subscriber ' .
+          'ORDER BY subscription.created DESC ';
+
+        if ($offset) {
+            if (common_config('db','type') == 'pgsql') {
+                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+            } else {
+                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+            }
+        }
+
+        $profile = new Profile();
+
+        $cnt = $profile->query(sprintf($qry, $this->id));
+
+        return $profile;
+    }
+
+    function subscriptionCount()
+    {
+        $c = common_memcache();
+
+        if (!empty($c)) {
+            $cnt = $c->get(common_cache_key('profile:subscription_count:'.$this->id));
+            if (is_integer($cnt)) {
+                return (int) $cnt;
+            }
+        }
+
+        $sub = new Subscription();
+        $sub->subscriber = $this->id;
+
+        $cnt = (int) $sub->count('distinct subscribed');
+
+        $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
+
+        if (!empty($c)) {
+            $c->set(common_cache_key('profile:subscription_count:'.$this->id), $cnt);
+        }
+
+        common_debug("subscriptionCount == $cnt");
+        return $cnt;
+    }
+
+    function subscriberCount()
+    {
+        $c = common_memcache();
+        if (!empty($c)) {
+            $cnt = $c->get(common_cache_key('profile:subscriber_count:'.$this->id));
+            if (is_integer($cnt)) {
+                return (int) $cnt;
+            }
+        }
+
+        $sub = new Subscription();
+        $sub->subscribed = $this->id;
+
+        $cnt = (int) $sub->count('distinct subscriber');
+
+        $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
+
+        if (!empty($c)) {
+            $c->set(common_cache_key('profile:subscriber_count:'.$this->id), $cnt);
+        }
+
+        common_debug("subscriberCount == $cnt");
+        return $cnt;
+    }
+
+    function faveCount()
+    {
+        $c = common_memcache();
+        if (!empty($c)) {
+            $cnt = $c->get(common_cache_key('profile:fave_count:'.$this->id));
+            if (is_integer($cnt)) {
+                return (int) $cnt;
+            }
+        }
+
+        $faves = new Fave();
+        $faves->user_id = $this->id;
+        $cnt = (int) $faves->count('distinct notice_id');
+
+        if (!empty($c)) {
+            $c->set(common_cache_key('profile:fave_count:'.$this->id), $cnt);
+        }
+
+        common_debug("faveCount == $cnt");
+        return $cnt;
+    }
+
+    function noticeCount()
+    {
+        $c = common_memcache();
+
+        if (!empty($c)) {
+            $cnt = $c->get(common_cache_key('profile:notice_count:'.$this->id));
+            if (is_integer($cnt)) {
+                return (int) $cnt;
+            }
+        }
+
+        $notices = new Notice();
+        $notices->profile_id = $this->id;
+        $cnt = (int) $notices->count('distinct id');
+
+        if (!empty($c)) {
+            $c->set(common_cache_key('profile:notice_count:'.$this->id), $cnt);
+        }
+
+        common_debug("noticeCount == $cnt");
+        return $cnt;
+    }
+
+    function blowSubscriberCount()
+    {
+        $c = common_memcache();
+        if (!empty($c)) {
+            $c->delete(common_cache_key('profile:subscriber_count:'.$this->id));
+        }
+    }
+
+    function blowSubscriptionCount()
+    {
+        $c = common_memcache();
+        if (!empty($c)) {
+            $c->delete(common_cache_key('profile:subscription_count:'.$this->id));
+        }
+    }
+
+    function blowFaveCount()
+    {
+        $c = common_memcache();
+        if (!empty($c)) {
+            $c->delete(common_cache_key('profile:fave_count:'.$this->id));
+        }
+    }
+
+    function blowNoticeCount()
+    {
+        $c = common_memcache();
+        if (!empty($c)) {
+            $c->delete(common_cache_key('profile:notice_count:'.$this->id));
+        }
+    }
 }