Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
Memoyu committed Jun 5, 2023
1 parent ecfe594 commit d494d92
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 2 deletions.
18 changes: 16 additions & 2 deletions EasyCachingFreeRedisExt.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{2A6F
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.FreeRedis", "src\EasyCaching.FreeRedis\EasyCaching.FreeRedis.csproj", "{32C59B57-FADA-460B-A02C-926C0815A107}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Demo.FreeRedis", "samples\EasyCaching.Demo.FreeRedis\EasyCaching.Demo.FreeRedis.csproj", "{96DD4331-8B12-405F-8CB4-23D3776916AC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Demo.FreeRedis", "samples\EasyCaching.Demo.FreeRedis\EasyCaching.Demo.FreeRedis.csproj", "{96DD4331-8B12-405F-8CB4-23D3776916AC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.FreeRedis.Tests", "tests\EasyCaching.FreeRedis.Tests\EasyCaching.FreeRedis.Tests.csproj", "{925BA316-CE38-4051-B2E9-CAFC7F524D56}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.FreeRedis.Tests", "tests\EasyCaching.FreeRedis.Tests\EasyCaching.FreeRedis.Tests.csproj", "{925BA316-CE38-4051-B2E9-CAFC7F524D56}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Bus.FreeRedis", "src\EasyCaching.Bus.FreeRedis\EasyCaching.Bus.FreeRedis.csproj", "{AEB342AE-DA45-4AB5-9B55-4FF2AA7505E5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyCaching.Bus.FreeRedis.Tests", "tests\EasyCaching.Bus.FreeRedis.Tests\EasyCaching.Bus.FreeRedis.Tests.csproj", "{2712CDAD-713D-4767-AE39-6FAA6115D9A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -33,6 +37,14 @@ Global
{925BA316-CE38-4051-B2E9-CAFC7F524D56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{925BA316-CE38-4051-B2E9-CAFC7F524D56}.Release|Any CPU.ActiveCfg = Release|Any CPU
{925BA316-CE38-4051-B2E9-CAFC7F524D56}.Release|Any CPU.Build.0 = Release|Any CPU
{AEB342AE-DA45-4AB5-9B55-4FF2AA7505E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AEB342AE-DA45-4AB5-9B55-4FF2AA7505E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AEB342AE-DA45-4AB5-9B55-4FF2AA7505E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AEB342AE-DA45-4AB5-9B55-4FF2AA7505E5}.Release|Any CPU.Build.0 = Release|Any CPU
{2712CDAD-713D-4767-AE39-6FAA6115D9A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2712CDAD-713D-4767-AE39-6FAA6115D9A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2712CDAD-713D-4767-AE39-6FAA6115D9A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2712CDAD-713D-4767-AE39-6FAA6115D9A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -41,6 +53,8 @@ Global
{32C59B57-FADA-460B-A02C-926C0815A107} = {51B59C2F-8D4A-4096-B830-7937DF3DBF87}
{96DD4331-8B12-405F-8CB4-23D3776916AC} = {2A6FB3DC-B657-4ADC-93F4-47888EE98F9E}
{925BA316-CE38-4051-B2E9-CAFC7F524D56} = {9E00FF89-215B-495E-9CAE-35B244224EEC}
{AEB342AE-DA45-4AB5-9B55-4FF2AA7505E5} = {51B59C2F-8D4A-4096-B830-7937DF3DBF87}
{2712CDAD-713D-4767-AE39-6FAA6115D9A6} = {9E00FF89-215B-495E-9CAE-35B244224EEC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {30677E46-019F-435E-8CF9-613F1A96623B}
Expand Down
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;
}
}
}
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;
}
}
}
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; }
}
}
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>();
}
}
}
33 changes: 33 additions & 0 deletions src/EasyCaching.Bus.FreeRedis/DefaultFreeRedisBus.cs
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 src/EasyCaching.Bus.FreeRedis/EasyCaching.Bus.FreeRedis.csproj
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>
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>
11 changes: 11 additions & 0 deletions tests/EasyCaching.Bus.FreeRedis.Tests/UnitTest1.cs
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()
{

}
}
}
1 change: 1 addition & 0 deletions tests/EasyCaching.Bus.FreeRedis.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;

0 comments on commit d494d92

Please sign in to comment.