StackExchange.Redis simple C# Example

41,942

Solution 1

You can find C# examples in the readme file.

using StackExchange.Redis;
...

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
// ^^^ store and re-use this!!!

IDatabase db = redis.GetDatabase();
string value = "abcdefg";
db.StringSet("mykey", value);
...
string value = db.StringGet("mykey");
Console.WriteLine(value); // writes: "abcdefg"

Solution 2

See the following code from their github sample:

 using (var muxer = ConnectionMultiplexer.Connect("localhost,resolvedns=1"))
        {
            muxer.PreserveAsyncOrder = preserveOrder;
            RedisKey key = "MBOA";
            var conn = muxer.GetDatabase();
            muxer.Wait(conn.PingAsync());

            Action<Task> nonTrivial = delegate
            {
                Thread.SpinWait(5);
            };
            var watch = Stopwatch.StartNew();
            for (int i = 0; i <= AsyncOpsQty; i++)
            {
                var t = conn.StringSetAsync(key, i);
                if (withContinuation) t.ContinueWith(nonTrivial);
            }
            int val = (int)muxer.Wait(conn.StringGetAsync(key));
            watch.Stop();

            Console.WriteLine("After {0}: {1}", AsyncOpsQty, val);
            Console.WriteLine("({3}, {4})\r\n{2}: Time for {0} ops: {1}ms; ops/s: {5}", AsyncOpsQty, watch.ElapsedMilliseconds, Me(),
                withContinuation ? "with continuation" : "no continuation", preserveOrder ? "preserve order" : "any order",
                AsyncOpsQty / watch.Elapsed.TotalSeconds);
        }

Solution 3

  • //-- Install-Package StackExchange.Redis -Version 1.2.6
  • In this simple example: set new string key/value to redis with expiry time, get redis string value by key from redis:
  • Code:
    public string GetRedisValue()
    {
        var cachedKey = "key";
        string value;
    
        using (var redis = ConnectionMultiplexer.Connect("localhost:6379"))
        {
            IDatabase db = redis.GetDatabase();
    
            if (!db.KeyExists(cachedKey))
            {
                value = DateTime.Now.ToString();
                //set new value
                db.StringSet(cachedKey, value, TimeSpan.FromSeconds(18));
            }
            else
            {
                //get cached value
                value = db.StringGet(cachedKey);
            }
        }
    
        return value;
    }
    
Share:
41,942
hellowahab
Author by

hellowahab

You can find me here at https://www.linkedin.com/in/hellowahab

Updated on July 09, 2022

Comments

  • hellowahab
    hellowahab almost 2 years

    I am looking for a very simple starter C# application for using StackExchange.Redis I have search over the web and found StackExchange.Redis

    But this doesn't seems like a quick startup example.

    I have setup redis on windows using StackExchange.Redis exe

    Can anyone help me locate a simple C# application connecting with redis server and setting and getting some keys.