Quantcast
Channel: Dotnet/C# – Xinyustudio
Viewing all articles
Browse latest Browse all 204

Using Redis with C# (II): Coding

$
0
0

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!


Filed under: Dotnet/C#, Programming Tagged: .net, C#, example, how to, Redis, ServiceStack, tutorial

Viewing all articles
Browse latest Browse all 204

Trending Articles