WeihanLi.Redis

Intro

StackExchange.Redis extension is a simpler generic operation and provides some extensions applicable to business scenarios

Five data types based on Redis have extended some applications:

  1. String

    • Cache
    • Counter
    • Firewall
    • RedLock
  2. Hash

    • Hash
    • Dictonary
  3. List

    • List
  4. Set

    • Set
  5. SortedSet

    • SortedSet
    • Rank

GetStarted

Install

Install from Nuget

Configuration configuration

RedisManager.AddRedisConfig(config =>
    {
        config.CachePrefix = "WeihanLi.Redis.UnitTest";
        config.ChannelPrefix = "WeihanLi.Redis.UnitTest";
    });

. net core application, which can also be configured in this way

serviceCollection.AddRedisConfig(config =>
    {
        config.CachePrefix = "WeihanLi.Redis.UnitTest";
        config.ChannelPrefix = "WeihanLi.Redis.UnitTest";
        config.EnableCompress = false;// disable compress
    });

Basic usage

  1. Cache cache

    The basic operation of cache is mainly based on RedisManager.CacheClient

    The basic operations of cache are defined in the Icache client, as follows:

    var key = "test111";
    var value = "Hello WeihanLi.Redis";
    Assert.True(RedisManager.CacheClient.Set(key, value));
    Assert.True(RedisManager.CacheClient.Exists(key));
    Assert.Equal(value, RedisManager.CacheClient.Get(key));
    Assert.True(RedisManager.CacheClient.Remove(key));
    Assert.False(RedisManager.CacheClient.Exists(key));
    RedisManager.CacheClient.GetOrSet(key, () => value, TimeSpan.FromSeconds(10));
  2. Counter counter

    var counterName = "counterTest";
    var counterClient = RedisManager.GetCounterClient(counterName, TimeSpan.FromSeconds(60));
    Assert.Equal(0, counterClient.Base);
    Assert.Equal(0, counterClient.Count());
    counterClient.Increase();
    Assert.Equal(1, counterClient.Count());
    counterClient.Increase(5);
    Assert.Equal(6, counterClient.Count());
    counterClient.Decrease(3);
    Assert.Equal(3, counterClient.Count());
    Assert.True(counterClient.Reset());
    Assert.Equal(0, counterClient.Count());
  3. Firewall

    var firewallName = "firewallTest";
    var firewallClient = RedisManager.GetFirewallClient(firewallName, TimeSpan.FromSeconds(3));
    Assert.True(firewallClient.Hit());
    Assert.False(firewallClient.Hit());
    await Task.Delay(TimeSpan.FromSeconds(3));
    Assert.True(firewallClient.Hit());
  4. RedLock Redis distributed lock

    using (var client = RedisManager.GetRedLockClient("redLockTest"))
    {
        Assert.True(client.TryLock(TimeSpan.FromSeconds(10)));
        using (var client1 = RedisManager.GetRedLockClient("redLockTest"))
        {
            Assert.False(client.TryLock(TimeSpan.FromSeconds(10)));
            Assert.False(client1.Release());
        }
        Assert.True(client.Release());
    }
    
    var key = Guid.NewGuid().ToString("N");
    using (var client = RedisManager.GetRedLockClient(key))
    {
        Assert.True(client.TryLock(TimeSpan.FromSeconds(20)));
    }
    
    using (var client = RedisManager.GetRedLockClient(key))
    {
        Assert.True(client.TryLock(TimeSpan.FromMinutes(3)));
        Assert.True(client.Release());
    }
  5. Rank

    var rankClient = RedisManager.GetRankClient<string>("testRank");
    Assert.Equal(0, rankClient.Length());
    rankClient.Add("xiaoming", 100);
    rankClient.Add("xiaohong", 95);
    rankClient.Add("xiaowang", 96);
    Assert.Equal(3, rankClient.Length());
    Assert.Equal(100, rankClient.Score("xiaoming"));
    var rank = rankClient.RangeByScore();
    Assert.Equal("xiaohong", rank[0]);
    rank = rankClient.RangeByScore(order: Order.Descending);
    Assert.Equal("xiaoming", rank[0]);
    var common = RedisManager.GetCommonRedisClient(RedisDataType.Rank);
    Assert.True(common.KeyDelete("testRank"));
  6. More usage waiting for you find...

Conatct

Contact me: weihanli@outlook.com