Skip to content

Commit

Permalink
移除CsRedis,新增FreeRedisHelper工具类,优化AddFreeRedis;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Dec 5, 2020
1 parent 3ddbbd9 commit 83ab9c2
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 31 deletions.
34 changes: 5 additions & 29 deletions ZqUtils.Core/Extensions/Extensions.IServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using Newtonsoft.Json;
using RabbitMQ.Client;
using Scrutor;
using System;
using System.Linq;
using System.Reflection;
using ZqUtils.Core.Attributes;
using ZqUtils.Core.Helpers;
using CsRedisClient = CSRedis.CSRedisClient;
/****************************
* [Author] 张强
* [Date] 2018-05-17
Expand Down Expand Up @@ -421,34 +421,6 @@ public static IServiceCollection AddDependsOnFromAssembly(
}
#endregion

#region AddCsRedis
/// <summary>
/// 初始化CSRedisCore,之后程序中可以直接使用RedisHelper的静态方法
/// </summary>
/// <param name="this"></param>
/// <param name="configuration">json配置</param>
/// <returns></returns>
public static IServiceCollection AddCsRedis(this IServiceCollection @this, IConfiguration configuration)
{
var connectionStrings = configuration.GetSection("Redis:ConnectionStrings").Get<string[]>();

CsRedisClient client;
if (connectionStrings.Length == 1)
//普通模式
client = new CsRedisClient(connectionStrings[0]);
else
//集群模式
//实现思路:根据key.GetHashCode() % 节点总数量,确定连向的节点
//也可以自定义规则(第一个参数设置)
client = new CsRedisClient(null, connectionStrings);

//初始化 RedisHelper
RedisHelper.Initialization(client);

return @this;
}
#endregion

#region AddFreeRedis
/// <summary>
/// 注入FreeRedis
Expand All @@ -470,6 +442,10 @@ public static IServiceCollection AddFreeRedis(this IServiceCollection @this, ICo
//集群模式
client = new RedisClient(connectionStrings.Select(v => ConnectionStringBuilder.Parse(v)).ToArray());

//配置序列化和反序列化
client.Serialize = obj => JsonConvert.SerializeObject(obj);
client.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);

return client;
});

Expand Down
99 changes: 99 additions & 0 deletions ZqUtils.Core/Helpers/FreeRedisHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#region License
/***
* Copyright © 2018-2020, 张强 ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion

using FreeRedis;
using Newtonsoft.Json;
using System;
using System.Linq;
/****************************
* [Author] 张强
* [Date] 2020-12-05
* [Describe] FreeRedis工具类
* **************************/
namespace ZqUtils.Core.Helpers
{
/// <summary>
/// FreeRedis工具类
/// </summary>
public class FreeRedisHelper : RedisClient
{
/// <summary>
/// 静态实例
/// </summary>
public static FreeRedisHelper Default;

/// <summary>
/// 静态构造函数
/// </summary>
static FreeRedisHelper()
{
var connectionStrings = ConfigHelper.Get<string[]>("Redis:ConnectionStrings");

if (connectionStrings.Length == 1)
//普通模式
Default = new FreeRedisHelper(connectionStrings[0]);
else
//集群模式
Default = new FreeRedisHelper(connectionStrings.Select(v => ConnectionStringBuilder.Parse(v)).ToArray());

//配置序列化和反序列化
Default.Serialize = obj => JsonConvert.SerializeObject(obj);
Default.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
}

/// <summary>
/// 集群模式
/// </summary>
/// <param name="clusterConnectionStrings"></param>
public FreeRedisHelper(ConnectionStringBuilder[] clusterConnectionStrings)
: base(clusterConnectionStrings)
{
}

/// <summary>
/// 主从模式
/// </summary>
/// <param name="connectionString"></param>
/// <param name="slaveConnectionStrings"></param>
public FreeRedisHelper(ConnectionStringBuilder connectionString, params ConnectionStringBuilder[] slaveConnectionStrings)
: base(connectionString, slaveConnectionStrings)
{
}

/// <summary>
/// Norman RedisClient
/// </summary>
/// <param name="connectionStrings"></param>
/// <param name="redirectRule"></param>
public FreeRedisHelper(ConnectionStringBuilder[] connectionStrings, Func<string, string> redirectRule)
: base(connectionStrings, redirectRule)
{
}

/// <summary>
/// 哨兵高可用模式
/// </summary>
/// <param name="sentinelConnectionString"></param>
/// <param name="sentinels"></param>
/// <param name="rw_splitting"></param>
public FreeRedisHelper(ConnectionStringBuilder sentinelConnectionString, string[] sentinels, bool rw_splitting)
: base(sentinelConnectionString, sentinels, rw_splitting)
{
}
}
}
3 changes: 1 addition & 2 deletions ZqUtils.Core/ZqUtils.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.8" />
<PackageReference Include="Confluent.Kafka" Version="1.5.2" />
<PackageReference Include="CSRedisCore" Version="3.6.5" />
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="DnsClient" Version="1.3.2" />
<PackageReference Include="EPPlus" Version="4.5.3.3" />
<PackageReference Include="FastExpressionCompiler" Version="2.0.0" />
<PackageReference Include="FreeRedis" Version="0.1.7" />
<PackageReference Include="FreeRedis" Version="0.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
Expand Down

0 comments on commit 83ab9c2

Please sign in to comment.