- In-memory database helps in big data management
- It allows real-time updates that enable businesses to gain access to their data as it happens.
- It is used with applications that allow very fast data access, storage and manipulation.
- In-memory database not only facilitates faster query response times, but at the same time, also reduces or totally eliminates the need of data indexing and storing pre-aggregated data in OLAP cubes or aggregate tables.
Redis
Redis is an open-source in-memory database project implementing a distributed, in-memory key-value store with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, hyperlog, bitmaps and spatial indexes. For more details please visit
Redis In-Memory Database.
In order to use Redis in-memory database in our application we have to first install Redis database. You can download it from here
Download latest version of Redis In-Memory Database. Once we are ready with installation let's create one simple console application to see how it works.
Step 1
Open Visual Studio -- here I am using Microsoft Visual Studio 2015 Community Edition. Click on File - New - Project. Here for demo purposes I am creating a console application.
Step 2
To connect with Redis we have to first download Redis Client from Nuget Package. In solution explorer right click on references and click on Manage NuGet Packages. It will open NuGet Package Manager, in that browse for ServiceStack.Redis.Complete and click on install.
Step 3
Now that we are ready with Redis environment let's do some code. Here we will create two static functions, one for inserting value into the Redis DB and the second one to read it from the DB.
To store data into the Redis database we are using the below code.
-
-
-
-
-
-
-
- private static bool Save(string host, string key, string value)
- {
- using (var objRedisClient = new RedisClient(host))
- {
- if (objRedisClient.Get<string>(key) == null)
- {
- return objRedisClient.Set<String>(key, value);
- }
- else
- {
- return false;
- }
- }
- }
To read details from Redis database:
-
-
-
-
-
-
- private static string Get(string host, string key)
- {
- using(var objRedisClient = new RedisClient(host))
- {
- return objRedisClient.Get<String>(key);
- }
- }
We will test this by making a call to these two functions:
- static void Main(string[] args)
- {
- Save("localhost", "Key1", "Value1");
- var result = Get("localhost", "Key1");
- }
Here is the result when you debug this code
In this article we have seen basic implementation of Redis in-memory database. You can download sample application source code attached to this article. I hope you enjoyed reading this article. Keep reading and don't forget to post your valuable feedback.