//Cas操作 //Cas 类似数据库的乐观锁,但是我们使用Memcached一般只是用来缓存数据,一般是不需要使用Cas的 public static void TestCas() { MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration(); memConfig.AddServer("127.0.0.1:11211"); using (MemcachedClient memClient = new MemcachedClient(memConfig)) { memClient.Store(Enyim.Caching.Memcached.StoreMode.Set, "Name", "shanzm"); CasResult <string> nameWithCas = memClient.GetWithCas <string>("Name"); Console.WriteLine($"读取的数据Name={nameWithCas.Result },Cas:{nameWithCas.Cas }"); //第一个运行的程序此时的Cas=1 Console.WriteLine("你此时在源文件中点击运行,“003Cas操作.exe”,抢先运行,点击回车进行下面的修改"); //另外的程序读取Name的Cas=2 Console.ReadKey(); CasResult <bool> updateWithCas = memClient.Cas(StoreMode.Set, "Name", "shanzm修改版本", nameWithCas.Cas); if (updateWithCas.Result) { Console.WriteLine("Name修改成功,现在的Cas:" + updateWithCas.Cas);//另外的程序此处的Cas为3 } else { Console.WriteLine("更新失败,被其他程序抢先了,现在的Cas:" + updateWithCas.Cas);//第一个运行的程序的Cas此时为0 } Console.ReadKey(); } }
private ErrorTypes GetFromMCWithCas(string sKey, out TaskResultData oTast, out ulong cas) { ErrorTypes oError = ErrorTypes.NoError; oTast = null; cas = 0; try { using (MemcachedClient oMc = new MemcachedClient(m_oMcConfig)) { CasResult <string> oGetData = oMc.GetWithCas <string>(sKey); if (oGetData.Result != null) { cas = oGetData.Cas; XmlSerializer oXmlSerializer = new XmlSerializer(typeof(TaskResultData)); using (StringReader oStringReader = new StringReader(oGetData.Result)) { oTast = (TaskResultData)oXmlSerializer.Deserialize(oStringReader); } } } } catch { oError = ErrorTypes.TaskResult; } return(oError); }
private CasedResult <T> Convert <T>(CasResult <T> casResult) { return(new CasedResult <T>() { Cas = casResult.Cas, Result = casResult.Result }); }
public void SetResult(CasResult casResult) { Condition.Requires(casResult).IsNotEqualTo(MemcacheIt.CasResult.None, "Cas result should be specified. Value of '{0}' is not acceptable.".FormatString(casResult)); _casResult = casResult; _succeded = casResult == MemcacheIt.CasResult.Stored; }
public void Cas_Should_Be_Called() { var validFor = new TimeSpan(0, 0, 1); var expiresAt = new DateTime(3000, 1, 1); var cas = new CasResult <object> { Cas = 1 }; // Test : CasResult<bool> Cas(StoreMode mode, string key, object value); // covered by 0L for cas // Test : CasResult<bool> Cas(StoreMode mode, string key, object value, ulong cas); var clientMock2 = new Mock <IMemcachedClient>(); var service2 = new MemcachedServiceCache(clientMock2.Object); service2.Add(cas, "name", CacheItemPolicy.Infinite, "value"); clientMock2.Verify(x => x.Cas(It.IsAny <StoreMode>(), "name", "value", cas.Cas)); // var clientMock2A = new Mock <IMemcachedClient>(); var service2A = new MemcachedServiceCache(clientMock2A.Object); service2A.Set(cas, "name", CacheItemPolicy.Infinite, "value"); clientMock2A.Verify(x => x.Cas(It.IsAny <StoreMode>(), "name", "value", cas.Cas)); // Test : CasResult<bool> Cas(StoreMode mode, string key, object value, DateTime expiresAt, ulong cas); var clientMock3 = new Mock <IMemcachedClient>(); var service3 = new MemcachedServiceCache(clientMock3.Object); service3.Add(cas, "name", new CacheItemPolicy { AbsoluteExpiration = expiresAt }, "value"); clientMock3.Verify(x => x.Cas(It.IsAny <StoreMode>(), "name", "value", expiresAt, cas.Cas)); // var clientMock3A = new Mock <IMemcachedClient>(); var service3A = new MemcachedServiceCache(clientMock3A.Object); service3A.Set(cas, "name", new CacheItemPolicy { AbsoluteExpiration = expiresAt }, "value"); clientMock3A.Verify(x => x.Cas(It.IsAny <StoreMode>(), "name", "value", expiresAt, cas.Cas)); // Test : CasResult<bool> Cas(StoreMode mode, string key, object value, TimeSpan validFor, ulong cas); var clientMock4 = new Mock <IMemcachedClient>(); var service4 = new MemcachedServiceCache(clientMock4.Object); service4.Add(cas, "name", new CacheItemPolicy { SlidingExpiration = validFor }, "value"); clientMock4.Verify(x => x.Cas(It.IsAny <StoreMode>(), "name", "value", validFor, cas.Cas)); // var clientMock4A = new Mock <IMemcachedClient>(); var service4A = new MemcachedServiceCache(clientMock4A.Object); service4A.Set(cas, "name", new CacheItemPolicy { SlidingExpiration = validFor }, "value"); clientMock4A.Verify(x => x.Cas(It.IsAny <StoreMode>(), "name", "value", validFor, cas.Cas)); }
public IActionResult Store(string key, [FromBody] StoreCacheItem item) { if (item == null) { return(this.NullValueErrorResult(key, _logger)); } if (!item.Cas.HasValue || item.Cas.Value == 0) { bool result = true; if (item.ValidFor.HasValue) { result = _memcachedClient.Store(GetMode(item), key, item.Value, item.ValidFor.Value); } else if (item.ExpireAt.HasValue) { result = _memcachedClient.Store(GetMode(item), key, item.Value, item.ExpireAt.Value); } else { result = _memcachedClient.Store(GetMode(item), key, item.Value); } if (!result) { return(StatusCode(StatusCodes.Status400BadRequest, result)); } return(StatusCode(StatusCodes.Status201Created)); } else { CasResult <bool> result = new CasResult <bool>(); if (item.ValidFor.HasValue) { result = _memcachedClient.Cas(GetMode(item), key, item.Value, item.ValidFor.Value, item.Cas.Value); } else if (item.ExpireAt.HasValue) { result = _memcachedClient.Cas(GetMode(item), key, item.Value, item.ExpireAt.Value, item.Cas.Value); } else { result = _memcachedClient.Cas(GetMode(item), key, item.Value, item.Cas.Value); } if (!result.Result) { return(StatusCode(StatusCodes.Status400BadRequest, result)); } return(StatusCode(StatusCodes.Status201Created, result)); } }
public bool TryGetWithCas(string key, out CasResult<object> value) { object tmp; ulong cas; var retval = this.PerformTryGet(key, out cas, out tmp); value = new CasResult<object> { Cas = cas, Result = tmp }; return retval.Success; }
private CasedResult <T> Convert2 <T>(CasResult <object> casResult) { var result = new CasedResult <T>(); result.Cas = casResult.Cas; if (casResult.Result != null) { result.Result = JsonConvert.DeserializeObject <T>(casResult.Result.ToString()); } return(result); }
public IGetOperationResult TryGetWithLock(string key, TimeSpan lockExpiration, out CasResult <object> value) { object tmp; ulong cas; var retval = this.PerformTryGetWithLock(key, lockExpiration, out cas, out tmp); value = new CasResult <object> { Cas = cas, Result = tmp }; return(retval); }
public bool TryGetWithCas(string key, DateTime newExpiration, out CasResult <object> value) { object tmp; ulong cas; var retval = this.PerformTryGetAndTouch(key, MemcachedClient.GetExpiration(null, newExpiration), out cas, out tmp).Success; value = new CasResult <object> { Cas = cas, Result = tmp }; return(retval); }
public void GetWithCas(string key, out ulong cas, out object value) { value = null; cas = 0; try { CasResult <object> result = clientInstance.Value.GetWithCas(ContactKey(key)); cas = result.Cas; value = result.Result; } catch (Exception ex) { LogHelper.LogException(logger, "EnyimMemcachedProvider.GetWithCas", ex, key); } }
public IActionResult Decrement(string key, [FromBody] IncrDecrCacheItem item) { if (item == null) { item = new IncrDecrCacheItem(); } if (!item.Cas.HasValue || item.Cas.Value == 0) { ulong result; if (item.ValidFor.HasValue) { result = _memcachedClient.Decrement(key, item.DefaultValue.Value, item.Delta.Value, item.ValidFor.Value); } else if (item.ExpireAt.HasValue) { result = _memcachedClient.Decrement(key, item.DefaultValue.Value, item.Delta.Value, item.ExpireAt.Value); } else { result = _memcachedClient.Decrement(key, item.DefaultValue.Value, item.Delta.Value); } return(Ok(result)); } else { CasResult <ulong> value = new CasResult <ulong>(); if (item.ValidFor.HasValue) { value = _memcachedClient.Decrement(key, item.DefaultValue.Value, item.Delta.Value, item.ValidFor.Value, item.Cas.Value); } else if (item.ExpireAt.HasValue) { value = _memcachedClient.Decrement(key, item.DefaultValue.Value, item.Delta.Value, item.ExpireAt.Value, item.Cas.Value); } else { value = _memcachedClient.Decrement(key, item.DefaultValue.Value, item.Delta.Value, item.Cas.Value); } var result = new { value = value.Result, cas = value.Cas, statusCode = value.StatusCode }; return(Ok(result)); } }
public void Append_Should_Be_Called() { var append = new ArraySegment <byte>(); var appendCas = new CasResult <ArraySegment <byte> > { Cas = 1, Result = append }; // Test : bool Append(string key, ArraySegment<byte> data); var clientMock = new Mock <IMemcachedClient>(); var service = new MemcachedServiceCache(clientMock.Object); service.Add(append, "name", CacheItemPolicy.Infinite, "value"); clientMock.Verify(x => x.Append("name", It.IsAny <ArraySegment <byte> >())); // Test : CasResult<bool> Append(string key, ulong cas, ArraySegment<byte> data); var clientMock2 = new Mock <IMemcachedClient>(); var service2 = new MemcachedServiceCache(clientMock2.Object); service2.Add(appendCas, "name", CacheItemPolicy.Infinite, "value"); clientMock2.Verify(x => x.Append("name", appendCas.Cas, It.IsAny <ArraySegment <byte> >())); }
public bool SetWithCas(string key, object value, int expiration, ulong cas, out ulong newCas) { newCas = 0; if (value == null || string.IsNullOrWhiteSpace(key)) { return(false); } try { //使用绝对过期时间 //CasResult<bool> result = clientInstance.Value.Cas(StoreMode.Set, ContactKey(key), value, DateTime.Now.AddSeconds(expiration), cas); //使用相对过期时间 CasResult <bool> result = clientInstance.Value.Cas(StoreMode.Set, ContactKey(key), value, new TimeSpan(0, 0, expiration), cas); newCas = result.Cas; return(result.Result); } catch (Exception ex) { LogHelper.LogException(logger, "EnyimMemcachedProvider.SetWithCas", ex, key, expiration, cas); return(false); } }
public bool TryGetWithCas(string key, out CasResult <object> value) { throw new NotImplementedException(); }
public bool TryGetWithCas <T1>(string key, out CasResult <T1> value) { return(_memcachedClient.TryGetWithCas(key, out value)); }
public void Increment_Should_Be_Called() { var validFor = new TimeSpan(0, 0, 1); var expiresAt = new DateTime(3000, 1, 1); var increment = new MemcachedServiceCache.IncrementTag { DefaultValue = 1, Delta = 2 }; var incrementCas = new CasResult <MemcachedServiceCache.IncrementTag> { Cas = 1, Result = increment }; // Test : ulong Increment(string key, ulong defaultValue, ulong delta); var clientMock = new Mock <IMemcachedClient>(); var service = new MemcachedServiceCache(clientMock.Object); service.Set(increment, "name", CacheItemPolicy.Infinite, "value"); clientMock.Verify(x => x.Increment("name", increment.DefaultValue, increment.Delta)); // Test : ulong Increment(string key, ulong defaultValue, ulong delta, DateTime expiresAt); var clientMock2 = new Mock <IMemcachedClient>(); var service2 = new MemcachedServiceCache(clientMock2.Object); service2.Set(increment, "name", new CacheItemPolicy { AbsoluteExpiration = expiresAt }, "value"); clientMock2.Verify(x => x.Increment("name", increment.DefaultValue, increment.Delta, expiresAt)); // Test : ulong Increment(string key, ulong defaultValue, ulong delta, TimeSpan validFor); var clientMock3 = new Mock <IMemcachedClient>(); var service3 = new MemcachedServiceCache(clientMock3.Object); service3.Set(increment, "name", new CacheItemPolicy { SlidingExpiration = validFor }, "value"); clientMock3.Verify(x => x.Increment("name", increment.DefaultValue, increment.Delta, validFor)); // Test : CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, ulong cas); var clientMock4 = new Mock <IMemcachedClient>(); var service4 = new MemcachedServiceCache(clientMock4.Object); service4.Set(incrementCas, "name", CacheItemPolicy.Infinite, "value"); clientMock4.Verify(x => x.Increment("name", increment.DefaultValue, increment.Delta, incrementCas.Cas)); // Test : CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas); var clientMock5 = new Mock <IMemcachedClient>(); var service5 = new MemcachedServiceCache(clientMock5.Object); service5.Set(incrementCas, "name", new CacheItemPolicy { AbsoluteExpiration = expiresAt }, "value"); clientMock5.Verify(x => x.Increment("name", increment.DefaultValue, increment.Delta, expiresAt, incrementCas.Cas)); // Test : CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas); var clientMock6 = new Mock <IMemcachedClient>(); var service6 = new MemcachedServiceCache(clientMock6.Object); service6.Set(incrementCas, "name", new CacheItemPolicy { SlidingExpiration = validFor }, "value"); clientMock6.Verify(x => x.Increment("name", increment.DefaultValue, increment.Delta, validFor, incrementCas.Cas)); }
public static SessionStateItem Load(IMemcachedClient client, string id, bool metaOnly) { CasResult<byte[]> header = new CasResult<byte[]>(); header = client.GetWithCas<byte[]>(HeaderPrefix + id); if (header.Result == null) return null; SessionStateItem entry; using (var ms = new MemoryStream(header.Result)) entry = SessionStateItem.LoadItem(ms); if (entry != null) entry.HeadCas = header.Cas; if (metaOnly) return entry; CasResult<byte[]> data = new CasResult<byte[]>(); data = client.GetWithCas<byte[]>(DataPrefix + id); if (data.Result == null) return null; using (var ms = new MemoryStream(data.Result)) using (var br = new BinaryReader(ms)) entry.Data = SessionStateItemCollection.Deserialize(br); entry.DataCas = data.Cas; return entry; }
public bool TryGetWithCas <T>(string key, out CasResult <T> value) { value = new CasResult <T>(); return(false); }
/// <summary> /// Tries the get with cas. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns></returns> public bool TryGetWithCas(string key, out CasResult <object> value) { return(Cache.TryGetWithCas(key, out value)); }