3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
24 // We keep 5 pages of inbox notices in memcache, +1 for pagination check
26 define('INBOX_CACHE_WINDOW', 101);
28 class Notice_inbox extends Memcached_DataObject
31 /* the code below is auto generated do not remove the above tag */
33 public $__table = 'notice_inbox'; // table name
34 public $user_id; // int(4) primary_key not_null
35 public $notice_id; // int(4) primary_key not_null
36 public $created; // datetime() not_null
37 public $source; // tinyint(1) default_1
40 function staticGet($k,$v=null)
41 { return Memcached_DataObject::staticGet('Notice_inbox',$k,$v); }
43 /* the code above is auto generated do not remove the tag below */
46 function stream($user_id, $offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
48 $cache = common_memcache();
51 $since_id != 0 || $before_id != 0 || !is_null($since) ||
52 ($offset + $limit) > INBOX_CACHE_WINDOW) {
53 common_debug('Doing direct DB hit for notice_inbox since the params are screwy.');
54 return Notice_inbox::_streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since);
57 $idkey = common_cache_key('notice_inbox:by_user:'.$user_id);
59 $idstr = $cache->get($idkey);
63 common_debug('Cache hit for notice_inbox.');
64 $window = explode(',', $idstr);
65 $ids = array_slice($window, $offset, $limit);
69 $laststr = common_cache_key($idkey.';last');
71 if (!empty($laststr)) {
72 common_debug('Cache hit for notice_inbox on last item.');
74 $window = explode(',', $laststr);
75 $last_id = $window[0];
76 $new_ids = Notice_inbox::_streamDirect($user_id, 0, INBOX_CACHE_WINDOW,
77 $last_id, null, null);
79 $new_window = array_merge($new_ids, $window);
81 $new_windowstr = implode(',', $new_window);
83 $result = $cache->set($idkey, $new_windowstr);
84 $result = $cache->set($idkey . ';last', $new_windowstr);
86 $ids = array_slice($new_window, $offset, $limit);
91 $window = Notice_inbox::_streamDirect($user_id, 0, INBOX_CACHE_WINDOW,
94 $windowstr = implode(',', $new_window);
96 $result = $cache->set($idkey, $windowstr);
97 $result = $cache->set($idkey . ';last', $windowstr);
99 $ids = array_slice($window, $offset, $limit);
104 function _streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since)
106 $inbox = new Notice_inbox();
108 $inbox->user_id = $user_id;
110 if ($since_id != 0) {
111 $inbox->whereAdd('notice_id > ' . $since_id);
114 if ($before_id != 0) {
115 $inbox->whereAdd('notice_id < ' . $before_id);
118 if (!is_null($since)) {
119 $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
122 $inbox->orderBy('notice_id DESC');
124 if (!is_null($offset)) {
125 $inbox->limit($offset, $limit);
130 if ($inbox->find()) {
131 while ($inbox->fetch()) {
132 $ids[] = $inbox->notice_id;