Giter VIP home page Giter VIP logo

weihanli.redis's Introduction

WeihanLi.RedisWeihanLi.Redis

English

Build

Build Status

Build Status

Intro

RedisExtensions for StackExchange.Redis, much more easier for generic opeartions,and supply some extensions for your business logics.

StackExchange.Redis 扩展,更简单的泛型操作,并提供一些的适用于业务场景中的扩展

基于 Redis 的五种主要的数据类型做了一些扩展:

  1. String

    • Cache
    • Counter
    • Firewall
    • RedLock
    • RateLimiter
  2. Hash

    • Hash
    • Dictonary
    • HashCounter
  3. List

    • List
    • Queue
    • Stack
  4. Set

    • Set
  5. SortedSet

    • SortedSet
    • Rank

GetStarted

Installation 安装

Install from Nuget

通过 Nuget 安装 WeihanLi.Redis

Configuration 配置

对于 V1.0.6 及以下版本支持 .netframework4.5 配置参考:

  1. 日志配置,日志基于 log4net 的配置,可以参考单元测试中的 log4net.config 的配置,将 log4net.config 放在项目根目录下【推荐】 如果不在项目根目录下或者文件名发生修改则需要自己手动调用 LogHelper.LogInit("log4net.config");

  2. Redis 配置

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

.net core 应用,还可以这样配置

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

对于 1.1* 及以上版本不再支持 dotnet framework,配置方式如下:

// IServiceCollection serviceCollection = new ServiceCollection();

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

Basic usage 基本用法

  1. Cache 缓存

    缓存的基本操作主要是基于 RedisManager.CacheClient

    缓存的基本操作定义在 ICacheClient 中,基本操作如下:

    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 计数器

    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分布式锁

    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. 依赖注入

    // 在 asp.net core 中可以直接从构造器注入,这里使用的一个 ServiceLocator 模式获取注入的服务
    var cacheClient = DependencyResolver.Current.ResolveService<ICacheClient>();
    Assert.NotNull(cacheClient);
    var key = Guid.NewGuid().ToString("N");
    Assert.Equal("abcaaa", cacheClient.GetOrSet(key, () => "abcaaa", TimeSpan.FromMinutes(10)));
    cacheClient.Remove(key);
    var hashClient = DependencyResolver.Current.ResolveService<IHashClient>();
    Assert.NotNull(hashClient);
    var pubsubClient = DependencyResolver.Current.ResolveService<IPubSubClient>();
    Assert.NotNull(pubsubClient);
  7. 更多用法等你来发现...

Contact

Contact me if you need: [email protected]

weihanli.redis's People

Contributors

weihanli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

weihanli.redis's Issues

Update logging

现在使用需要手动 LogHelper.LogInit() 方法进行日志的初始化,造成使用不便

InMemoryCache

Sometimes we want excellent performance experience, we may consider for in-memory cache on the base of redis cache.

redesign api for .netstandard

The api that the version 1.1.0 and before is more for .net framework, from version 1.1.0, this package no longer support .net famework. and the api is not so suitable for .net standard, so the api needs to be redesigned.

RedisMQ

implement Redis based message queue

缓存击穿,缓存穿透,缓存雪崩情况处理

  • 缓存击穿

一般的缓存系统,都是按照key去缓存查询,如果不存在对应的value,就应该去后端系统查找(比如DB)。如果key对应的value是一定不存在的,并且对该key并发请求量很大,就会对后端系统造成很大的压力。在高并发下,多线程同时查询同一个资源,如果缓存中没有这个资源,那么这些线程都会去数据库查找,对数据库造成极大压力,缓存失去存在的意义

  • 缓存雪崩

当缓存服务器重启或者大量缓存集中在某一个时间段失效,这样在失效的时候,也会给后端系统(比如DB)带来很大压力。

  • 缓存穿透

缓存穿透是指用户查询数据,在数据库没有,自然在缓存中也不会有。这样就导致用户查询的时候,在缓存中找不到,每次都要去数据库中查询。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.