Redis has a very nice feature called Pub/Sub. You can publish an event when a key value change and other applications can subscribe to this event. You can use this feature to keep your in memory cache always fresh.
InMemory cache is the fastest cache and very critical for performance of your application. But the problem with in memory cache is its not possible to clear your cache when the data change in original source (i:e database).
I am trying to explain the whole idea using an example. Suppose your application displays list of categories with count (number of books in each category) in websites to allow users browse books by category. As categories do not change that much and we don’t add books every minute so your team decided to put this in memory cache for 30 minutes. Which works fine but sometimes when you add a new book under a category the count didn’t update in website immediately as they cached in memory for 30 minutes. The update could take maximum 30 minutes to display in browser. Because your application deployed in web farm all individual instance has its own in memory cache of this data.
Caching is a very important part of scaling your website. In this blog post I am trying to show how you can implement a two level caching. One Redis for distributed caching and the other one is inmemory caching. So the idea is first you look whether data exists in memory. If not then check whether the data exists in Redis. If not then load from your Sql Data source. And then save this in memory and Redis. So for next request we can serve the data from memory, if not from Redis. Continue reading
Its a very common feature, specially in eCommerce site to display a list of recently viewed items. The idea is the application keep track of a certain number of items user viewed and display them. This helps user to quickly view any items that he/she previously viewed without searching the store again.
Lets say we have to store maximum 5 recent items. So anytime user view details of an item then we need to save that item against user id and when we display the list of recently viewed items, that item should be at top. If a user view an item that he/she viewed before than instead of adding that item we need move the item to top. After adding an item we also need to check how many items added. If the maximum items per user reached then we need to remove the item that has been viewed by user least recently. You most probably don’t want to store those recent items forever. At the same time you don’t want to expire them at any specific future time. The lifespan of users recent items needs to slide as the user browse. Continue reading
Redis is an open source, in-memory advanced key-value store with optional persistence to disk. It’s high performance and support for 5 different data-sets makes Redis very attractive technology for different applications. One of the very good use case of Redis is using as cache server. In this article I will try to show how we can use Redis as simple key value cache server. I assume you already have Redis installed on your machine. We will use the Redis client library named StackExchange.Redis. Continue reading