madelson / DistributedLock

A .NET library for distributed synchronization
MIT License
1.74k stars 182 forks source link

Document Support for Key Prefixes in RedisDistributedLock #178

Open teomane opened 6 months ago

teomane commented 6 months ago

Hello,

I'm encountering an issue with RedisDistributedLock. Currently, I am using a single Redis instance for multiple environments (such as dev and test). My goal is to use the same key in the TryAcquireLockAsync method but with different prefixes for each environment.

For example, in the application code, I have: _distributedLockProvider.AcquireLockAsync(“DO_SOMETHING”);

However, in Redis, I need the keys to be environment-specific, like dev:DO_SOMETHING for the development environment and test:DO_SOMETHING for the test environment.

As a reference, in StackExchangeRedisCache, this is achieved as follows:

services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = “your_configuration”;
    options.InstanceName = $"{Environment}:";
});

In this case, InstanceName serves as a prefix.

Would it be a good idea to consider adding an option for this in RedisDistributedSynchronizationOptionsBuilder during startup configuration?

madelson commented 6 months ago

Hi @teomane thanks for your interest in the library!

I wonder if the approach described here could work for you: https://github.com/madelson/DistributedLock/pull/65

This leverages native autoprefixing support from the Redis client.

teomane commented 6 months ago

Hi @madelson ,

It works for me! Thank you for your response.

madelson commented 6 months ago

Great. I’ll keep this issue open as a reminder to add a note about this in the documentation