From 3550afb5f04485839283be39065f3d09899b5c1a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Thu, 14 Apr 2011 14:01:10 -0400 Subject: [PATCH] Cache rollup stuff in the cache, not in the DB --- classes/Profile_list.php | 48 +++++++++++++++++----------- classes/Profile_tag.php | 22 +++++++++++++ classes/Profile_tag_subscription.php | 20 ++++++++++++ 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/classes/Profile_list.php b/classes/Profile_list.php index 4fd731c9b2..bbe892c0d0 100644 --- a/classes/Profile_list.php +++ b/classes/Profile_list.php @@ -467,18 +467,24 @@ class Profile_list extends Memcached_DataObject function taggedCount($recount=false) { - if (!$recount) { - return $this->tagged_count; + $keypart = sprintf('profile_list:tagged_count:%d:%s', + $this->tagger, + $this->tag); + + $count = self::cacheGet($keypart); + + if ($count === false) { + $tags = new Profile_tag(); + + $tags->tag = $this->tag; + $tags->tagger = $this->tagger; + + $count = $tags->count('distinct tagged'); + + self::cacheSet($keypart, $count); } - $tags = new Profile_tag(); - $tags->tag = $this->tag; - $tags->tagger = $this->tagger; - $orig = clone($this); - $this->tagged_count = (int) $tags->count('distinct tagged'); - $this->update($orig); - - return $this->tagged_count; + return $count; } /** @@ -492,17 +498,21 @@ class Profile_list extends Memcached_DataObject function subscriberCount($recount=false) { - if ($recount) { - return $this->subscriber_count; + $keypart = sprintf('profile_list:subscriber_count:%d', + $this->id); + + $count = self::cacheGet($keypart); + + if ($count === false) { + + $sub = new Profile_tag_subscription(); + $sub->profile_tag_id = $this->id; + $count = (int) $sub->count('distinct profile_id'); + + self::cacheSet($keypart, $count); } - $sub = new Profile_tag_subscription(); - $sub->profile_tag_id = $this->id; - $orig = clone($this); - $this->subscriber_count = (int) $sub->count('distinct profile_id'); - $this->update($orig); - - return $this->subscriber_count; + return $count; } /** diff --git a/classes/Profile_tag.php b/classes/Profile_tag.php index d7841bd8ca..00585280d3 100644 --- a/classes/Profile_tag.php +++ b/classes/Profile_tag.php @@ -291,4 +291,26 @@ class Profile_tag extends Memcached_DataObject } return true; } + + function insert() + { + $result = parent::insert(); + if ($result) { + self::blow('profile_list:tagged_count:%d:%s', + $this->tagger, + $this->tag); + } + return $result; + } + + function delete() + { + $result = parent::delete(); + if ($result) { + self::blow('profile_list:tagged_count:%d:%s', + $this->tagger, + $this->tag); + } + return $result; + } } diff --git a/classes/Profile_tag_subscription.php b/classes/Profile_tag_subscription.php index f666fe51a0..c8b136da60 100644 --- a/classes/Profile_tag_subscription.php +++ b/classes/Profile_tag_subscription.php @@ -102,4 +102,24 @@ class Profile_tag_subscription extends Memcached_DataObject Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile)); } } + + function insert() + { + $result = parent::insert(); + if ($result) { + self::blow('profile_list:subscriber_count:%d', + $this->profile_tag_id); + } + return $result; + } + + function delete() + { + $result = parent::delete(); + if ($result) { + self::blow('profile_list:subscriber_count:%d', + $this->profile_tag_id); + } + return $result; + } }