Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
29 Apr 2011 1  
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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here