VEU.WathBody/3-app/1-lib/Kit.Helpers/Service/Memory/MemoryLockDictionaryService...

85 lines
2.9 KiB
C#

//namespace Kit.Helpers
//{
// /// <summary>Сервис для синхронизации потоков по ключу на основе словаря в памяти приложения</summary>
// /// <typeparam name="TKey">ключ синхронизации</typeparam>
// public sealed class MemoryLockDictionaryService : ILockDictionaryService
// {
// private object _lockObjectLock;
// private IDictionary<string, object> _lockObjects;
// public MemoryLockDictionaryService()
// {
// _lockObjectLock = new object();
// _lockObjects = new Dictionary<string, object>();
// }
// private string GetTypeMask(string type) => $"$t${(type ?? "no_type")}$t$";
// private string GetKeyMask(string key) => $"$k${(key ?? "no_key")}$k$";
// private string GetLockKey(string type, string key) => $"{GetTypeMask(type)}_{GetKeyMask(key)}";
// public int GetLockCount(string type) => _lockObjects.Count(x => x.Key.StartsWith(GetTypeMask(type)));
// public int GetLockCount(string type, string key) => _lockObjects.Count(x => x.Key.Equals(GetLockKey(type, key)));
// private object GetLockObject(string lockKey)
// {
// lock (_lockObjectLock)
// {
// if (_lockObjects.ContainsKey(lockKey) == false)
// _lockObjects.Add(lockKey, new object());
// return _lockObjects[lockKey];
// }
// }
// private void RemoveLockObject(string lockKey)
// {
// // дожидаемся освобождения объекта
// lock (this.GetLockObject(lockKey))
// {
// }
// // удаляем объект блокировки из очереди
// lock (_lockObjectLock)
// {
// try
// {
// _lockObjects.Remove(lockKey);
// }
// catch { }
// }
// }
// public void ExecuteOnLock(string type, string key, Action action)
// {
// string lockKey = GetLockKey(type, key);
// lock (GetLockObject(lockKey))
// {
// try
// {
// action();
// }
// finally
// {
// RemoveLockObject(lockKey);
// }
// }
// }
// public TResult ExecuteOnLock<TResult>(string type, string key, Func<TResult> action)
// {
// string lockKey = GetLockKey(type, key);
// lock (GetLockObject(lockKey))
// {
// try
// {
// return action();
// }
// finally
// {
// RemoveLockObject(lockKey);
// }
// }
// }
// }
//}