Making Notification Center with Redis Cache

In your web service there is a time you want to let user know that they have got comment on their post, got message, got news etc.  Like ones that I see on top right at Facebook, Twitter.  Today I want to make the same thing using Redis Cache.

In-memory cache is a good choice when it comes to frequently accessed and disposable data.  Redis Cache is even nice that they can store structure data.  For C#ers, you can define data model and just insert up and fetch from cache into the model.

The scenario is that someone do some action, and I create notification data to applicable users.

The model I am using today, is here at https://7thdata.com/en-US/Schema/P/Notification.  This model just describe the data record level of notification, to make if more fetch friendly, I am going to create container data structure per users to make it simpler, faster, lighter to retrieve per user base notification data.

My data model looks like below;

Now that defining data model is done.  Let’s first create class that handles access to cache.  I am going to go ahead and make a class called NotifactionHandler.cs.  This class will handle all notification related actions.  And RCache.cs to handle all in and out of Redis Cache.

RCache.cs looks like below;

This basically create connection to Redis Cache and ready for you to use.

Now that you have this, you can simply call and set data into the cache as below;

You have to name the cache record, and have to be unique + identifiable.  I am setting string “notification-” and notification data ID to follow.  This should create unique record.

When I am about to insert new notification data to the cache, I want to check and see if the notification data set already exist for specific user. If exist then I want to add new record, if not then I want to create new data-set.

Here is code to fetch cache data.

Now that it is ready to set data into the cache and read, let’s call this from the action of the web service.

Let’s say when someone leave comment on your log, triggers notification.  I have all of “leave comment”, “send message action” under one controller called TalkController.cs. Inside TalkController.cs there is a function to create comment.  Let’s call the notification handler from here.  We will cover how this whole message is handled later, for now don’t worry about how I handle messages in this function.

Here is my function to leave comment on log.

Here you can see that I am calling SendLogCommentNotification after creating new comment for the log. And my SendLogCommentNotication is,

I already have feature to send notification email for people who has notification on, but never mind this part and take a look at SendNotificationFlag, which is below;

Lots of lines, but you can see  that I check to see if the record exist in cache using

then after I compose data-set I call

to set new data-set.

Now every time when someone leave comment on log, the applicable users should get notification data-set inserted into their notification data-set.

Let’s take a look.

I am creating new controller called NotifcationController.cs, and also create views Index, _NotificationList.  Index will show the full list of notification with status “New”, and _NotificationList is a partial view that show the most recent 10 of notifications.

Here is my ActionResult for Index

and here is my View

Now let’s set a link to the page and see if I can insert notification data.

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_101016_064520_pm

OK I have nothing, so I see “No notification”. Good.

Now I go ahead and leave comment on someone, then check from that person’s profile.  Then go to the same notification page.

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_101016_110224_pm

Great, working.

Now, let’s finish it off for now by creating _NotificatioList, partial view that will be called from the navigation, and be part of dropdown list.  My reference for the dropdown list is http://v4-alpha.getbootstrap.com/components/dropdowns/.

Here is my partial view _NotificationList.cshtml

And here is my ActionResult _NotificationList, which is same as Index, just returning partial view.

And here is part of my _Layout.cshtml , which is the top navigation part of the page.

Now I should see 1 notification and have popup pulldown menu with message.

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_101016_114708_pm

Great! working.  Now you can customize link, looks etc.  And also change flag to NOT new then it will not appear it or count as notification.  You can also plug this into to SignalR and make it push to the user from the server.

 

Leave a Reply