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(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) { SenparcTrace.Log("Memcached同步锁发生异常:" + ex.Message); return(false); } } ); return(lockResult); }
public override bool Lock(string resourceName, int retryCount, TimeSpan retryDelay) { var key = _mamcachedStrategy.GetFinalKey(resourceName); var successfull = RetryLock(key, retryCount /*暂时不限制*/, retryDelay, () => { try { if (_mamcachedStrategy.Cache.Store(StoreMode.Add, key, new object(), new TimeSpan(0, 0, 10))) { return(true);//取得锁 } else { 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) { SenparcTrace.Log("Memcached同步锁发生异常:" + ex.Message); return(false); } } ); return(successfull); }