]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
cache frequently-used subscriber, subscription, notice and fave count values
authorEvan Prodromou <evan@controlyourself.ca>
Fri, 10 Jul 2009 00:28:38 +0000 (17:28 -0700)
committerEvan Prodromou <evan@controlyourself.ca>
Fri, 10 Jul 2009 00:28:38 +0000 (17:28 -0700)
classes/Notice.php
classes/Profile.php
classes/User.php
lib/command.php
lib/profileaction.php
lib/subs.php
lib/twitterapi.php

index e975cab93ce6b44e8bef11375c866ec1d3304382..75044cf638ae1b5218a08863ceed01f2a59a4191 100644 (file)
@@ -356,6 +356,8 @@ class Notice extends Memcached_DataObject
         $this->blowTagCache($blowLast);
         $this->blowGroupCache($blowLast);
         $this->blowConversationCache($blowLast);
+        $profile = Profile::staticGet($this->profile_id);
+        $profile->blowNoticeCount();
     }
 
     function blowConversationCache($blowLast=false)
index a0ed6b3ca349e0da83dc161a409893f821cdb8af..224b61bd2ee3508f19789923879b07f5ae3582e5 100644 (file)
@@ -337,4 +337,132 @@ class Profile extends Memcached_DataObject
 
         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));
+        }
+    }
 }
index 04b38a0d2223ef7d240768ec0de6282d03bc51a8..6c1f149e4ded31fca07627ab0c16210e63ece717 100644 (file)
@@ -494,6 +494,8 @@ class User extends Memcached_DataObject
             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
         }
+        $profile = $this->getProfile();
+        $profile->blowFaveCount();
     }
 
     function getSelfTags()
index 56466138293caa18cab3023d5bfca0dd2a24ffff..4e2280bc8070d58bc5edefc5edd09230f5a7c95b 100644 (file)
@@ -97,18 +97,11 @@ class StatsCommand extends Command
 {
     function execute($channel)
     {
+        $profile = $this->user->getProfile();
 
-        $subs = new Subscription();
-        $subs->subscriber = $this->user->id;
-        $subs_count = (int) $subs->count() - 1;
-
-        $subbed = new Subscription();
-        $subbed->subscribed = $this->user->id;
-        $subbed_count = (int) $subbed->count() - 1;
-
-        $notices = new Notice();
-        $notices->profile_id = $this->user->id;
-        $notice_count = (int) $notices->count();
+        $subs_count   = $profile->subscriptionCount();
+        $subbed_count = $profile->subscriberCount();
+        $notice_count = $profile->noticeCount();
 
         $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
                                    "Subscribers: %2\$s\n".
index eeb5dbe48d80db5510b98861626f62ad6badd343..9e9c79c78a90436c76397427826ecf40f4d41bc2 100644 (file)
@@ -163,18 +163,9 @@ class ProfileAction extends OwnerDesignAction
 
     function showStatistics()
     {
-        // XXX: WORM cache this
-        $subs = new Subscription();
-        $subs->subscriber = $this->profile->id;
-        $subs_count = (int) $subs->count() - 1;
-
-        $subbed = new Subscription();
-        $subbed->subscribed = $this->profile->id;
-        $subbed_count = (int) $subbed->count() - 1;
-
-        $notices = new Notice();
-        $notices->profile_id = $this->profile->id;
-        $notice_count = (int) $notices->count();
+        $subs_count   = $this->profile->subscriptionCount();
+        $subbed_count = $this->profile->subscriberCount();
+        $notice_count = $this->profile->noticeCount();
 
         $this->elementStart('div', array('id' => 'entity_statistics',
                                          'class' => 'section'));
@@ -199,7 +190,7 @@ class ProfileAction extends OwnerDesignAction
                                                              array('nickname' => $this->profile->nickname))),
                        _('Subscriptions'));
         $this->elementEnd('dt');
-        $this->element('dd', null, (is_int($subs_count)) ? $subs_count : '0');
+        $this->element('dd', null, $subs_count);
         $this->elementEnd('dl');
 
         $this->elementStart('dl', 'entity_subscribers');
