• 各种redis的介绍:ServiceStack.Redis,StackExchange.Redis,CSRedis


    1.ServiceStack.Redis 是商业版,免费版有限制;ServiceStack.Redis每小时6000次限制,ServiceStack 4.0 开始已经成为商业产品,不再完全免费,好在是开源的.

    2.StackExchange.Redis 是免费版,但是内核在 .NETCore 运行有问题经常 Timeout,暂无法解决;

    3.CSRedis于2016年开始支持.NETCore一直迭代至今,实现了低门槛、高性能,和分区高级玩法的.NETCore redis-cli SDK;

    1. 通过包管理器安装CSRedis
    2. 实例化csredis,并继承icasheservice
    3. DBContent 里实例化csredisClient
    4. TestRedisCache


    public class TestRedisCache: ICacheService
    {
    /// <summary>
    /// redis
    /// </summary>
    public CSRedisClient rds;

    //redis cach second,default 120,read appsetting.json
    int _expirySeconds = 120;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="connectionStrings"></param>
    public TestRedisCache(string[] connectionStrings, int expirySeconds)
    {
    this.rds = new CSRedis.CSRedisClient((p => { return null; }), connectionStrings);
    _expirySeconds = expirySeconds;
    }

    public void Add<V>(string key, V value)
    {
    this.rds.Set(key, value);

    }

    public void Add<V>(string key, V value, int cacheDurationInSeconds)
    {
    if (cacheDurationInSeconds >= 2147483647)
    {
    cacheDurationInSeconds = _expirySeconds;
    }
    this.rds.Set(key, value, cacheDurationInSeconds);
    }

    public bool ContainsKey<V>(string key)
    {
    return this.rds.Exists(key);
    }

    public V Get<V>(string key)
    {
    return this.rds.Get<V>(key);
    }

    public IEnumerable<string> GetAllKey<V>()
    {
    return this.rds.Keys("*");
    }

    public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
    {
    if (this.ContainsKey<V>(cacheKey))
    {
    return this.Get<V>(cacheKey);
    }
    else
    {
    var result = create();
    this.Add(cacheKey, result, cacheDurationInSeconds);
    return result;
    }
    }

    public void Remove<V>(string key)
    {
    this.rds.Del(key);
    }
    public void DisposeRedis()
    {
    this.rds.Dispose();
    }
    }
    }


    5. DBContext

    public DBContext()
    {
    //get mysql connnection str
    var salveCount = configuration.GetSection("ConnectionStrings:SlaveCount").Value.ObjToInt();
    List<SlaveConnectionConfig> slaves = new List<SlaveConnectionConfig>();
    for (int i = 0; i < salveCount; i++)
    {
    slaves.Add(new SlaveConnectionConfig
    {
    ConnectionString = configuration[$"ConnectionStrings:Slave:{i}:ConnectionString"], HitRate = configuration[$"ConnectionStrings:Slave:{i}:HitRate"].ObjToInt()
    });
    }

    //redis db default expiration Second
    if (!string.IsNullOrEmpty(configuration["RedisString:DurationSecond"]))
    { CachExpirationSecond = int.Parse(configuration["RedisString:DurationSecond"]); }

    string redisconn = configuration["RedisString:ConnectionHost"];

    if (RedisDB == null)
    {
    RedisDB = new ExamRedisCache(new string[] { redisconn }, CachExpirationSecond);
    }


    //mysql db object
    if (DB == null)
    {
    DB = new SqlSugarClient(new ConnectionConfig()
    {
    ConnectionString = configuration["ConnectionStrings:Master"],
    DbType = DbType.MySql,
    IsAutoCloseConnection = true,
    InitKeyType = InitKeyType.Attribute,
    SlaveConnectionConfigs = slaves,
    ConfigureExternalServices = new ConfigureExternalServices()
    {
    DataInfoCacheService = RedisDB
    }
    });
    }

    //调式代码 用来打印SQL
    DB.Aop.OnLogExecuting = (sql, pars) =>
    {
    Console.WriteLine(sql + " " +
    DB.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
    Console.WriteLine();
    };
    }

  • 相关阅读:
    解决js计算0.1+0.2 !==0.3
    webpack 4 移除 CommonsChunkPlugin,取而代之的是两个新的配置项(optimization.splitChunks 和 optimization.runtimeChunk
    jq轮播图插件
    如何在 GitHub 的项目中创建一个分支呢?
    VUE图片剪辑插件 React图片剪辑插件
    前端图片压缩上传
    vue实现rsa加密,数字签名,md5加密等
    vue-class-component使用Mixins
    微信小程序--获取用户地理位置名称(无须用户授权)的方法
    [学习笔记]二进制分组
  • 原文地址:https://www.cnblogs.com/csj007523/p/13751390.html
Copyright © 2020-2023  润新知