Loki provides an easy way to handle locking scenarios on distributed systems.
- Support Redis locking handler for primary
- Support MSSQL locking handler for secondary
- Multiple locking handlers can be added such as MongoDB etc
- Secondary locking handler can be set for against connection failure problems
Firstly you have to easily initialize the Loki with LokiConfigurationBuilder.
List<EndPoint> redisEndPoints = new List<EndPoint>
{
new DnsEndPoint("redisUri", redisPort)
};
LokiConfigurationBuilder.Instance.SetServiceKey("SimpleTestClient")
.SetPrimaryLockHandler(new RedisLokiLockHandler(redisEndPoints.ToArray()))
.Build();
Then just use Locking.Instance.ExecuteWithinLock() method where you want to provide concurrency.
Locking.Instance.ExecuteWithinLock(() =>
{
//do somethings..
}, expiryFromSeconds: 2);
Also you can easily implement custom locking handlers.
public class FooLockHandler : LokiLockHandler
{
public override bool Lock(string serviceKey, int expiryFromSeconds)
{
//Lock operations
}
public override void Release(string serviceKey)
{
//Release operations
}
}
If you want to use MSSQL locking handler for secondary, firstly you need to create LokiLockings table as below:
CREATE TABLE [dbo].[LokiLockings](
[ServiceKey] [varchar](50) NOT NULL,
[CreationDate] [datetime] NOT NULL,
CONSTRAINT [PK_LokiLockings] PRIMARY KEY CLUSTERED
(
[ServiceKey] ASC
))
then just:
LokiConfigurationBuilder.Instance.SetServiceKey("SimpleTestClient")
.SetPrimaryLockHandler(new RedisLokiLockHandler(redisEndPoints.ToArray()))
.SetSecondaryLockHandler(new MSSQLLokiLockHandler("connectionString"))
.Build();