@@ -208,12 +199,12 @@ class ProfileAction extends OwnerDesignAction
                                                              array('nickname' => $this->profile->nickname))),
                        _('Subscribers'));
         $this->elementEnd('dt');
-        $this->element('dd', 'subscribers', (is_int($subbed_count)) ? $subbed_count : '0');
+        $this->element('dd', 'subscribers', $subbed_count);
         $this->elementEnd('dl');
 
         $this->elementStart('dl', 'entity_notices');
         $this->element('dt', null, _('Notices'));
-        $this->element('dd', null, (is_int($notice_count)) ? $notice_count : '0');
+        $this->element('dd', null, $notice_count);
         $this->elementEnd('dl');
 
         $this->elementEnd('div');
index 3bd67b39c7142ffb64deb1926afa378b4315cfa0..e76023752725344128009f3dc2af602c9d3d5b93 100644 (file)
@@ -44,7 +44,6 @@ function subs_subscribe_user($user, $other_nickname)
 
 function subs_subscribe_to($user, $other)
 {
-
     if ($user->isSubscribed($other)) {
         return _('Already subscribed!.');
     }
@@ -60,12 +59,16 @@ function subs_subscribe_to($user, $other)
 
     subs_notify($other, $user);
 
-        $cache = common_memcache();
+    $cache = common_memcache();
 
     if ($cache) {
         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
        }
 
+    $profile = $user->getProfile();
+
+    $profile->blowSubscriptionsCount();
+    $other->blowSubscribersCount();
 
     if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
         if (!$other->subscribeTo($user)) {
@@ -117,7 +120,6 @@ function subs_unsubscribe_user($user, $other_nickname)
 
 function subs_unsubscribe_to($user, $other)
 {
-
     if (!$user->isSubscribed($other))
         return _('Not subscribed!.');
 
@@ -139,6 +141,11 @@ function subs_unsubscribe_to($user, $other)
         $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
        }
 
+    $profile = $user->getProfile();
+
+    $profile->blowSubscriptionsCount();
+    $other->blowSubscribersCount();
+
     return true;
 }
 
index 8f902cbcaf87066ab58cc5d1cd2e693d19145db0..f48513e67f7d243140cdba2c19883dadc10cb2a0 100644 (file)
@@ -89,7 +89,7 @@ class TwitterapiAction extends Action
 
         $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
         $twitter_user['protected'] = false; # not supported by Laconica yet
-        $twitter_user['followers_count'] = $this->count_subscriptions($profile);
+        $twitter_user['followers_count'] = $profile->subscriberCount();
 
         // To be supported soon...
         $twitter_user['profile_background_color'] = '';
@@ -98,17 +98,11 @@ class TwitterapiAction extends Action
         $twitter_user['profile_sidebar_fill_color'] = '';
         $twitter_user['profile_sidebar_border_color'] = '';
 
-        $subbed = DB_DataObject::factory('subscription');
-        $subbed->subscriber = $profile->id;
-        $subbed_count = (int) $subbed->count() - 1;
-        $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0;
+        $twitter_user['friends_count'] = $profile->subscriptionCount();
 
         $twitter_user['created_at'] = $this->date_twitter($profile->created);
 
-        $faves = DB_DataObject::factory('fave');
-        $faves->user_id = $user->id;
-        $faves_count = (int) $faves->count();
-        $twitter_user['favourites_count'] = $faves_count; // British spelling!
+        $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
 
         // Need to pull up the user for some of this
         $user = User::staticGet($profile->id);
@@ -129,11 +123,7 @@ class TwitterapiAction extends Action
         $twitter_user['profile_background_image_url'] = '';
         $twitter_user['profile_background_tile'] = false;
 
-        $notices = DB_DataObject::factory('notice');
-        $notices->profile_id = $profile->id;
-        $notice_count = (int) $notices->count();
-
-        $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0;
+        $twitter_user['statuses_count'] = $profile->noticeCount();
 
         // Is the requesting user following this user?
         $twitter_user['following'] = false;
@@ -396,7 +386,7 @@ class TwitterapiAction extends Action
             $enclosure = $entry['enclosures'][0];
             $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
         }
-        
+
         $this->elementEnd('item');
     }