Skip to content

Commit

Permalink
TrimNull
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 25, 2023
1 parent b68c601 commit 3c934b0
Show file tree
Hide file tree
Showing 28 changed files with 656 additions and 612 deletions.
39 changes: 31 additions & 8 deletions NewLife.Redis.Extensions/NewLife.Redis.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<AssemblyTitle>新生命Redis扩展</AssemblyTitle>
<Description>Redis扩展库,便于注入Redis,支持分布式缓存IDistributedCache和数据保护IDataProtection</Description>
<Company>新生命开发团队</Company>
Expand Down Expand Up @@ -44,6 +44,36 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net5.0'">
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net7.0'">
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net8.0'">
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Content Include="..\Doc\leaf.png" Link="leaf.png" PackagePath="\" />
Expand All @@ -55,13 +85,6 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NewLife.Redis\NewLife.Redis.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions NewLife.Redis.Extensions/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public RedisCache(IOptions<RedisOptions> optionsAccessor, IServiceProvider servi
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Byte[] Get(String key) => _redis.Get<Byte[]>(key);
public Byte[]? Get(String key) => _redis.Get<Byte[]>(key);

/// <summary>
/// 异步获取
/// </summary>
/// <param name="key"></param>
/// <param name="token"></param>
/// <returns></returns>
public Task<Byte[]> GetAsync(String key, CancellationToken token = default) => Task.Run(() => _redis.Get<Byte[]>(key), token);
public Task<Byte[]?> GetAsync(String key, CancellationToken token = default) => Task.Run(() => _redis.Get<Byte[]>(key), token);

/// <summary>
/// 设置
Expand Down
8 changes: 4 additions & 4 deletions NewLife.Redis.Extensions/RedisXmlRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace NewLife.Redis.Extensions;
/// <summary>在Redis中存储Xml</summary>
public class RedisXmlRepository : IXmlRepository
{
private readonly NewLife.Caching.Redis _redis;
private readonly Func<NewLife.Caching.Redis> _redisFactory;
private readonly NewLife.Caching.Redis? _redis;
private readonly Func<NewLife.Caching.Redis>? _redisFactory;

private readonly String _key;

Expand Down Expand Up @@ -42,7 +42,7 @@ public RedisXmlRepository(Func<NewLife.Caching.Redis> redisFactory, String key)
/// <returns></returns>
private IEnumerable<XElement> GetAllElementsCore()
{
var rds = _redis ?? _redisFactory();
var rds = _redis ?? _redisFactory!();
var list = rds.GetList<String>(_key);
foreach (var item in list)
{
Expand All @@ -55,7 +55,7 @@ private IEnumerable<XElement> GetAllElementsCore()
/// <param name="friendlyName"></param>
public void StoreElement(XElement element, String friendlyName)
{
var rds = _redis ?? _redisFactory();
var rds = _redis ?? _redisFactory!();
var list = rds.GetList<String>(_key);
list.Add(element.ToString(SaveOptions.DisableFormatting));
}
Expand Down
12 changes: 6 additions & 6 deletions NewLife.Redis/Clusters/ClusterNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ public class ClusterNode : RedisNode
{
#region 属性
/// <summary>标识</summary>
public String ID { get; set; }
public String ID { get; set; } = null!;

/// <summary>标志。master/slave/myself</summary>
public String Flags { get; set; }
public String Flags { get; set; } = null!;

/// <summary>主机。当前节点对应的主机</summary>
public String Master { get; set; }
public String Master { get; set; } = null!;

/// <summary>链接状态</summary>
public Int32 LinkState { get; set; }

/// <summary>当前节点的从节点集合</summary>
public IList<ClusterNode> Slaves { get; set; }
public IList<ClusterNode>? Slaves { get; set; }

/// <summary>本节点数据槽</summary>
public IList<Slot> Slots { get; private set; } = new List<Slot>();

/// <summary>正在转入</summary>
public IDictionary<Int32, String> Importings { get; private set; }
public IDictionary<Int32, String>? Importings { get; private set; }

/// <summary>正在转出</summary>
public IDictionary<Int32, String> Migratings { get; private set; }
public IDictionary<Int32, String>? Migratings { get; private set; }
#endregion

#region 构造
Expand Down
4 changes: 2 additions & 2 deletions NewLife.Redis/Clusters/IRedisCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public interface IRedisCluster
/// <param name="key">键</param>
/// <param name="write">可写</param>
/// <returns></returns>
IRedisNode SelectNode(String key, Boolean write);
IRedisNode? SelectNode(String key, Boolean write);

/// <summary>根据异常重选节点</summary>
/// <param name="key">键</param>
/// <param name="write">可写</param>
/// <param name="node"></param>
/// <param name="exception"></param>
/// <returns></returns>
IRedisNode ReselectNode(String key, Boolean write, IRedisNode node, Exception exception);
IRedisNode? ReselectNode(String key, Boolean write, IRedisNode node, Exception exception);

/// <summary>重置节点。设置成功状态</summary>
/// <param name="node"></param>
Expand Down
12 changes: 6 additions & 6 deletions NewLife.Redis/Clusters/MasterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ public class MasterInfo
{
#region 属性
/// <summary>名称</summary>
public String Name { get; set; }
public String? Name { get; set; }

/// <summary>状态。ok</summary>
public String Status { get; set; }
public String? Status { get; set; }

/// <summary>地址</summary>
public String IP { get; set; }
public String? IP { get; set; }

/// <summary>端口</summary>
public Int32 Port { get; set; }

/// <summary>节点信息</summary>
public String EndPoint => IP.IsNullOrEmpty() ? null : $"{IP}:{Port}";
public String? EndPoint => IP.IsNullOrEmpty() ? null : $"{IP}:{Port}";

/// <summary>从机数</summary>
public Int32 Slaves { get; set; }
Expand All @@ -29,14 +29,14 @@ public class MasterInfo
#region 构造
/// <summary>已重载。</summary>
/// <returns></returns>
public override String ToString() => !IP.IsNullOrEmpty() ? EndPoint : base.ToString();
public override String? ToString() => !IP.IsNullOrEmpty() ? EndPoint : base.ToString();
#endregion

#region 方法
/// <summary>分析字符串</summary>
/// <param name="str"></param>
/// <returns></returns>
public static MasterInfo Parse(String str)
public static MasterInfo? Parse(String str)
{
if (str.IsNullOrEmpty()) return null;

Expand Down
24 changes: 12 additions & 12 deletions NewLife.Redis/Clusters/RedisCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public class RedisCluster : RedisBase, IRedisCluster, IDisposable
{
#region 属性
/// <summary>集群节点</summary>
public ClusterNode[] Nodes { get; private set; }
public ClusterNode[]? Nodes { get; private set; }

private TimerX _timer;
private TimerX? _timer;
#endregion

#region 构造
/// <summary>实例化</summary>
/// <param name="redis"></param>
public RedisCluster(Redis redis) : base(redis, null) { }
public RedisCluster(Redis redis) : base(redis, "") { }

/// <summary>销毁</summary>
public void Dispose() => _timer.TryDispose();
Expand All @@ -44,13 +44,13 @@ public void GetNodes()
ParseNodes(rs);
}

private String _lastNodes;
private String? _lastNodes;
/// <summary>分析节点</summary>
/// <param name="nodes"></param>
public void ParseNodes(String nodes)
{
var showLog = Nodes == null;
if (showLog) WriteLog("分析[{0}]集群节点:", Redis?.Name);
if (showLog) WriteLog("分析[{0}]集群节点:", Redis.Name);

var list = new List<ClusterNode>();
foreach (var item in nodes.Split("\r", "\n"))
Expand All @@ -74,7 +74,7 @@ public void ParseNodes(String nodes)
var str = list.Join("\n", n => n + " " + n?.Slots.Join(","));
if (str != _lastNodes)
{
if (!showLog) WriteLog("分析[{0}]集群节点:", Redis?.Name);
if (!showLog) WriteLog("分析[{0}]集群节点:", Redis.Name);
showLog = true;
_lastNodes = str;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ private List<ClusterNode> SortNodes(List<ClusterNode> list)
/// <param name="key">键</param>
/// <param name="write">可写</param>
/// <returns></returns>
public virtual IRedisNode SelectNode(String key, Boolean write)
public virtual IRedisNode? SelectNode(String key, Boolean write)
{
if (key.IsNullOrEmpty()) return null;

Expand Down Expand Up @@ -167,7 +167,7 @@ public virtual IRedisNode SelectNode(String key, Boolean write)
/// <param name="node"></param>
/// <param name="exception"></param>
/// <returns></returns>
public IRedisNode ReselectNode(String key, Boolean write, IRedisNode node, Exception exception)
public IRedisNode? ReselectNode(String key, Boolean write, IRedisNode node, Exception exception)
{
using var span = Redis.Tracer?.NewSpan("redis:ReselectNode", new { key, (node as RedisNode)?.EndPoint });

Expand Down Expand Up @@ -208,7 +208,7 @@ public IRedisNode ReselectNode(String key, Boolean write, IRedisNode node, Excep
/// <param name="endpoint"></param>
/// <param name="key"></param>
/// <returns></returns>
public virtual ClusterNode Map(String endpoint, String key)
public virtual ClusterNode? Map(String endpoint, String key)
{
var node = Nodes.FirstOrDefault(e => e.EndPoint == endpoint);
if (node == null) return null;
Expand Down Expand Up @@ -240,7 +240,7 @@ public virtual void ResetNode(IRedisNode node)
/// <returns></returns>
public virtual void AddSlots(ClusterNode node, params Int32[] slots)
{
var pool = (Redis as FullRedis).GetPool(node);
var pool = (Redis as FullRedis)!.GetPool(node);
var client = pool.Get();
try
{
Expand All @@ -265,7 +265,7 @@ public virtual void AddSlots(ClusterNode node, params Int32[] slots)
/// <returns></returns>
public virtual void DeleteSlots(ClusterNode node, params Int32[] slots)
{
var pool = (Redis as FullRedis).GetPool(node);
var pool = (Redis as FullRedis)!.GetPool(node);
var client = pool.Get();
try
{
Expand Down Expand Up @@ -358,6 +358,6 @@ public virtual Boolean Rebalance()
/// <summary>写日志</summary>
/// <param name="format"></param>
/// <param name="args"></param>
public void WriteLog(String format, params Object[] args) => Log?.Info(format, args);
public void WriteLog(String format, params Object?[] args) => Log?.Info(format, args);
#endregion
}
Loading

0 comments on commit 3c934b0

Please sign in to comment.