private bool TryAcquireLock(string key, TimeSpan timeout) { lock (_lockObject) { if (!_cache.Contains(key)) { _cache.SetString(key, key, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = timeout, }); _logger?.LogInformation($"Acquired lock for key '{key}'."); return true; } } _logger?.LogInformation($"Key '{key}' is locked by another request."); return false; }
public static bool Contains(this IDistributedCache cache, DistributedCacheRegistration key, params object[] values) => cache.Contains(key.GetNamespace(values));