You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB
C#

using System;
using Enyim.Caching;
using Enyim.Caching.Memcached;
using Microsoft.Extensions.Logging;
namespace Expedience.Infrastructure.Concurrency
{
public interface IDistributedLock
{
bool AcquireLock(string lockKey, TimeSpan timeout);
void ReleaseLock(string lockKey);
}
public class DistributedLock
{
private readonly IMemcachedClient _memcachedClient;
public DistributedLock(ILogger<DistributedLock> logger, IMemcachedClient memcachedClient)
{
_memcachedClient = memcachedClient;
}
public bool AcquireLock(string lockKey, TimeSpan timeout, TimeSpan lockExpiration)
{
var startTime = DateTime.UtcNow;
while (true)
{
if (_memcachedClient.Store(StoreMode.Add, lockKey, true, lockExpiration))
{
return true; // Lock acquired
}
if (DateTime.UtcNow - startTime > timeout)
{
return false; // Timeout
}
Thread.Sleep(100); // Wait and try again
}
}
public void ReleaseLock(string lockKey)
{
_memcachedClient.Remove(lockKey);
}
}
}