Skip to content

Commit

Permalink
✨ CSRedis Bus
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Feb 11, 2019
1 parent 51dfc9c commit edc0453
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 15 deletions.
7 changes: 7 additions & 0 deletions EasyCaching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.PerformanceTest
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.CSRedis", "src\EasyCaching.CSRedis\EasyCaching.CSRedis.csproj", "{6584761E-E51C-408F-BE51-CA0F6269589B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Bus.CSRedis", "src\EasyCaching.Bus.CSRedis\EasyCaching.Bus.CSRedis.csproj", "{861E5373-BEF6-4AA2-92C7-8F4941A079E7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -133,6 +135,10 @@ Global
{6584761E-E51C-408F-BE51-CA0F6269589B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6584761E-E51C-408F-BE51-CA0F6269589B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6584761E-E51C-408F-BE51-CA0F6269589B}.Release|Any CPU.Build.0 = Release|Any CPU
{861E5373-BEF6-4AA2-92C7-8F4941A079E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{861E5373-BEF6-4AA2-92C7-8F4941A079E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{861E5373-BEF6-4AA2-92C7-8F4941A079E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{861E5373-BEF6-4AA2-92C7-8F4941A079E7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CE61FAA2-0233-451C-991D-4222ED61C84B} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
Expand All @@ -155,5 +161,6 @@ Global
{2A8D7103-DF64-47B6-A406-8F8559AF7E7F} = {EBB55F65-7D07-4281-8D5E-7B0CA88E1AD0}
{6EBE36A2-F128-4C63-B90A-B700D8C2F2E8} = {EBB55F65-7D07-4281-8D5E-7B0CA88E1AD0}
{6584761E-E51C-408F-BE51-CA0F6269589B} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
{861E5373-BEF6-4AA2-92C7-8F4941A079E7} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
EndGlobalSection
EndGlobal
31 changes: 16 additions & 15 deletions build/version.props
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<Project>
<PropertyGroup>
<EasyCachingCorePackageVersion>0.5.0.1</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>0.5.0</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>0.5.0</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>0.5.0</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>0.5.0</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>0.5.0.1</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>0.5.0</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>0.5.0</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>0.5.0</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>0.5.0</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>0.5.0</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>0.5.0</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>0.5.0</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>0.5.0.1</EasyCachingRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>0.5.0-beta</EasyCachingRabbitBusPackageVersion>
<EasyCachingCorePackageVersion>0.5.1-alpha</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>0.5.1-alpha</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>0.5.1-alpha</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>0.5.1-alpha</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>0.5.1-alpha</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>0.5.1-alpha</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>0.5.1-alpha</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>0.5.1-alpha</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>0.5.1-alpha</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>0.5.1-alpha</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>0.5.1-alpha</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>0.5.1-alpha</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>0.5.1-alpha</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>0.5.1-alpha</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>0.5.1-alpha</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>0.5.1-alpha</EasyCachingRabbitBusPackageVersion>
</PropertyGroup>
</Project>
23 changes: 23 additions & 0 deletions src/EasyCaching.Bus.CSRedis/Configurations/CSRedisBusOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace EasyCaching.Bus.CSRedis
{
using System;
using System.Collections.Generic;

/// <summary>
/// Redis bus options.
/// </summary>
public class CSRedisBusOptions
{
/// <summary>
/// Gets or sets the node rule.
/// </summary>
/// <value>The node rule.</value>
public Func<string, string> NodeRule { get; set; } = null;

/// <summary>
/// Gets or sets the connection strings.
/// </summary>
/// <value>The connection strings.</value>
public List<string> ConnectionStrings { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace EasyCaching.Bus.CSRedis
{
using System;
using EasyCaching.Core.Bus;
using EasyCaching.Core.Configurations;
using EasyCaching.Core.Serialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

/// <summary>
/// Redis options extension.
/// </summary>
internal sealed class CSRedisOptionsExtension : IEasyCachingOptionsExtension
{
/// <summary>
/// The name.
/// </summary>
private const string _name = "easycachingbus";

/// <summary>
/// The configure.
/// </summary>
private readonly Action<CSRedisBusOptions> _configure;

/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.CSRedis.RedisOptionsExtension"/> class.
/// </summary>
/// <param name="name">Name.</param>
/// <param name="configure">Configure.</param>
public CSRedisOptionsExtension(Action<CSRedisBusOptions> configure)
{
this._configure = configure;
}

/// <summary>
/// Adds the services.
/// </summary>
/// <param name="services">Services.</param>
public void AddServices(IServiceCollection services)
{
services.AddOptions();

services.TryAddSingleton<IEasyCachingSerializer, DefaultBinaryFormatterSerializer>();

services.Configure(_name, _configure);

services.AddSingleton<EasyCachingCSRedisClient>(x =>
{
var optionsMon = x.GetRequiredService<IOptionsMonitor<CSRedisBusOptions>>();
var options = optionsMon.Get(_name);

var conns = options.ConnectionStrings;
var rule = options.NodeRule;

if (conns.Count == 1)
{
var redisClient = new EasyCachingCSRedisClient(_name, conns[0]);
return redisClient;
}
else
{
var redisClient = new EasyCachingCSRedisClient(_name, rule, conns.ToArray());
return redisClient;
}
});

services.AddSingleton<IEasyCachingBus, DefaultCSRedisBus>();
}

/// <summary>
/// Withs the services.
/// </summary>
/// <param name="services">Services.</param>
public void WithServices(IApplicationBuilder services)
{
// Method intentionally left empty.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace EasyCaching.Bus.CSRedis
{
using System;
using global::CSRedis;

public class EasyCachingCSRedisClient : CSRedisClient
{
private readonly string _name;

public string Name { get { return this._name; } }

public EasyCachingCSRedisClient(string name, string connectionString)
: base(connectionString)
{
this._name = name;
}

public EasyCachingCSRedisClient(string name, Func<string, string> NodeRule, params string[] connectionStrings)
: base(NodeRule, connectionStrings)
{
this._name = name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace EasyCaching.Bus.CSRedis
{
using System;
using EasyCaching.Core;
using EasyCaching.Core.Configurations;
using Microsoft.Extensions.Configuration;

/// <summary>
/// EasyCaching options extensions.
/// </summary>
public static class EasyCachingOptionsExtensions
{
/// <summary>
/// Withs the redis bus.
/// </summary>
/// <returns>The redis bus.</returns>
/// <param name="options">Options.</param>
/// <param name="configure">Configure.</param>
public static EasyCachingOptions WithCSRedisBus(this EasyCachingOptions options, Action<CSRedisBusOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

options.RegisterExtension(new CSRedisOptionsExtension(configure));
return options;
}

/// <summary>
/// Withs the redis bus.
/// </summary>
/// <returns>The redis bus.</returns>
/// <param name="options">Options.</param>
/// <param name="configuration">Configuration.</param>
/// <param name="sectionName">Section name.</param>
public static EasyCachingOptions WithCSRedisBus(this EasyCachingOptions options, IConfiguration configuration, string sectionName = EasyCachingConstValue.RedisBusSection)
{
var dbConfig = configuration.GetSection(sectionName);
var redisOptions = new CSRedisBusOptions();
dbConfig.Bind(redisOptions);

void configure(CSRedisBusOptions x)
{
x.ConnectionStrings = redisOptions.ConnectionStrings;
}

options.RegisterExtension(new CSRedisOptionsExtension(configure));
return options;
}
}
}
83 changes: 83 additions & 0 deletions src/EasyCaching.Bus.CSRedis/DefaultCSRedisBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace EasyCaching.Bus.CSRedis
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EasyCaching.Core.Bus;
using Newtonsoft.Json;

public class DefaultCSRedisBus : IEasyCachingBus
{
/// <summary>
/// The cache.
/// </summary>
private readonly EasyCachingCSRedisClient _client;

/// <summary>
/// The handler.
/// </summary>
private Action<EasyCachingMessage> _handler;

/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.Bus.CSRedis.DefaultCSRedisBus"/> class.
/// </summary>
/// <param name="clients">Clients.</param>
public DefaultCSRedisBus(IEnumerable<EasyCachingCSRedisClient> clients)
{
this._client = clients.FirstOrDefault(x => x.Name.Equals("easycachingbus"));
}

/// <summary>
/// Publish the specified topic and message.
/// </summary>
/// <param name="topic">Topic.</param>
/// <param name="message">Message.</param>
public void Publish(string topic, EasyCachingMessage message)
{
var msg = JsonConvert.SerializeObject(message);

_client.Publish(topic, msg);
}

/// <summary>
/// Publishs the async.
/// </summary>
/// <returns>The async.</returns>
/// <param name="topic">Topic.</param>
/// <param name="message">Message.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public async Task PublishAsync(string topic, EasyCachingMessage message, CancellationToken cancellationToken = default(CancellationToken))
{
var msg = JsonConvert.SerializeObject(message);

await _client.PublishAsync(topic, msg);
}

/// <summary>
/// Subscribe the specified topic and action.
/// </summary>
/// <param name="topic">Topic.</param>
/// <param name="action">Action.</param>
public void Subscribe(string topic, Action<EasyCachingMessage> action)
{
_handler = action;

_client.Subscribe(
(topic, msg => OnMessage(msg.Body))
);
}

/// <summary>
/// Ons the message.
/// </summary>
/// <param name="body">Body.</param>
private void OnMessage(string body)
{
var message = JsonConvert.DeserializeObject<EasyCachingMessage>(body);

_handler?.Invoke(message);
}
}
}
33 changes: 33 additions & 0 deletions src/EasyCaching.Bus.CSRedis/EasyCaching.Bus.CSRedis.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../build/version.props" />
<Import Project="../../build/releasenotes.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Owners>Catcher Wong</Owners>
<Authors>Catcher Wong</Authors>
<Version>$(EasyCachingRedisBusPackageVersion)</Version>
<Description>
A simple caching bus(message bus) based on CSRedis.
</Description>
<PackageTags>Bus,Hybrid,Redis,Caching,Cache</PackageTags>
<PackageProjectUrl>https://github.com/dotnetcore/EasyCaching</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/dotnetcore/EasyCaching/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/dotnetcore/EasyCaching</RepositoryUrl>
<ProjectUrl>https://github.com/dotnetcore/EasyCaching</ProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/dotnetcore/EasyCaching/master/media/nuget-icon.png</PackageIconUrl>
<PackageReleaseNotes>
$(EasyCachingRedisBusPackageNotes)
</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSRedisCore" Version="3.0.33" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EasyCaching.Core\EasyCaching.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Configurations\" />
</ItemGroup>
</Project>

0 comments on commit edc0453

Please sign in to comment.