65.9K
CodeProject 正在变化。 阅读更多。
Home

Redis VB.NET 第一部分:在 .NET 中使用 Redis (NoSQL) 简介

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.43/5 (5投票s)

2011 年 4 月 29 日

CPOL

2分钟阅读

viewsIcon

54535

downloadIcon

11260

创建一个可用的 Redis 组件, 以便使用 Redis 存储系统

引言

最近刚进入利润丰厚但又极其消耗精力的软件设计领域,我最近的一次搜索之旅(通常从 Google 开始,在 CodeProject 结束)把我置身于雷区之中。 一个极具诱惑力的雷区,包含了如此多的功能,让我开始怀疑 - 人们没有它怎么生存? 我说的是名为 Redis 的 NoSQL 键值存储。 我建议在继续阅读本文之前先浏览一下该网站。 在本文中,我将使您能够创建一个非常简单的 redis 组件(一个 Windows 控制台应用程序),该组件接受来自用户的 string 并将其存储在 redis 中。 我还将介绍另一个应用程序将如何读取此存储的 string

背景

本系列文章的基本思想是介绍将 redis 用作数据库的概念,用于在松散耦合的组件集合之间实际移动数据。 由于松散耦合的组件最需要高内聚,因此我希望引导您使用 redis 来实现此目的。 我加入 redis 潮流的原因是数据可以更容易、更快地在高度自由流动的环境中移动。 但请注意,与传统数据库(例如 Oracle、MySQL、SQL Server 等)不同,redis 必然是非持久性的:如果服务器关闭,数据将丢失。 但不用担心,因为 redis 支持其数据的持久性,条件是它以一定的时间间隔或在手动请求时发生。 现在考虑到这些,让我们继续。

Using the Code

要使用此代码,请在 VB.NET 中创建一个 Windows 控制台应用程序项目。(使用 .NET 3.5)。 我不建议使用 .NET 4,因为 redis 库是用 3.5 编写的,并且在使用 .NET 4 项目中的库时存在某些未解决的问题。先决条件是

  1. VB.NET 的基本编程知识 - 我不会解释您应该如何获取用户输入。
  2. 项目的 Redis 库文件 (ServiceStack)
  3. Redis 服务器可执行文件 (Google Code)
  4. 一往无前的勇气

创建项目。 将引用添加到 ServiceStack 库中的所有 DLL 中。 接受用户输入到变量中。

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

string 传递给 RedisStore 类的 SetKey 函数以存储它,并调用 GetKey 函数来检索它。 您还会注意到,redis 允许您使用 GetKey(Of T) 函数在其存储中存储整个对象

好吧,这就是本教程第一部分的内容。 试一试,让我知道我是否应该继续将 Redis 用作 PubSub 媒介,这恰好是 redis 的预期用途之一,除了仅仅作为键值存储之外。

历史

  • 2011 年 4 月 28 日:初始版本
© . All rights reserved.