Click here to Skip to main content
15,867,330 members
Articles / Database Development / NoSQL

Redis VB.NET Part I: Introduction to Using Redis (NoSQL) with .NET

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
29 Apr 2011CPOL2 min read 51.3K   11.2K   15   10
Create a usable redis component to enable usage of the redis storage system

Introduction

Having just recently stepped into the lucrative and extremely exhausting world of software designing, a recent search journey of mine (which usually starts with Google and ends at CodeProject) left me in the middle of a minefield. A minefield that is so seductively luring, encompassing so many functionalities that I began to wonder - how do people survive without it? I am talking about the NoSQL key-value store called Redis. I'd suggest exploring the site a little bit before continuing with this article. In this article, I am going to enable you to create a very simple redis component (a Windows console application) that accepts a string from the user and stores it in redis. I will also describe how this stored string will be read by another application.

Background

The basic idea of this series of articles is to introduce the concept of using redis as a database for actual movement of data between a collection of loosely coupled components. Since loosely coupled components most necessarily need high cohesion, I hope to direct you into using redis for that purpose. The reason I jumped into the redis bandwagon was the ease with which data can be moved around faster and in a highly free floating environment. Be warned though that unlike conventional databases (such as Oracle, MySQL, SQL Server, etc.), redis is necessarily non persistent: the data IS LOST if the server is shut down. But no worries because redis supports persistence of its data, the condition being it happens at intervals or when manually requested. Now with these things in mind, let's proceed.

Using the Code

To use this code, create a Windows Console Application Project in VB.NET. (Use .NET 3.5). I wouldn't recommend using .NET 4 as the redis library was written in 3.5 and there are certain unresolved issues when using the library in a .NET 4 project. The prerequisites for this are:

  1. Basic programming knowledge in VB.NET - I am not going to explain how you are to get the user input.
  2. The Redis library files for the project (ServiceStack)
  3. The Redis server executable (Google Code)
  4. A leap of faith

Create the project. Add references to the all the DLLs in the ServiceStack Library. Accept the user input into a variable.

VB.NET
Imports ServiceStack.Redis
''' <summary>
''' The redis store encapsulation class around the ServiceStack redis client 
''' </summary>
''' <remarks>This class is cumulatively constructed across the tutorial 
''' and is not broken.
''' </remarks>
Public Class RedisStore

#Region " Properties "
     Private _sourceClient As RedisClient
    Public ReadOnly Property SourceClient() As RedisClient
        Get
            Return _sourceClient
        End Get
    End Property

#End Region

#Region " Constructors "
     Public Sub New()
        MyClass.New(False)
    End Sub

    Public Sub New(ByVal ForceCheckServer As Boolean)
        _sourceClient = New RedisClient
        If ForceCheckServer AndAlso Not IsServerAlive() Then
            Throw New Exception("The server has not been started!")
        End If
    End Sub

#End Region

    Public Function IsServerAlive() As Boolean
        Try
            Return SourceClient.Ping
        Catch ex As Exception
            Return False
        End Try
    End Function

#Region " Functionalities "
 #Region " Get/Set Keys "
     Public Function SetKey(ByVal key As String, ByVal value As String) As Boolean
        Return SourceClient.Set(key, value)
    End Function

    Public Function SetKey(Of T)(ByVal key As String, ByVal value As T) As Boolean
        Return SourceClient.Set(Of T)(key, value)
    End Function

    Public Function GetKey(ByVal key As String) As String
        Return Helper.GetString(SourceClient.Get(key))
    End Function

    Public Function GetKey(Of T)(ByVal key As String) As T
        Return SourceClient.Get(Of T)(key)
    End Function

#End Region

#End Region

End Class

Public Class Helper

    Private Shared ReadOnly UTF8EncObj As New System.Text.UTF8Encoding()

    Public Shared Function GetBytes(ByVal source As Object) As Byte()
        Return UTF8EncObj.GetBytes(source)
    End Function

    Public Shared Function GetString(ByVal sourceBytes As Byte()) As String
        Return UTF8EncObj.GetString(sourceBytes)
    End Function

End Class

Pass the string onto the RedisStore class' SetKey function to store it and call the GetKey function to retrieve it. You will also notice that redis allows you to store an entire object in its store using the GetKey(Of T) function!.

Well, that's it for the first part of the tutorial. Have a go and let me know if I should continue onto using Redis as a PubSub medium, which happens to be one of the intended usages of redis, apart from being just a key value store.

History

  • 28th April, 2011: Initial version

License

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


Written By
Student
India India
Hariharan leapt into the field of programming after being introduced to C in his second year at college. A fan of basketball and chess, the logical thought process behind programming and the concepts of linking real principles onto a concrete platform pushed him deep into this field. On finishing his bachelor's majoring in Electrical and Electronics engineering with Soft Computing, Numerical Analysis and Biomedical Engineering as minor, he did a six month stint at India's second largest IT company- Infosys Technologies. He left Infosys armed with strong concepts of SDL Cycles and process development, gained domain knowledge in Java and had explored Visual basic, C++, C# and kept his mind open for more. Currently working in a startup as the product designer, his arsenal of technologies has doubled to accommodate the challenges his job demands.
Off work, he enjoys learning new languages (non-programming) and is currently teaching himself German and Spanish. He also enjoys travelling around and exploring new ideas, places and relationships.

Currently attempting to extend his education to post graduation.

Comments and Discussions

 
QuestionNo encuentra el servidor Pin
El Guru28-Aug-21 19:44
El Guru28-Aug-21 19:44 
AnswerRe: No encuentra el servidor Pin
El Guru29-Aug-21 4:43
El Guru29-Aug-21 4:43 
GeneralGood Intro Pin
Alireza_136221-Aug-13 20:22
Alireza_136221-Aug-13 20:22 
Thanks for sharing ,I'm still looking for more complete scenario of using Redis.
GeneralRe: Good Intro Pin
Hariharan Arunachalam22-Aug-13 3:29
Hariharan Arunachalam22-Aug-13 3:29 
GeneralNice Into Pin
cmschick10-May-11 10:16
cmschick10-May-11 10:16 
GeneralRe: Nice Into Pin
Hariharan Arunachalam10-May-11 17:52
Hariharan Arunachalam10-May-11 17:52 
GeneralRe: Nice Into Pin
cmschick11-May-11 1:17
cmschick11-May-11 1:17 
GeneralRe: Nice Into Pin
Hariharan Arunachalam11-May-11 16:20
Hariharan Arunachalam11-May-11 16:20 
GeneralCool Pin
Matt McGuire3-May-11 5:07
professionalMatt McGuire3-May-11 5:07 
GeneralRe: Cool Pin
Hariharan Arunachalam3-May-11 6:33
Hariharan Arunachalam3-May-11 6:33 

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.