private RunningSemaphore AcquireRunningSemaphore(string cacheKey, IEnumerable <KeyValuePair <string, string> > attributes, TimeSpan maxAge) { RunningSemaphore semaphore = null; var orderedAttributes = attributes.OrderBy(a => a.Key).ToArray(); lock (_runningSemaphores) { semaphore = _runningSemaphores.FirstOrDefault( r => r.Key == cacheKey && r.MaxAge == maxAge && r.Attributes.OrderBy(a => a.Key).SequenceEqual(orderedAttributes) ); if (semaphore == null) { semaphore = new RunningSemaphore() { Attributes = orderedAttributes, Key = cacheKey, MaxAge = maxAge, Semaphore = new SemaphoreSlim(1) }; _runningSemaphores.Add(semaphore); } } return(semaphore); }
private void RemoveSemaphore(RunningSemaphore runningSemaphore) { lock (_runningSemaphores) { _runningSemaphores.Remove(runningSemaphore); } }