Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension method AddCSRedisCache for IServiceCollection #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Microsoft.Extensions.Caching.CSRedis/CSRedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Caching.CSRedis;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.Caching.Redis {

/// <summary>
/// Distributed cache implementation using Redis.
/// </summary>
public class CSRedisCache : IDistributedCache {
private CSRedis.CSRedisClient _redisClient;
public CSRedisCache(CSRedis.CSRedisClient redisClient) {
_redisClient = redisClient;
public CSRedisCache(IOptions<CSRedisCacheOptions> optionsAccessor) {
_redisClient = optionsAccessor.Value.CSRedisClient;
}
// KEYS[1] = = key
// ARGV[1] = absolute-expiration - ticks as long (-1 for none)
Expand Down
22 changes: 22 additions & 0 deletions src/Microsoft.Extensions.Caching.CSRedis/CSRedisCacheOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CSRedis;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Options;

namespace Caching.CSRedis
{
/// <summary>
/// Configuration options for <see cref="CSRedisCache"/>.
/// </summary>
public class CSRedisCacheOptions : IOptions<CSRedisCacheOptions>
{
/// <summary>
/// Redis Client
/// </summary>
public CSRedisClient CSRedisClient { get; set; }

CSRedisCacheOptions IOptions<CSRedisCacheOptions>.Value
{
get { return this; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Caching.CSRedis;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Redis;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
public static class CSRedisCacheServiceCollectionExtensions
{
/// <summary>
/// Adds Redis distributed caching services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">An <see cref="Action{CSRedisCacheOptions}"/> to configure the provided
/// <see cref="CSRedisCacheOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddCSRedisCache(this IServiceCollection services, Action<CSRedisCacheOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}

services.AddOptions();
services.Configure(setupAction);
services.Add(ServiceDescriptor.Singleton<IDistributedCache, CSRedisCache>());

return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp21'">
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.1.23" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CSRedisCore\CSRedisCore.csproj" />
Expand Down
4 changes: 3 additions & 1 deletion src/Microsoft.Extensions.Caching.CSRedis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

```csharp
var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultDatabase=13,ssl=false,writeBuffer=10240,poolsize=50,prefix=key前辍");
services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(csredis));
services.AddCSRedisCache(options => {
options.CSRedisClient = csredis;
});
```

# 集群模式
Expand Down