RSS

Using Redis with C# (II): Coding

14 Jul

Now that you have installed Redis on your system, let’s jump to the actual coding using C#. If you wish to get the detailed procedures to install Redis, click here.

  • Create a winform project in Visual Studio
  • Install the nuget package by running the following command in the Package Manager Console

PM> Install-Package ServiceStack.Redis

Add a button and a button click event handler:

              private void button1_Click(object sender, EventArgs e)
        {
            string elementKey = “TableId”;
            using (var redisClient = new RedisClient(host, port, password))
            {
                if (redisClient.Get<string>(elementKey) == null)
                {
                    redisClient.Set(elementKey, “12345678”);
                }
                else
                    redisClient.Set(elementKey, “87654321”);
                // get value from the cache by key
                var message = “Item value is: “ + redisClient.Get<string>(elementKey);
                MessageBox.Show(message);
            }
        }

where the host, port and password are as follows:

string host = “localhost”;
int port = 6379;
string password = “xxxxx”;

Run the winform application and click the button:

image

Yeah! You have been able to program Redis in C#!

Happy coding!

 
Leave a comment

Posted by on July 14, 2015 in Dotnet/C#, Programming

 

Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: