Skip to content

Commit 4f0e550

Browse files
author
RedEnchilada
committed
Cache notification list
1 parent d74a164 commit 4f0e550

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

plugins/Notification/NotificationPlugin.php

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ function onEndShowFooter($action) {
259259
$note->user_id = $user->id;
260260
$note->type = 'grouppost';
261261
$note->arg = $action->group->id;
262-
$note->delete();
262+
if ($note->delete()) {
263+
// Clear the user's notification cache
264+
$cache = Cache::instance();
265+
$key = Cache::key('usernotifications:'.$user->id);
266+
$cache->delete($key);
267+
}
263268

264269
return true;
265270
}
@@ -269,7 +274,12 @@ function onEndShowFooter($action) {
269274
$note->user_id = $user->id;
270275
$note->type = 'groupjoin';
271276
$note->arg = $action->group->id;
272-
$note->delete();
277+
if ($note->delete()) {
278+
// Clear the user's notification cache
279+
$cache = Cache::instance();
280+
$key = Cache::key('usernotifications:'.$user->id);
281+
$cache->delete($key);
282+
}
273283

274284
return true;
275285
}
@@ -279,7 +289,12 @@ function onEndShowFooter($action) {
279289
$note->user_id = $user->id;
280290
$note->type = 'grouprequest';
281291
$note->arg = $action->group->id;
282-
$note->delete();
292+
if ($note->delete()) {
293+
// Clear the user's notification cache
294+
$cache = Cache::instance();
295+
$key = Cache::key('usernotifications:'.$user->id);
296+
$cache->delete($key);
297+
}
283298

284299
return true;
285300
}
@@ -288,7 +303,12 @@ function onEndShowFooter($action) {
288303
$note = new User_notification();
289304
$note->user_id = $user->id;
290305
$note->type = 'subscribe';
291-
$note->delete();
306+
if ($note->delete()) {
307+
// Clear the user's notification cache
308+
$cache = Cache::instance();
309+
$key = Cache::key('usernotifications:'.$user->id);
310+
$cache->delete($key);
311+
}
292312

293313
return true;
294314
}
@@ -297,7 +317,12 @@ function onEndShowFooter($action) {
297317
$note = new User_notification();
298318
$note->user_id = $user->id;
299319
$note->type = 'message';
300-
$note->delete();
320+
if ($note->delete()) {
321+
// Clear the user's notification cache
322+
$cache = Cache::instance();
323+
$key = Cache::key('usernotifications:'.$user->id);
324+
$cache->delete($key);
325+
}
301326

302327
return true;
303328
}
@@ -306,7 +331,12 @@ function onEndShowFooter($action) {
306331
$note = new User_notification();
307332
$note->user_id = $user->id;
308333
$note->type = 'mention';
309-
$note->delete();
334+
if ($note->delete()) {
335+
// Clear the user's notification cache
336+
$cache = Cache::instance();
337+
$key = Cache::key('usernotifications:'.$user->id);
338+
$cache->delete($key);
339+
}
310340

311341
return true;
312342
}
@@ -316,17 +346,37 @@ function onEndShowFooter($action) {
316346
$note->user_id = $user->id;
317347
$note->type = 'mention';
318348
$note->arg = $action->notice->id;
319-
$note->delete();
349+
if ($note->delete()) {
350+
// Clear the user's notification cache
351+
$cache = Cache::instance();
352+
$key = Cache::key('usernotifications:'.$user->id);
353+
$cache->delete($key);
354+
}
320355
$note->type = 'favorite';
321-
$note->delete();
356+
if ($note->delete()) {
357+
// Clear the user's notification cache
358+
$cache = Cache::instance();
359+
$key = Cache::key('usernotifications:'.$user->id);
360+
$cache->delete($key);
361+
}
322362
$note->type = 'repeat';
323-
$note->delete();
363+
if ($note->delete()) {
364+
// Clear the user's notification cache
365+
$cache = Cache::instance();
366+
$key = Cache::key('usernotifications:'.$user->id);
367+
$cache->delete($key);
368+
}
324369

325370
$note = new User_notification();
326371
$note->user_id = $user->id;
327372
$note->type = 'grouppost';
328373
$note->arg2 = $action->notice->id;
329-
$note->delete();
374+
if ($note->delete()) {
375+
// Clear the user's notification cache
376+
$cache = Cache::instance();
377+
$key = Cache::key('usernotifications:'.$user->id);
378+
$cache->delete($key);
379+
}
330380

331381
return true;
332382
}
@@ -381,7 +431,7 @@ function onEndShowScripts($action) {
381431
if(!User_notification_settings::isEnabled($user->id))
382432
return true;
383433
$action->script($this->path('notify.js'));
384-
$action->inlineScript('SNNote.init('.json_encode(User_notification::getAllForUser($user))
434+
$action->inlineScript('SNNote.init('.(User_notification::getCachedNotes($user))
385435
.', '.json_encode(array('updateUrl' => common_local_url('getnotificationjson'),
386436
'removeUrl' => common_local_url('removenotifications'),
387437
'openInNewWindow' => (User_notification_settings::openInNewWindow($user->id) ? true : false),

plugins/Notification/User_notification.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ function keyTypes()
7575
{
7676
return array('id' => 'K');
7777
}
78+
79+
// Get notifications cached as pre-encoded JSON
80+
static function getCachedNotes($user) {
81+
$cache = Cache::instance();
82+
83+
if (empty($cache)) // No caching... just get them straight
84+
return json_encode(User_notification::getAllForUser($user));
85+
86+
$key = Cache::key('usernotifications:'.$user->id);
87+
88+
$json = $cache->get($key);
89+
90+
if ($json !== false) // Cache hit! Woohoo!
91+
return $json;
92+
93+
// Nope. Generate new ones and store them
94+
95+
$json = json_encode(User_notification::getAllForUser($user));
96+
97+
$result = $cache->set($key, $json);
98+
99+
return $json;
100+
}
78101

79102
// Return an array of notification information, ready to be JSON-encoded and sent to the user
80103
// Return false if no notifications
@@ -198,6 +221,12 @@ static function saveNew($from, $to, $type, $arg1 = null, $arg2 = null) {
198221
$notify->arg2 = $arg2;
199222
$notify->created = time();
200223
$notify->insert();
224+
225+
// Clear the user's notification cache
226+
$cache = Cache::instance();
227+
$key = Cache::key('usernotifications:'.$to->id);
228+
$cache->delete($key);
229+
201230
return $notify;
202231
}
203232
}

plugins/Notification/removenotifications.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ function handle($args)
7878

7979
$user = common_current_user();
8080

81+
// Clear the user's notification cache
82+
$cache = Cache::instance();
83+
$key = Cache::key('usernotifications:'.$user->id);
84+
$cache->delete($key);
85+
8186
$notes = $this->trimmed('notifications');
8287
$notes = explode(',', $notes);
8388

0 commit comments

Comments
 (0)