/// <summary> /// 获取一个锁(需要自己释放) /// </summary> /// <param name="key">锁的键</param> /// <param name="value">当前占用值</param> /// <param name="span">耗时时间</param> /// <returns>成功返回true</returns> public bool LockTake(string key, string value, TimeSpan span) { EnsureUtil.NotNullAndNotEmpty(key, "Lockkey"); EnsureUtil.NotNullAndNotEmpty(value, "Lockvalue"); var obj = _LockCache.GetValue(key, () => { return(new object()); }); Monitor.Enter(obj); _LockUserCache[key] = value; LogUtil.Debug($"{key}:{value}获取锁成功"); return(true); }
/// <summary> /// 获取一个锁(需要自己释放) /// </summary> /// <param name="key">锁的键</param> /// <param name="value">当前占用值</param> /// <param name="span">耗时时间</param> /// <returns>成功返回true</returns> public bool LockTake(string key, string value, TimeSpan span) { EnsureUtil.NotNullAndNotEmpty(key, "Lockkey"); EnsureUtil.NotNullAndNotEmpty(value, "Lockvalue"); var obj = _LockCache.GetValue(key, () => { return(new object()); }); if (Monitor.TryEnter(obj, span)) { _LockUserCache[key] = value; return(true); } return(false); }
/// <summary> /// 释放一个锁 /// </summary> /// <param name="key">锁的键</param> /// <param name="value">当前占用值</param> /// <returns>成功返回true</returns> public bool LockRelease(string key, string value) { EnsureUtil.NotNullAndNotEmpty(key, "Lockkey"); EnsureUtil.NotNullAndNotEmpty(value, "Lockvalue"); _LockCache.TryGetValue(key, out object obj); if (obj != null) { if (_LockUserCache[key] == value) { Monitor.Exit(obj); return(true); } return(false); } return(true); }
/// <summary> /// .ctor /// </summary> /// <param name="dataAccessFactory"></param> /// <param name="configName"></param> public BaseUnitOfWork(IDataAccessFactory dataAccessFactory, string configName) { EnsureUtil.NotNullAndNotEmpty(configName, "configName"); _dataAccessFactory = dataAccessFactory; _configName = configName; }