Click here to Skip to main content
15,885,758 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.6K   11.2K   15  
Create a usable redis component to enable usage of the redis storage system
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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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