Skip to content
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
5 changes: 5 additions & 0 deletions src/EasyCaching.Redis/Configurations/RedisDBOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ public class RedisDBOptions : BaseRedisOptions
/// Specifies the time in milliseconds that the system should allow for synchronous operations (defaults to 5 seconds)
/// </summary>
public int SyncTimeout { get; set; }

/// <summary>
/// Gets or sets the Redis database KeyPrefix will use.
/// </summary>
public string KeyPrefix { get; set; }
}
}
9 changes: 7 additions & 2 deletions src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace EasyCaching.Redis
{
using StackExchange.Redis;
using StackExchange.Redis.KeyspaceIsolation;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -20,7 +21,7 @@ public class RedisDatabaseProvider : IRedisDatabaseProvider
/// The connection multiplexer.
/// </summary>
private Lazy<ConnectionMultiplexer> _connectionMultiplexer;

public RedisDatabaseProvider(string name, RedisOptions options)
{
_options = options.DBConfig;
Expand All @@ -39,7 +40,11 @@ public IDatabase GetDatabase()
{
try
{
return _connectionMultiplexer.Value.GetDatabase();

var database = _connectionMultiplexer.Value.GetDatabase();
if (!string.IsNullOrEmpty(_options.KeyPrefix))
database = database.WithKeyPrefix(_options.KeyPrefix);
return database;
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,61 @@ public void NamedSerializerTest()
Assert.Equal(EasyCachingConstValue.DefaultSerializerName, info3.Serializer.Name);
}
}

public class RedisCachingProviderWithKeyPrefixTest
{
private readonly IEasyCachingProviderFactory _providerFactory;

public RedisCachingProviderWithKeyPrefixTest()
{
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(x =>
{

x.UseRedis(options =>
{
options.DBConfig = new RedisDBOptions
{
AllowAdmin = true
};
options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380));
options.DBConfig.Database = 8;
options.SerializerName = "json";
}, "NotKeyPrefix");

x.UseRedis(options =>
{
options.DBConfig = new RedisDBOptions
{
AllowAdmin = true,
KeyPrefix = "foo:"
};
options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380));
options.DBConfig.Database = 8;
options.SerializerName = "json";
}, "WithKeyPrefix");

x.WithJson("json");
});

IServiceProvider serviceProvider = services.BuildServiceProvider();
_providerFactory = serviceProvider.GetService<IEasyCachingProviderFactory>();
}

[Fact]
public void KeyPrefixTest()
{
var NotKeyPrefix = _providerFactory.GetCachingProvider("NotKeyPrefix");
var WithKeyPrefix = _providerFactory.GetCachingProvider("WithKeyPrefix");

WithKeyPrefix.Set("KeyPrefix", "ok", TimeSpan.FromSeconds(10));

var val1 = NotKeyPrefix.Get<string>("foo:" + "KeyPrefix");
var val2 = WithKeyPrefix.Get<string>("foo:" + "KeyPrefix");
Assert.NotEqual(val1.Value, val2.Value);

var val3 = WithKeyPrefix.Get<string>("KeyPrefix");
Assert.Equal(val1.Value, val3.Value);
}
}
}