zoukankan      html  css  js  c++  java
  • 各种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();
    };
    }

  • 相关阅读:
    IIS的各种身份验证详细测试
    HTTP Error 401.3 Unauthorized Error While creating IIS 7.0 web site on Windows 7
    C/S and B/S
    WCF ContractFilter mismatch at the EndpointDispatcher exception
    Configure WCF
    Inheritance VS Composition
    Unhandled Error in Silverlight Application, code 2103 when changing the namespace
    Java RMI VS TCP Socket
    Principles Of Object Oriented Design
    Socket处理发送和接收数据包,一个小实例:
  • 原文地址:https://www.cnblogs.com/csj007523/p/13751390.html
Copyright © 2011-2022 走看看