ASP.NET WebAPI - Basic Redis
Introduction
This article walks you through configuring Redis and making basic operations using .net C# client.
Redis is one of the fastest and feature-rich key-value stores to come from the NoSQL movement. It is similar to memcached but the dataset is not volatile, and values can either be strings lists, sets, sorted sets or hashes.
You can download the Redis Client in any one of the following ways:
- Packaged by default in ServiceStack.dll
- Available to download separately as a stand-alone ServiceStack.Redis.dll
- As Source Code via Git: git clone git://github.com/ServiceStack/ServiceStack.Redis.git
- For those interested in having a GUI admin tool to visualize your Redis data should check out the Redis Admin UI
STEP 1 - Create ASP.NET WebAPI 2 Application
I will be using Visual Studio 2013 as my development environment. Our first step will be to create an ASP.NET Web Application project based on the Web API template:
- Open Visual Studio 2013 and create a new project of type ASP.NET Web Application.
- On this project I create a solution called WebAPI.
http://code.msdn.microsoft.com/site/view/file/125453/1/1.png
- Press OK, and a new screen will appear, with several options of template to use on our project.
- Select the option WebAPI.
http://code.msdn.microsoft.com/site/view/file/125454/1/2.png
- The solution will be created.
STEP 2 - Install Nuget
Now in order to use Redis as CacheManager we need to install a Nuget package.
So on the Visual Studio 2013, select the follow menu option:
- Tools-> Library Package manager -> Manage NuGet Packages for Solution
- Search for Redis and select the option Install.
http://code.msdn.microsoft.com/site/view/file/125455/1/3.png
This option, will install automatically the Nuget Package.
STEP 3 - Start Redis
First download the latest .exe package from here https://github.com/rgl/redis/downloads (choose the appropriate latest 32 or 64 bit version).
Run the redis-server.exe executable file. This will start redis in command line.
As you see the redis is now running on port 6379 on local machine.
http://code.msdn.microsoft.com/site/view/file/125456/1/4.png
STEP 4 - Create basic Redis class
C#
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebApi2.Controllers
{
public class RedisController : ApiController
{
// GET: api/Redis/key
public string Get(string key)
{
using (var redis = new RedisClient("localhost", 6379))
{
return redis.GetEntry(key);
}
}
// POST: api/Redis
public void Post(string key, string keyValue)
{
using (var redis = new RedisClient("localhost", 6379))
{
redis.SetEntry(key, keyValue);
}
}
}
}
STEP 5 - Test application
Use postman to post some values into Redis.
http://code.msdn.microsoft.com/site/view/file/125457/1/5.png
Call api/Redis and verify that the return is the 10, value send to the key "Test", on postman.
http://code.msdn.microsoft.com/site/view/file/125458/1/6.png
Resources
Some good resources about Signal could be found here:
- My personal blog: http://joaoeduardosousa.wordpress.com/
- ServiceStack: https://github.com/ServiceStack/ServiceStack.Redis/wiki/IRedisClient
- Download Code: ASP.NET Web Api - Basic Redis