public ObjectType GetAndLock(long objectId, ref RedisErrorCodes errorCode) { string key = TypeName + ":" + objectId; ObjectType obj = Get(objectId); if (obj != null) { if (obj.Lock()) { return(PullFromServer(key)); } errorCode = RedisErrorCodes.SessionIsBusy; return(null); } else { errorCode = RedisErrorCodes.SessionIsNotFound; return(null); } }
public void TestMethod() { try { ContainerRedis.Instance.LoadAllObjects(); if (ContainerRedis.Instance.GetAllLocalObjects().Count > 0) { foreach (var item in ContainerRedis.Instance.GetAllLocalObjects()) { ContainerRedis.Instance.Remove(item.Key); } } var coll = ContainerRedis.Instance.GetAllLocalObjects(); Assert.IsTrue(coll.Count == 0); var obj = new LockableMessage() { Content = "Hello world", Id = 12 }; ContainerRedis.Instance.Add(obj); coll = ContainerRedis.Instance.GetAllLocalObjects(); Assert.IsTrue(coll.Count == 1); RedisErrorCodes error = RedisErrorCodes.None; var lockableObj = ContainerRedis.Instance.GetAndLock(obj.Id, ref error); if (error == RedisErrorCodes.None) { obj.Content = "Changed"; obj.Unlock(); } ContainerRedis.Instance.Remove(obj.Id); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }