예제 #1
0
		private void HandleAddOrUpdateAndGet(CacheMessage message)
		{
			DoHandleAddUpdateAndGet(message, true, true,true);
		}
예제 #2
0
		private void HandleAddGroup(CacheMessage message)
		{
			var msg = (CacheMessage)message;
			if (msg.GroupDefinition == null) return;
			if (!_groups.ContainsKey(msg.GroupDefinition.Id))
			{
				_groups.TryAdd(msg.GroupDefinition.Id, new CacheGroup(msg.GroupDefinition));
			}
		}
예제 #3
0
		public void SetGroupTimeout(TimeSpan timeout, string groupId = null)
		{
			if (!_groups.ContainsKey(groupId ?? string.Empty))
			{
				throw new Exception("Missing cache group id " + groupId ?? string.Empty);
			}
			var cm = new CacheMessage(CacheAction.UpdateGroupTimeout, _messageTimeout)
			{
				GroupId = groupId ?? string.Empty,
				GroupTimeout = timeout
			};

			_cacheMessages.Enqueue(cm);
		}
예제 #4
0
		private void HandleAddItem(CacheMessage message, bool update = false)
		{
			DoHandleAddUpdateAndGet(message, false, false,true);
		}
예제 #5
0
		private void HandleRemoveGroup(CacheMessage message)
		{
			if (_groups.ContainsKey(message.GroupId))
			{
				CacheGroup cg;
				_groups.TryRemove(message.GroupId, out cg);
			}
		}
예제 #6
0
		public void RemoveGroup(string groupId)
		{
			var cm = new CacheMessage(CacheAction.RemoveGroup, _messageTimeout)
			{
				GroupId = groupId
			};
			_cacheMessages.Enqueue(cm);
		}
예제 #7
0
		private void HandleInvalidateItem(CacheMessage message)
		{
			if (!_groups.ContainsKey(message.GroupId)) return;
			var group = _groups[message.GroupId];
			if (!group.Items.ContainsKey(message.ItemId)) return;
			var item = group.Items[message.ItemId];
			if (!item.IsWaiting)
			{
				group.Items.Remove(message.ItemId);
			}
		}
예제 #8
0
		public ICoroutineResult Get(string cacheId, Action<object> set, string groupId = null)
		{
			if (!_groups.ContainsKey(groupId ?? string.Empty))
			{
				throw new Exception("Missing cache group id " + groupId ?? string.Empty);
			}
			var cm = new CacheMessage(CacheAction.Get, _messageTimeout)
			{
				GroupId = groupId,
				ItemId = cacheId,
				CacheItem = new CacheItem
				{
					Id = cacheId
				}
			};

			_cacheMessages.Enqueue(cm);
			return BuildResult(cm, set);
		}
예제 #9
0
		private void HandleMessage(CacheMessage message)
		{
			message.GroupId = message.GroupId ?? string.Empty;
			switch (message.Action)
			{
				case (CacheAction.AddGroup):
					HandleAddGroup(message);
					break;
				case (CacheAction.InvalidateGroup):
					HandleInvalidateGroup(message);
					break;
				case (CacheAction.RemoveGroup):
					HandleRemoveGroup(message);
					break;
				case (CacheAction.AddItem):
					HandleAddItem(message);
					break;
				case (CacheAction.AddOrUpdateItem):
					HandleAddOrUpdateItem(message);
					break;
				case (CacheAction.InvalidateItem):
					HandleInvalidateItem(message);
					break;
				case (CacheAction.AddAndGet):
					HandleAddAndGet(message);
					break;
				case (CacheAction.AddOrUpdateAndGet):
					HandleAddOrUpdateAndGet(message);
					break;
				case (CacheAction.Get):
					HandleGet(message);
					break;
				case (CacheAction.UpdateGroupTimeout):
					HandleUpdateGroupTimeout(message);
					break;
				default:
					message.Value = null;
					message.IsCompleted = true;
					break;
			}
		}
예제 #10
0
		private IEnumerable<ICoroutineResult> WaitForResponse(CacheMessage cm)
		{
			while (!cm.IsCompleted.Value)
			{
				yield return CoroutineResult.Wait;
			}
			cm.IsCompleted = true;
			if (cm.HasValue)
			{
				yield return CoroutineResult.Return(cm.Value);
			}
			else
			{
				yield return CoroutineResult.Return(null);
			}
		}
