summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
Diffstat (limited to 'classes')
-rw-r--r--classes/Notice.php136
-rw-r--r--classes/Notice_inbox.php51
-rw-r--r--classes/User.php18
3 files changed, 178 insertions, 27 deletions
diff --git a/classes/Notice.php b/classes/Notice.php
index 27b98de1c..6a7522bd3 100644
--- a/classes/Notice.php
+++ b/classes/Notice.php
@@ -285,7 +285,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 +307,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 +352,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,27 +613,80 @@ 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)
{
+ $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
+ array(),
+ 'public',
+ $offset, $limit, $since_id, $before_id, $since);
+
+ return Notice::getStreamByIds($ids);
+ }
+
+ function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
+ {
+ $notice = new Notice();
- $parts = array();
+ $notice->selectAdd(); // clears it
+ $notice->selectAdd('id');
- $qry = 'SELECT * FROM notice ';
+ $notice->orderBy('id DESC');
+
+ if (!is_null($offset)) {
+ $notice->limit($offset, $limit);
+ }
if (common_config('public', 'localonly')) {
- $parts[] = 'is_local = 1';
+ $notice->whereAdd('is_local = 1');
} else {
# -1 == blacklisted
- $parts[] = 'is_local != -1';
+ $notice->whereAdd('is_local != -1');
+ }
+
+ if ($since_id != 0) {
+ $notice->whereAdd('id > ' . $since_id);
}
- if ($parts) {
- $qry .= ' WHERE ' . implode(' AND ', $parts);
+ if ($before_id != 0) {
+ $notice->whereAdd('id < ' . $before_id);
+ }
+
+ if (!is_null($since)) {
+ $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
+ }
+
+ $ids = array();
+
+ if ($notice->find()) {
+ while ($notice->fetch()) {
+ $ids[] = $notice->id;
+ }
}
- return Notice::getStream($qry,
- 'public',
- $offset, $limit, $since_id, $before_id, null, $since);
+ $notice->free();
+ $notice = NULL;
+
+ return $ids;
}
function addToInboxes()
@@ -947,4 +1000,59 @@ class Notice extends Memcached_DataObject
array('notice' => $this->id));
}
}
+
+ function stream($fn, $args, $cachekey, $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) > NOTICE_CACHE_WINDOW) {
+ return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
+ $before_id, $since)));
+ }
+
+ $idkey = common_cache_key($cachekey);
+
+ $idstr = $cache->get($idkey);
+
+ if (!empty($idstr)) {
+ // Cache hit! Woohoo!
+ $window = explode(',', $idstr);
+ $ids = array_slice($window, $offset, $limit);
+ return $ids;
+ }
+
+ $laststr = $cache->get($idkey.';last');
+
+ if (!empty($laststr)) {
+ $window = explode(',', $laststr);
+ $last_id = $window[0];
+ $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
+ $last_id, 0, 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 = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
+ 0, 0, null)));
+
+ $windowstr = implode(',', $window);
+
+ $result = $cache->set($idkey, $windowstr);
+ $result = $cache->set($idkey . ';last', $windowstr);
+
+ $ids = array_slice($window, $offset, $limit);
+
+ return $ids;
+ }
}
diff --git a/classes/Notice_inbox.php b/classes/Notice_inbox.php
index 81ddb4538..dec14b0d1 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,47 @@ 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, $limit, $since_id, $before_id, $since)
+ {
+ return Notice::stream(array('Notice_inbox', '_streamDirect'),
+ array($user_id),
+ 'notice_inbox:by_user:'.$user_id,
+ $offset, $limit, $since_id, $before_id, $since);
+ }
+
+ 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 3b9b5cd83..ce7ea1464 100644
--- a/classes/User.php
+++ b/classes/User.php
@@ -444,21 +444,17 @@ class User extends Memcached_DataObject
'SELECT notice.* ' .
'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
'WHERE subscription.subscriber = %d ';
- $order = null;
+ return Notice::getStream(sprintf($qry, $this->id),
+ 'user:notices_with_friends:' . $this->id,
+ $offset, $limit, $since_id, $before_id,
+ $order, $since);
} else if ($enabled === true ||
($enabled == 'transitional' && $this->inboxed == 1)) {
- $qry =
- 'SELECT notice.* ' .
- 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
- 'WHERE notice_inbox.user_id = %d ';
- // NOTE: we override ORDER
- $order = null;
+ $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
+
+ return Notice::getStreamByIds($ids);
}
- return Notice::getStream(sprintf($qry, $this->id),
- 'user:notices_with_friends:' . $this->id,
- $offset, $limit, $since_id, $before_id,
- $order, $since);
}
function blowFavesCache()