-
Notifications
You must be signed in to change notification settings - Fork 26
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
Configurable prefix for redis key #121
Comments
Hi @sizzle168, Yes, you can configure a prefix for the redis key, here's a related issue: #93 |
@cristipufu
I need a prefix before "rl:" |
You can play around with partitionKey - does it have to be before "rl:"? |
That wouldn't be that helpful. We have different applications running on the same redis db with a app-specific redis key-prefix and I would want to put that rate limit values under the prefix of our app. |
You can do something like: return RedisRateLimitPartition.GetFixedWindowRateLimiter($"{appName}", _ =>
new RedisFixedWindowRateLimiterOptions
{
ConnectionMultiplexerFactory = () => connectionMultiplexer,
PermitLimit = 2,
Window = TimeSpan.FromSeconds(1)
}); The key in Redis would be "rl:{appName}" |
Recently I was also searching for an option to easily add prefixes. Though in my case it was for parallel test execution with a real redis during integration tests. There is public static class IConnectionMultiplexerExtensions
{
public static IConnectionMultiplexer WithKeyPrefix(this IConnectionMultiplexer multiplexer, string prefix)
{
if (String.IsNullOrEmpty(prefix)) {
return multiplexer;
}
return GetDatabaseWithKeyPrefixProxy.Decorate(multiplexer, prefix);
}
private class GetDatabaseWithKeyPrefixProxy : DispatchProxy
{
public string? Prefix { get; private set; }
public IConnectionMultiplexer? Target { get; private set; }
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
{
if (targetMethod?.Name == nameof(IConnectionMultiplexer.GetDatabase)) {
return ((IDatabase?)targetMethod?.Invoke(Target, args))?.WithKeyPrefix(Prefix);
}
return targetMethod?.Invoke(Target, args);
}
public static IConnectionMultiplexer Decorate(IConnectionMultiplexer multi, String prefix)
{
var proxy = (Create<IConnectionMultiplexer, GetDatabaseWithKeyPrefixProxy>() as GetDatabaseWithKeyPrefixProxy)!;
proxy.Target = multi;
proxy.Prefix = prefix;
return (proxy as IConnectionMultiplexer)!;
}
}
} It works just fine for me but I only use it for tests. I imagine due to the dynamic nature of this and the breadth of |
Hey,
thanks for providing this useful package. I was wondering if there is a way to configure a prefix for the redis key? Currently, it could lead to problems if several applications share the same redis database.
Thanks in advance.
The text was updated successfully, but these errors were encountered: