-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
307 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/EasyCaching.Bus.FreeRedis/Configurations/EasyCachingFreeRedisClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace EasyCaching.Bus.FreeRedis.Configurations | ||
{ | ||
using global::FreeRedis; | ||
using System; | ||
|
||
public class EasyCachingFreeRedisClient : RedisClient | ||
{ | ||
private readonly string _name; | ||
|
||
public string Name { get { return this._name; } } | ||
|
||
public EasyCachingFreeRedisClient(string name, ConnectionStringBuilder connectionString, params ConnectionStringBuilder[] slaveConnectionStrings) | ||
: base(connectionString, slaveConnectionStrings) | ||
{ | ||
this._name = name; | ||
} | ||
|
||
public EasyCachingFreeRedisClient(string name, ConnectionStringBuilder[] clusterConnectionStrings) | ||
: base(clusterConnectionStrings) | ||
{ | ||
this._name = name; | ||
} | ||
|
||
public EasyCachingFreeRedisClient(string name, ConnectionStringBuilder[] connectionStrings, Func<string, string> redirectRule) | ||
: base(connectionStrings, redirectRule) | ||
{ | ||
this._name = name; | ||
} | ||
|
||
public EasyCachingFreeRedisClient(string name, ConnectionStringBuilder sentinelConnectionString, string[] sentinels, bool rw_splitting) | ||
: base(sentinelConnectionString, sentinels, rw_splitting) | ||
{ | ||
this._name = name; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/EasyCaching.Bus.FreeRedis/Configurations/EasyCachingOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace EasyCaching.Bus.FreeRedis.Configurations | ||
{ | ||
using EasyCaching.Core; | ||
using EasyCaching.Core.Configurations; | ||
using Microsoft.Extensions.Configuration; | ||
using System; | ||
|
||
public static class EasyCachingOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Withs the FreeRedis bus (specify the config via hard code). | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="configure">Configure bus settings.</param> | ||
public static EasyCachingOptions WithFreeRedisBus( | ||
this EasyCachingOptions options | ||
, Action<FreeRedisBusOptions> configure | ||
) | ||
{ | ||
ArgumentCheck.NotNull(configure, nameof(configure)); | ||
|
||
options.RegisterExtension(new FreeRedisOptionsExtension(configure)); | ||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Withs the FreeRedis bus (read config from configuration file). | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="configuration">The configuration.</param> | ||
/// <param name="sectionName">The section name in the configuration file.</param> | ||
public static EasyCachingOptions WithFreeRedisBus( | ||
this EasyCachingOptions options | ||
, IConfiguration configuration | ||
, string sectionName = EasyCachingConstValue.RedisBusSection | ||
) | ||
{ | ||
var dbConfig = configuration.GetSection(sectionName); | ||
var redisOptions = new FreeRedisBusOptions(); | ||
dbConfig.Bind(redisOptions); | ||
|
||
void configure(FreeRedisBusOptions x) | ||
{ | ||
x.ConnectionStrings = redisOptions.ConnectionStrings; | ||
} | ||
|
||
options.RegisterExtension(new FreeRedisOptionsExtension(configure)); | ||
return options; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/EasyCaching.Bus.FreeRedis/Configurations/FreeRedisBusOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace EasyCaching.Bus.FreeRedis.Configurations | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using global::FreeRedis; | ||
|
||
public class FreeRedisBusOptions | ||
{ | ||
public List<ConnectionStringBuilder> ConnectionStrings { get; set; } | ||
|
||
public List<ConnectionStringBuilder> SlaveConnectionStrings { get; set; } | ||
|
||
/// <summary> | ||
/// Redirect rule | ||
/// </summary> | ||
public Func<string, string> RedirectRule { get; set; } = null; | ||
|
||
/// <summary> | ||
/// Sentinel master connection string. | ||
/// </summary> | ||
public ConnectionStringBuilder SentinelConnectionString { get; set; } | ||
|
||
/// <summary> | ||
/// Redis sentinels. | ||
/// </summary> | ||
public List<string> Sentinels { get; set; } | ||
|
||
/// <summary> | ||
/// This variable indicates whether to use the read-write separation mode. | ||
/// </summary> | ||
public bool RwSplitting { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the Redis database KeyPrefix will use. | ||
/// </summary> | ||
public string KeyPrefix { get; set; } | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/EasyCaching.Bus.FreeRedis/Configurations/FreeRedisOptionsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
namespace EasyCaching.Bus.FreeRedis.Configurations | ||
{ | ||
using EasyCaching.Core; | ||
using EasyCaching.Core.Bus; | ||
using EasyCaching.Core.Configurations; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Linq; | ||
|
||
public class FreeRedisOptionsExtension : IEasyCachingOptionsExtension | ||
{ | ||
/// <summary> | ||
/// The name. | ||
/// </summary> | ||
private const string _name = "easycachingbus"; | ||
|
||
/// <summary> | ||
/// The configure. | ||
/// </summary> | ||
private readonly Action<FreeRedisBusOptions> _configure; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:EasyCaching.Bus.FreeRedis.Configurations.FreeRedisBusOptions"/> class. | ||
/// </summary> | ||
/// <param name="configure">Configure.</param> | ||
public FreeRedisOptionsExtension(Action<FreeRedisBusOptions> configure) | ||
{ | ||
this._configure = configure; | ||
} | ||
|
||
|
||
public void AddServices(IServiceCollection services) | ||
{ | ||
services.AddOptions(); | ||
services.Configure(_name, _configure); | ||
|
||
services.TryAddSingleton<IEasyCachingProviderFactory, DefaultEasyCachingProviderFactory>(); | ||
|
||
services.AddSingleton<EasyCachingFreeRedisClient>(x => | ||
{ | ||
var optionsMon = x.GetRequiredService<IOptionsMonitor<FreeRedisBusOptions>>(); | ||
var options = optionsMon.Get(_name); | ||
var conns = options.ConnectionStrings; | ||
var slaveConns = options.SlaveConnectionStrings; | ||
var redirectRule = options.RedirectRule; | ||
var sentinelsConn = options.SentinelConnectionString; | ||
var sentinels = options.Sentinels; | ||
var rwSplitting = options.RwSplitting; | ||
if (sentinelsConn != null) | ||
{ | ||
// Redis Sentinel | ||
return new EasyCachingFreeRedisClient(_name, sentinelsConn, sentinels.ToArray(), rwSplitting); | ||
} | ||
if (conns.Count == 1) | ||
{ | ||
// Pooling RedisClient | ||
var slave = slaveConns != null && slaveConns.Any() ? slaveConns.ToArray() : null; | ||
return new EasyCachingFreeRedisClient(_name, conns[0], slave); | ||
} | ||
else | ||
{ | ||
// Norman RedisClient Or Redis Cluster | ||
if (redirectRule != null) return new EasyCachingFreeRedisClient(_name, conns.ToArray(), redirectRule); | ||
else return new EasyCachingFreeRedisClient(_name, conns.ToArray()); | ||
} | ||
}); | ||
|
||
services.AddSingleton<IEasyCachingBus, DefaultFreeRedisBus>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace EasyCaching.Bus.FreeRedis | ||
{ | ||
using EasyCaching.Core; | ||
using EasyCaching.Core.Bus; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
internal class DefaultFreeRedisBus : EasyCachingAbstractBus | ||
{ | ||
public override void BasePublish(string topic, EasyCachingMessage message) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override Task BasePublishAsync(string topic, EasyCachingMessage message, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void BaseSubscribe(string topic, Action<EasyCachingMessage> action) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override Task BaseSubscribeAsync(string topic, Action<EasyCachingMessage> action, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/EasyCaching.Bus.FreeRedis/EasyCaching.Bus.FreeRedis.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FreeRedis" Version="1.1.1" /> | ||
<PackageReference Include="EasyCaching.Core" Version="1.9.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\media\nuget-icon.png" Pack="true" Visible="false" PackagePath="" /> | ||
</ItemGroup> | ||
</Project> |
30 changes: 30 additions & 0 deletions
30
tests/EasyCaching.Bus.FreeRedis.Tests/EasyCaching.Bus.FreeRedis.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="EasyCaching.BaseTest" Version="1.9.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\EasyCaching.Bus.FreeRedis\EasyCaching.Bus.FreeRedis.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace EasyCaching.Bus.FreeRedis.Tests | ||
{ | ||
public class UnitTest1 | ||
{ | ||
[Fact] | ||
public void Test1() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |