Skip to content

RedisCache configure code-only configuration for StackExchange.Redis #452

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

Merged
merged 1 commit into from
Feb 3, 2023
Merged
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
6 changes: 6 additions & 0 deletions src/EasyCaching.Redis/Configurations/RedisDBOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace EasyCaching.Redis
{
using EasyCaching.Core.Configurations;
using StackExchange.Redis;

/// <summary>
/// Redis cache options.
Expand Down Expand Up @@ -29,5 +30,10 @@ public class RedisDBOptions : BaseRedisOptions
/// Gets or sets the Redis database KeyPrefix will use.
/// </summary>
public string KeyPrefix { get; set; }

/// <summary>
/// Gets or sets the Redis database ConfigurationOptions will use.
/// </summary>
public ConfigurationOptions ConfigurationOptions { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public IEnumerable<IServer> GetServerList()
/// <returns>The connection multiplexer.</returns>
private ConnectionMultiplexer CreateConnectionMultiplexer()
{
if (_options.ConfigurationOptions != null)
return ConnectionMultiplexer.Connect(_options.ConfigurationOptions.ToString());

if (string.IsNullOrWhiteSpace(_options.Configuration))
{
var configurationOptions = new ConfigurationOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace EasyCaching.UnitTests
using EasyCaching.Serialization.Json;
using EasyCaching.Serialization.MessagePack;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using System;
using Xunit;

Expand Down Expand Up @@ -98,6 +99,24 @@ public void Use_Configuration_String_Should_Succeed()
Assert.Equal(8, dbProvider.GetDatabase().Database);
}

[Fact]
public void Use_Configuration_Options_Should_Succeed()
{
IServiceCollection services = new ServiceCollection();
var redisConfig = ConfigurationOptions.Parse("127.0.0.1:6380");
redisConfig.DefaultDatabase = 8;
services.AddEasyCaching(x =>
x.UseRedis(options =>
{
options.DBConfig.ConfigurationOptions = redisConfig;
}, ProviderName).UseRedisLock().WithJson(ProviderName));
IServiceProvider serviceProvider = services.BuildServiceProvider();
var dbProvider = serviceProvider.GetService<IRedisDatabaseProvider>();
Assert.NotNull(dbProvider);

Assert.Equal(8, dbProvider.GetDatabase().Database);
}

[Fact]
public void GetDatabase_Should_Succeed()
{
Expand Down