public override async Task <ICacheLock> LockAsync() { var key = _mamcachedStrategy.GetFinalKey(_resourceName); var lockResult = await RetryLockAsync(key, _retryCount, _retryDelay, async() => { try { var ttl = base.GetTotalTtl(_retryCount, _retryDelay); if (await _mamcachedStrategy.Cache.StoreAsync(StoreMode.Add, key, new object(), TimeSpan.FromMilliseconds(ttl))) { base.LockSuccessful = true; return(true);//取得锁 } else { base.LockSuccessful = false; return(false);//已被别人锁住,没有取得锁 } } catch (Exception ex) { LogTrace.Log("Memcached同步锁发生异常:" + ex.Message); return(false); } } ).ConfigureAwait(false); return(lockResult); }
/// <summary> /// Checks that all the folders we define here are accessible, and if not /// creates them. If something goes wrong returns False. /// </summary> public static bool InitializeFolders() { foreach (string path in FoldersList) { if (!CheckAndCreateDir(path)) { LogTrace.Error("Unable to create folder {0}", path); return(false); } } LogTrace.Log("Folders initialized for {0}", DashboardPath); return(true); }
/// <summary> /// 立即等待并抢夺锁 /// </summary> /// <returns></returns> public override ICacheLock Lock() { int currentRetry = 0; int maxRetryDelay = (int)_retryDelay.TotalMilliseconds; while (currentRetry++ < _retryCount) { #region 尝试获得锁 var getLock = false; try { lock (lookPoolLock) { if (LockPool.ContainsKey(_resourceName)) { getLock = false;//已被别人锁住,没有取得锁 } else { LockPool.Add(_resourceName, new object()); //创建锁 getLock = true; //取得锁 } } } catch (Exception ex) { LogTrace.Log("本地同步锁发生异常:" + ex.Message); getLock = false; } #endregion if (getLock) { LockSuccessful = true; return(this);//取得锁 } Thread.Sleep(_rnd.Next(maxRetryDelay)); } LockSuccessful = false; return(this); }
public override ICacheLock Lock() { var key = _mamcachedStrategy.GetFinalKey(_resourceName); var lockResult = RetryLock(key, _retryCount, _retryDelay, () => { try { var ttl = base.GetTotalTtl(_retryCount, _retryDelay); if (_mamcachedStrategy.Cache.Store(Enyim.Caching.Memcached.StoreMode.Add, key, new object(), TimeSpan.FromMilliseconds(ttl))) { base.LockSuccessful = true; return(true);//取得锁 } else { base.LockSuccessful = false; return(false);//已被别人锁住,没有取得锁 } //if (_mamcachedStrategy._cache.Get(key) != null) //{ // return false;//已被别人锁住,没有取得锁 //} //else //{ // _mamcachedStrategy._cache.Store(StoreMode.set, key, new object(), new TimeSpan(0, 0, 10));//创建锁 // return true;//取得锁 //} } catch (Exception ex) { LogTrace.Log("Memcached同步锁发生异常:" + ex.Message); return(false); } } ); return(lockResult); }
/// <summary> /// 记录系统日志 /// </summary> /// <param name="messageFormat"></param> /// <param name="param"></param> public static void Log(string messageFormat, params object[] param) { LogTrace.Log(messageFormat.FormatWith(param)); }