Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cache notification list
  • Loading branch information
RedEnchilada committed Dec 20, 2014
1 parent d74a164 commit 4f0e550
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
72 changes: 61 additions & 11 deletions plugins/Notification/NotificationPlugin.php
Expand Up @@ -259,7 +259,12 @@ function onEndShowFooter($action) {
$note->user_id = $user->id;
$note->type = 'grouppost';
$note->arg = $action->group->id;
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

return true;
}
Expand All @@ -269,7 +274,12 @@ function onEndShowFooter($action) {
$note->user_id = $user->id;
$note->type = 'groupjoin';
$note->arg = $action->group->id;
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

return true;
}
Expand All @@ -279,7 +289,12 @@ function onEndShowFooter($action) {
$note->user_id = $user->id;
$note->type = 'grouprequest';
$note->arg = $action->group->id;
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

return true;
}
Expand All @@ -288,7 +303,12 @@ function onEndShowFooter($action) {
$note = new User_notification();
$note->user_id = $user->id;
$note->type = 'subscribe';
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

return true;
}
Expand All @@ -297,7 +317,12 @@ function onEndShowFooter($action) {
$note = new User_notification();
$note->user_id = $user->id;
$note->type = 'message';
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

return true;
}
Expand All @@ -306,7 +331,12 @@ function onEndShowFooter($action) {
$note = new User_notification();
$note->user_id = $user->id;
$note->type = 'mention';
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

return true;
}
Expand All @@ -316,17 +346,37 @@ function onEndShowFooter($action) {
$note->user_id = $user->id;
$note->type = 'mention';
$note->arg = $action->notice->id;
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}
$note->type = 'favorite';
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}
$note->type = 'repeat';
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

$note = new User_notification();
$note->user_id = $user->id;
$note->type = 'grouppost';
$note->arg2 = $action->notice->id;
$note->delete();
if ($note->delete()) {
// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);
}

return true;
}
Expand Down Expand Up @@ -381,7 +431,7 @@ function onEndShowScripts($action) {
if(!User_notification_settings::isEnabled($user->id))
return true;
$action->script($this->path('notify.js'));
$action->inlineScript('SNNote.init('.json_encode(User_notification::getAllForUser($user))
$action->inlineScript('SNNote.init('.(User_notification::getCachedNotes($user))
.', '.json_encode(array('updateUrl' => common_local_url('getnotificationjson'),
'removeUrl' => common_local_url('removenotifications'),
'openInNewWindow' => (User_notification_settings::openInNewWindow($user->id) ? true : false),
Expand Down
29 changes: 29 additions & 0 deletions plugins/Notification/User_notification.php
Expand Up @@ -75,6 +75,29 @@ function keyTypes()
{
return array('id' => 'K');
}

// Get notifications cached as pre-encoded JSON
static function getCachedNotes($user) {
$cache = Cache::instance();

if (empty($cache)) // No caching... just get them straight
return json_encode(User_notification::getAllForUser($user));

$key = Cache::key('usernotifications:'.$user->id);

$json = $cache->get($key);

if ($json !== false) // Cache hit! Woohoo!
return $json;

// Nope. Generate new ones and store them

$json = json_encode(User_notification::getAllForUser($user));

$result = $cache->set($key, $json);

return $json;
}

// Return an array of notification information, ready to be JSON-encoded and sent to the user
// Return false if no notifications
Expand Down Expand Up @@ -198,6 +221,12 @@ static function saveNew($from, $to, $type, $arg1 = null, $arg2 = null) {
$notify->arg2 = $arg2;
$notify->created = time();
$notify->insert();

// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$to->id);
$cache->delete($key);

return $notify;
}
}
5 changes: 5 additions & 0 deletions plugins/Notification/removenotifications.php
Expand Up @@ -78,6 +78,11 @@ function handle($args)

$user = common_current_user();

// Clear the user's notification cache
$cache = Cache::instance();
$key = Cache::key('usernotifications:'.$user->id);
$cache->delete($key);

$notes = $this->trimmed('notifications');
$notes = explode(',', $notes);

Expand Down

0 comments on commit 4f0e550

Please sign in to comment.