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
2 changes: 1 addition & 1 deletion build/releasenotes.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
1. Upgrading dependencies.
</EasyCachingProtobufPackageNotes>
<EasyCachingCSRedisPackageNotes>
1. IRedisCachingProvider Support SearchKeysAsync.
1. Fix prefix issue.
</EasyCachingCSRedisPackageNotes>
<EasyCachingCSRedisBusPackageNotes>
1. Upgrading dependencies.
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<EasyCachingJsonPackageVersion>1.5.2</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>1.5.2</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>1.5.2</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>1.5.2</EasyCachingCSRedisPackageVersion>
<EasyCachingCSRedisPackageVersion>1.5.3</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>1.5.2</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>1.5.2</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>1.5.2</EasyCachingRabbitBusPackageVersion>
Expand Down
8 changes: 8 additions & 0 deletions src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ private string HandlePrefix(string prefix)
if (!prefix.EndsWith("*", StringComparison.OrdinalIgnoreCase))
prefix = string.Concat(prefix, "*");

if (!string.IsNullOrWhiteSpace(_cache.Nodes?.Values?.FirstOrDefault()?.Prefix))
prefix = _cache.Nodes?.Values?.FirstOrDefault()?.Prefix + prefix;

return prefix;
}

Expand All @@ -289,6 +292,11 @@ private string[] SearchRedisKeys(string pattern)
}
while (nextCursor != 0);

var prefix = _cache.Nodes?.Values?.FirstOrDefault()?.Prefix;

if (!string.IsNullOrWhiteSpace(prefix))
keys = keys.Select(x => x.Remove(0, prefix.Length)).ToList();

return keys.Distinct().ToArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,87 @@ public void NamedSerializerTest()
Assert.Equal(EasyCachingConstValue.DefaultSerializerName, info3.Serializer.Name);
}
}

public class CSRedisCachingProviderWithKeyPrefixTest
{
private readonly IEasyCachingProviderFactory _providerFactory;

public CSRedisCachingProviderWithKeyPrefixTest()
{
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(x =>
{
x.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=3,poolsize=10"
}
};

config.SerializerName = "json";
}, "NotKeyPrefix");

x.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=3,poolsize=10,prefix=foo:"
}
};
config.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);
}

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

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

var val1 = WithKeyPrefix.Get<string>("KeyPrefix1");
var val2 = WithKeyPrefix.Get<string>("KeyPrefix2");

Assert.True(val1.HasValue);
Assert.True(val2.HasValue);
Assert.Equal(val1.Value, val2.Value);

WithKeyPrefix.RemoveByPrefix("Key");

var val3 = WithKeyPrefix.Get<string>("KeyPrefix1");
var val4 = WithKeyPrefix.Get<string>("KeyPrefix2");
Assert.False(val3.HasValue);
Assert.False(val4.HasValue);
}
}
}