NCache, a distributed, linearly scalable and fast in-memory key-value store allows the caching of objects to reduce database trips and easily scale higher loads. Similarly, Redis is also a distributed caching solution but lacks key features like SQL & LINQ Support, Client Cache (InProc) and Peer-to-Peer Clustering etc. For .NET Redis client applications, NCache provides a wrapper API identical to StackExchange.Redis API so that users can easily move to NCache with minimal code changes.
Therefore, the actual step is to learn about the migration from Redis to NCache. This blog provides a step-wise guide on what needs to be done for a smooth migration from Redis to NCache.
Need of NCache Wrapper for Redis for .NET and .NET Core
With all being said above, now imagine a Redis client using a pure .NET/.NET Core application, who wants to port with minimal code change and added features. What should he do? Use NCache! It provides many features and a seamless migration from Redis to NCache.
However, for using NCache with your own Redis client application, you would have to learn a whole lot of NCache, its provided features and then incorporate NCache while also performing other functions. The application deployed will have to be re-tested again and again which would become a very costly and troublesome process.
So, NCache solved the above-mentioned problem very smartly; it implemented a wrapper over NCache API which is identical to that of StackExchange.Redis API. This is the standard StackExchange.Redis API due to which there is minimal code change required to your Redis client application in order to use it in a scalable manner with NCache.
How to Make Use of NCache Wrapper for Redis Easily
The best part about migrating from Redis to NCache is the ease of no code and no logic change required for the migration. The StackExchange.Redis API’s behavior is the same throughout the whole NCache integrated with Redis client application.
You as a user, just have to edit a few namespaces to provide access to the cache along with downloading the NuGet package and your whole application is good to go. The source code for this wrapper along with a sample application is uploaded on GitHub.
Step 1: Download NCache Redis Wrapper NuGet
NCache provides a NuGet package to be installed in your StackExchange.Redis application. The NuGet package contains the client.ncconf file that is used to configure the cache client to access NCache cluster from the client machine.
1 |
Install-Package NCache.StackExchange.Redis |
Step:2 Add the Namespace to your application
Add the following namespace, in your application and source files:
1 |
NCache.StackExchange.Redis |
Step 3: Connect to the Cache and the NCache Database
The next step is to connect to the cache through the ConnectionMultiplexer class which is the central object in the StackExchange.Redis API that hides away the details of multiple servers. Make sure the cache is running so that operations can be performed seamlessly. You can also learn how to create a cache from the step wise guide in NCache documentation.
1 |
ConnectionMultiplexer ncache = ConnectionMultiplexer.Connect(“cacheName”); |
Just like NCache provides an ICache interface to get an instance of an NCache’s cache, GetNCacheInteface is to get caching through the Redis interface.
1 |
ICache cache = ncache.GetNCacheInterface(“cacheName”); |
Just like accessing a Redis database, it is very simple to use NCache as a wrapper like written below.
1 |
var db = ncache.GetDatabase(); |
Also, along with the rest of your application you can continue using the StackExchange.Redis API the same way as you were using before without any code changes.
Once your application is connected to the NCache cache cluster, you can perform operations quite easily and that is it, your Redis application is now migrated to NCache. So, following just a few steps migrates your entire Redis application to NCache without any hassle.
Why NCache Wrapper for a Redis Client?
Using NCache for a Redis client application provides you with the following benefits:
-
- SQL Query and LINQ: NCache provides SQL and LINQ support for querying the cache based on object attributes, groups/subgroups, tags and named tags This makes it really easy to search for the data you have stored in the cache.
- Server-Side Code (.NET): NCache allows you to deploy server-side code to run on the cache servers in the cluster. A rich set of features are provided to let you develop and deploy server-side code in .NET. Some features are Read-through, Write-through and Cache Loader.
- Keeping the Cache Fresh (Dependencies): NCache makes sure your cache is always fresh and you can confidently rely on its data integrity. If the data in cache becomes stale, it is immediately updated to that of the database. NCache provides a list of features for this purpose like Synchronize Cache with Database and Dependencies unlike Redis which gives only through Expirations.
- Cache Performance (Client Cache (InProc Speed)): NCache offers Client Cache (Near Cache) which is basically a local cache very close to your application (InProc/OutProc). It gives your application InProc speed, meaning a faster retrieval of objects.
Conclusion
To sum it up, what we learned from the blog is how NCache wrapper lets you migrate your entire Redis application to NCache by following a series of simple steps. You can easily use NCache’s features by just using the NCache Redis wrapper with no code change and the original StackExchange.Redis API. There are a lot of other features provided by NCache that enhance your application’s performance and help you achieve the scalability you wish for.
NCache Details Download NCache Edition Comparison