예제 #11
0
		public ICoroutineResult AddOrUpdateAndGet(CacheDefinition cacheItem, Action<object> set, string groupId = null)
		{
			SetupCacheDefinitionExpireTime(cacheItem, groupId);
			var cm = new CacheMessage(CacheAction.AddOrUpdateAndGet, _messageTimeout)
			{
				GroupId = groupId,
				CacheItem = cacheItem,
				ItemId = cacheItem.Id
			};
			_cacheMessages.Enqueue(cm);
			return BuildResult(cm, set);
		}
예제 #12
0
		private ICoroutineResult BuildResult(CacheMessage cm, Action<object> set)
		{
			return CoroutineResult.RunAndGetResult(WaitForResponse(cm),
				string.Format("NodeCache::BuildResult('{0}',action)", cm.CacheItem.Id))
				.OnComplete((a) => set(a.Result))
				.WithTimeout(_messageTimeout)
				.OnError((a) =>
				{
					Log.Warning("Unable to load " + cm.CacheItem.Id);
					set(null);
					return true;
				}).AndWait();
		}
예제 #13
0
		public void InvalidateItem(string id, string groupId = null)
		{
			var cm = new CacheMessage(CacheAction.InvalidateItem, _messageTimeout)
			{
				GroupId = groupId,
				ItemId = id
			};
			_cacheMessages.Enqueue(cm);
		}
예제 #14
0
		public void AddOrUpdateItem(CacheDefinition cacheItem, string groupId = null)
		{
			SetupCacheDefinitionExpireTime(cacheItem, groupId);
			var cm = new CacheMessage(CacheAction.AddOrUpdateItem, _messageTimeout)
			{
				GroupId = groupId,
				CacheItem = cacheItem,
				ItemId = cacheItem.Id
			};
			_cacheMessages.Enqueue(cm);
		}
예제 #15
0
		private void HandleInvalidateGroup(CacheMessage message)
		{
			throw new System.NotImplementedException();
		}
예제 #16
0
		private void DoHandleAddUpdateAndGet(CacheMessage message, bool doGet, bool doUpdate, bool doAdd)
		{
			var expireAfter = message.CacheItem.ExpireAfter;
			if (!_groups.ContainsKey(message.GroupId)) return;
			var group = _groups[message.GroupId];
			if (!group.Items.ContainsKey(message.ItemId) || doUpdate)
			{
				if (!doAdd)
				{
					message.HasValue = false;
					message.IsCompleted = true;
					return;
				}
				var item = new CacheItem
				{
					ExpireAfter = expireAfter,
					IsWaiting = true
				};
				@group.Items[message.ItemId] = item;

				if (message.CacheItem.LoadData == null)
				{
					item.Value = message.CacheItem.Value;
					item.IsWaiting = false;
				}
				else
				{
					if (doGet)
					{
						_pending.Add(message);
					}
					var coroutine = CoroutineResult.RunAndGetResult(message.CacheItem.LoadData(),
						string.Format("NodeCache::HandleAddUpdateGet('{0}',{1},{2})",
						message.CacheItem.Id, doGet, doUpdate))
						.OnComplete<object>((r) =>
						{
							item.Value = r;
							item.IsWaiting = false;
						})
						.WithTimeout(_messageTimeout)
						.OnError((e) =>
						{
							item.IsWaiting = false;
							return true;
						}).AsCoroutine();
					_coroutinesManager.StartCoroutine(coroutine);
				}
			}
			else
			{
				var item = group.Items[message.ItemId];
				if (!item.IsWaiting)
				{

					if (group.RollingExpiration)
					{
						item.ExpireAfter = message.CacheItem.ExpireAfter;
					}
					message.Value = item.Value;
					message.HasValue = true;
					message.IsCompleted = true;
				}
				else
				{
					if (doGet)
					{
						message.CacheItem.ExpireAfter = expireAfter;
						_pending.Add(message);
					}
				}
			}
		}
예제 #17
0
		private void HandleUpdateGroupTimeout(CacheMessage message)
		{
			throw new NotImplementedException();
		}
예제 #18
0
		public void AddGroup(CacheGroupDefinition groupDefinition, CacheDefinition cacheItem = null)
		{
			if (_groups.ContainsKey(groupDefinition.Id))
			{
				throw new Exception("Duplicate cache group id " + groupDefinition.Id);
			}
			var cm = new CacheMessage(CacheAction.AddGroup, _messageTimeout)
			{
				GroupDefinition = groupDefinition,
				CacheItem = cacheItem
			};
			_cacheMessages.Enqueue(cm);
		}