Click here to Skip to main content
15,884,836 members
Articles / Web Development / ASP.NET
Tip/Trick

ASP.NET WebAPI - Basic Redis

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
5 Oct 2014CPOL2 min read 19.6K   5   5
This article walks you through configuration Redis and made 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,

Source Code: http://code.msdn.microsoft.com/ASPNET-WebAPI-Basic-Redis-ade42ca7

This article walks you through configuration Redis and made 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.

  • Press OK, and a new screen will appear, with several options of template to use on our project.
  • Select the option WebAPI.

  • 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.

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.

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 aplication

Use postman to post some values into Redis.

Call api/Redis and verify that the return is the 10, value send to the key "Test", on postman.

Resources

Some good resources about Signal could be found here:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Devscope
Portugal Portugal
I am João Sousa, and since i finish my degree I’m working in software development using Microsoft technologies.

I was awarded

Microsoft Most Valuable Professional (MVP) 2015 – .Net

My profissional profile:

Azure Developer
.NET Developer

My Certifications:

MCTS - .NET Framework - Application Development Foundation
MCTS - .NET Framework 2.0 - Windows-based Client Development
MCTS - .NET Framework 3.5 ADO.NET Applications
MCTS - .NET Framework 3.5 ASP.NET Applications
MCSD - Programming in HTML5 with JavaScript and CSS3
MCSD - Developing ASP.NET MVC 4 Web Applications
MCSD - Developing Windows Azure and Web Services
MCSA Office 365 - Managing Office 365 Identities and Requirements
MCSA Office 365 - Enabling Office 365 Services
MCSD - Implementing Microsoft Azure Infrastructure Solutions

Comments and Discussions

 
QuestionUm no Pin
Sacha Barber5-Oct-14 22:33
Sacha Barber5-Oct-14 22:33 
AnswerRe: Um no Pin
Kashif_Imran6-Oct-14 1:09
Kashif_Imran6-Oct-14 1:09 
Also, the inline styling is making the code look like a mess. BTW, if the screenshots and code is removed, there will hardly be any article left.
AnswerRe: Um no Pin
Sacha Barber6-Oct-14 2:55
Sacha Barber6-Oct-14 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.