summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Prodromou <evan@controlyourself.ca>2009-04-28 13:07:05 -0400
committerEvan Prodromou <evan@controlyourself.ca>2009-04-28 13:07:05 -0400
commitc7105c2af1ebe3cddd477265c6fea59a61d0c7e5 (patch)
tree51a00bae83d11b8e66d707e3d17d86aeb6b51847
parentce5b98ca016841edf920ebc062f1b4d53488762c (diff)
Change to avoid a join in notice inbox
The join in notice_inbox is causing temp-table sorting on identi.ca, so I'm trying a finer-tuned approach.
-rw-r--r--classes/Notice.php31
-rw-r--r--classes/Notice_inbox.php101
-rw-r--r--classes/User.php29
3 files changed, 127 insertions, 34 deletions
diff --git a/classes/Notice.php b/classes/Notice.php
index 27b98de1c..b087c94bc 100644
--- a/classes/Notice.php
+++ b/classes/Notice.php
@@ -215,6 +215,7 @@ class Notice extends Memcached_DataObject
if (common_config('queue', 'enabled')) {
$notice->blowAuthorCaches();
} else {
+ common_debug("Blowing caches for new notice.");
$notice->blowCaches();
}
}
@@ -285,7 +286,7 @@ class Notice extends Memcached_DataObject
// Clear the user's cache
$cache = common_memcache();
if (!empty($cache)) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $this->profile_id));
+ $cache->delete(common_cache_key('notice_inbox:by_user:'.$this->profile_id));
}
$this->blowNoticeCache($blowLast);
$this->blowPublicCache($blowLast);
@@ -307,9 +308,9 @@ class Notice extends Memcached_DataObject
$member->group_id = $group_inbox->group_id;
if ($member->find()) {
while ($member->fetch()) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id));
+ $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
if ($blowLast) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id . ';last'));
+ $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
}
}
}
@@ -352,9 +353,9 @@ class Notice extends Memcached_DataObject
'WHERE subscription.subscribed = ' . $this->profile_id);
while ($user->fetch()) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
+ $cache->delete(common_cache_key('notice_inbox:by_user:'.$user_id));
if ($blowLast) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id . ';last'));
+ $cache->delete(common_cache_key('notice_inbox:by_user:'.$user_id.';last'));
}
}
$user->free();
@@ -613,6 +614,26 @@ class Notice extends Memcached_DataObject
return $wrapper;
}
+ function getStreamByIds($ids)
+ {
+ $cache = common_memcache();
+
+ if (!empty($cache)) {
+ $notices = array();
+ foreach ($ids as $id) {
+ $notices[] = Notice::staticGet('id', $id);
+ }
+ return new ArrayWrapper($notices);
+ } else {
+ $notice = new Notice();
+ $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
+ $notice->orderBy('id DESC');
+
+ $notice->find();
+ return $notice;
+ }
+ }
+
function publicStream($offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
{
diff --git a/classes/Notice_inbox.php b/classes/Notice_inbox.php
index 81ddb4538..162da74fe 100644
--- a/classes/Notice_inbox.php
+++ b/classes/Notice_inbox.php
@@ -1,7 +1,7 @@
<?php
/*
* Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, Controlez-Vous, Inc.
+ * Copyright (C) 2008, 2009, Control Yourself, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@@ -21,7 +21,11 @@ if (!defined('LACONICA')) { exit(1); }
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
-class Notice_inbox extends Memcached_DataObject
+// We keep 5 pages of inbox notices in memcache, +1 for pagination check
+
+define('INBOX_CACHE_WINDOW', 101);
+
+class Notice_inbox extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
@@ -38,4 +42,97 @@ class Notice_inbox extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
+
+ function stream($user_id, $offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
+ {
+ $cache = common_memcache();
+
+ if (empty($cache) ||
+ $since_id != 0 || $before_id != 0 || !is_null($since) ||
+ ($offset + $limit) > INBOX_CACHE_WINDOW) {
+ common_debug('Doing direct DB hit for notice_inbox since the params are screwy.');
+ return Notice_inbox::_streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since);
+ }
+
+ $idkey = common_cache_key('notice_inbox:by_user:'.$user_id);
+
+ $idstr = $cache->get($idkey);
+
+ if (!empty($idstr)) {
+ // Cache hit! Woohoo!
+ common_debug('Cache hit for notice_inbox.');
+ $window = explode(',', $idstr);
+ $ids = array_slice($window, $offset, $limit);
+ return $ids;
+ }
+
+ $laststr = common_cache_key($idkey.';last');
+
+ if (!empty($laststr)) {
+ common_debug('Cache hit for notice_inbox on last item.');
+
+ $window = explode(',', $laststr);
+ $last_id = $window[0];
+ $new_ids = Notice_inbox::_streamDirect($user_id, 0, INBOX_CACHE_WINDOW,
+ $last_id, null, null);
+
+ $new_window = array_merge($new_ids, $window);
+
+ $new_windowstr = implode(',', $new_window);
+
+ $result = $cache->set($idkey, $new_windowstr);
+ $result = $cache->set($idkey . ';last', $new_windowstr);
+
+ $ids = array_slice($new_window, $offset, $limit);
+
+ return $ids;
+ }
+
+ $window = Notice_inbox::_streamDirect($user_id, 0, INBOX_CACHE_WINDOW,
+ null, null, null);
+
+ $windowstr = implode(',', $new_window);
+
+ $result = $cache->set($idkey, $windowstr);
+ $result = $cache->set($idkey . ';last', $windowstr);
+
+ $ids = array_slice($window, $offset, $limit);
+
+ return $ids;
+ }
+
+ function _streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since)
+ {
+ $inbox = new Notice_inbox();
+
+ $inbox->user_id = $user_id;
+
+ if ($since_id != 0) {
+ $inbox->whereAdd('notice_id > ' . $since_id);
+ }
+
+ if ($before_id != 0) {
+ $inbox->whereAdd('notice_id < ' . $before_id);
+ }
+
+ if (!is_null($since)) {
+ $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
+ }
+
+ $inbox->orderBy('notice_id DESC');
+
+ if (!is_null($offset)) {
+ $inbox->limit($offset, $limit);
+ }
+
+ $ids = array();
+
+ if ($inbox->find()) {
+ while ($inbox->fetch()) {
+ $ids[] = $inbox->notice_id;
+ }
+ }
+
+ return $ids;
+ }
}
diff --git a/classes/User.php b/classes/User.php
index 098381f73..ce7ea1464 100644
--- a/classes/User.php
+++ b/classes/User.php
@@ -451,34 +451,9 @@ class User extends Memcached_DataObject
} else if ($enabled === true ||
($enabled == 'transitional' && $this->inboxed == 1)) {
- $cache = common_memcache();
+ $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
- if (!empty($cache)) {
-
- # Get the notices out of the cache
-
- $notices = $cache->get(common_cache_key($cachekey));
-
- # On a cache hit, return a DB-object-like wrapper
-
- if ($notices !== false) {
- $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
- return $wrapper;
- }
- }
-
- $inbox = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
-
- $ids = array();
-
- while ($inbox->fetch()) {
- $ids[] = $inbox->notice_id;
- }
-
- $inbox->free();
- unset($inbox);
-
- return Notice::getStreamByIds($ids, 'user:notices_with_friends:' . $this->id);
+ return Notice::getStreamByIds($ids);
}
}