private void HandleAddOrUpdateAndGet(CacheMessage message) { DoHandleAddUpdateAndGet(message, true, true,true); }
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)); } }
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); }
private void HandleAddItem(CacheMessage message, bool update = false) { DoHandleAddUpdateAndGet(message, false, false,true); }
private void HandleRemoveGroup(CacheMessage message) { if (_groups.ContainsKey(message.GroupId)) { CacheGroup cg; _groups.TryRemove(message.GroupId, out cg); } }
public void RemoveGroup(string groupId) { var cm = new CacheMessage(CacheAction.RemoveGroup, _messageTimeout) { GroupId = groupId }; _cacheMessages.Enqueue(cm); }
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); } }
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); }
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; } }
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); } }
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); }
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(); }
public void InvalidateItem(string id, string groupId = null) { var cm = new CacheMessage(CacheAction.InvalidateItem, _messageTimeout) { GroupId = groupId, ItemId = id }; _cacheMessages.Enqueue(cm); }
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); }
private void HandleInvalidateGroup(CacheMessage message) { throw new System.NotImplementedException(); }
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); } } } }
private void HandleUpdateGroupTimeout(CacheMessage message) { throw new NotImplementedException(); }
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); }