forked from dotnetcore/EasyCaching
-
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
1 parent
51dfc9c
commit edc0453
Showing
8 changed files
with
319 additions
and
15 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
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 |
---|---|---|
@@ -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
23
src/EasyCaching.Bus.CSRedis/Configurations/CSRedisBusOptions.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,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; } | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/EasyCaching.Bus.CSRedis/Configurations/CSRedisOptionsExtension.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,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. | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/EasyCaching.Bus.CSRedis/Configurations/EasyCachingCSRedisClient.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,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; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/EasyCaching.Bus.CSRedis/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,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; | ||
} | ||
} | ||
} |
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,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
33
src/EasyCaching.Bus.CSRedis/EasyCaching.Bus.CSRedis.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,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> |