public void CacheDictionary_Null() { var cd = new CacheDictionary<string, int>(EqualityComparer<string>.Default); Assert.AreEqual(0, cd.Count); var values = cd.Values; Assert.AreEqual(4, cd.GetOrAdd(key: null, x => (x ?? "null").Length)); Assert.AreEqual(1, cd.Count); Assert.AreEqual(1, values.Count()); Assert.IsTrue(values.Contains(4)); Assert.AreEqual(4, cd.GetOrAdd(key: null, x => (x ?? "null").Length)); Assert.AreEqual(1, cd.Count); Assert.AreEqual(1, values.Count()); Assert.IsTrue(values.Contains(4)); Assert.IsTrue(cd.Remove(key: null)); Assert.AreEqual(0, cd.Count); Assert.AreEqual(0, values.Count()); cd.Clear(); Assert.AreEqual(0, cd.Count); Assert.AreEqual(0, values.Count()); }
public static Vessel GetVesselById(Guid id, bool useCache = true) { // Fast resolve if it's the active vessel, which is probably the most likely case. if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == id) { return(FlightGlobals.ActiveVessel); } // Check cache. if (useCache) { var item = vesselCache.GetValueOrDefault(id, null); if (item != null) { if (item.IsAlive) { return((Vessel)item.Target); } vesselCache.Remove(id); } } Vessel vessel = FlightGlobals.Vessels.Find(v => v.id == id); if (vessel != null && useCache) { vesselCache[id] = new WeakReference(vessel); } return(vessel); }
private void LimitQueueToCacheCapacity() { while (Capacity < QueueForLRU.Capacity) { DoublyLinkedListNode <KeyValuePair <int, int> > dequeueNode = QueueForLRU.Dequeue(); CacheDictionary.Remove(dequeueNode.Data.Key); } }
public void CacheDictionary_Simple() { var cd = new CacheDictionary<int, int>(EqualityComparer<int>.Default); Assert.AreEqual(0, cd.Count); var values = cd.Values; Assert.AreEqual(42, cd.GetOrAdd(21, x => x * 2)); Assert.AreEqual(1, cd.Count); Assert.AreEqual(1, values.Count()); Assert.IsTrue(values.Contains(42)); Assert.AreEqual(42, cd.GetOrAdd(21, x => x * 2)); Assert.AreEqual(1, cd.Count); Assert.AreEqual(1, values.Count()); Assert.IsTrue(values.Contains(42)); Assert.AreEqual(44, cd.GetOrAdd(22, x => x * 2)); Assert.AreEqual(2, cd.Count); Assert.AreEqual(2, values.Count()); Assert.IsTrue(values.Contains(42) && values.Contains(44)); Assert.IsTrue(cd.Remove(21)); Assert.AreEqual(1, cd.Count); Assert.AreEqual(1, values.Count()); Assert.IsTrue(values.Contains(44)); Assert.IsFalse(cd.Remove(21)); Assert.AreEqual(1, cd.Count); Assert.AreEqual(1, values.Count()); Assert.IsTrue(values.Contains(44)); Assert.IsFalse(cd.Remove(-1)); Assert.AreEqual(1, cd.Count); Assert.AreEqual(1, values.Count()); Assert.IsTrue(values.Contains(44)); cd.Clear(); Assert.AreEqual(0, cd.Count); Assert.AreEqual(0, values.Count()); }
private static void DoCacheDispose(string key, object value, CacheRemovedReason reason) { if (reason == CacheRemovedReason.Expired) { _contextSet.Foreach((ckey, context) => { if (context != null && MathUtils.DiffDate(context.ExpireDate).TotalSeconds > 3600) { _contextSet.Remove(ckey); } return(true); }); } }
public void Delete <T>(T entity) where T : class, IReportingEntity { lock (lockr) { var type = typeof(T); if (_cache.ContainsKey(type)) { _cache.Remove(type); } else { throw new Exception(string.Format("Missing type for Deletion? {0}", type)); } } }
public static RewardChessField GetRewardChessField(int userId, ChessFieldColor color, Random r) { RewardChessField field = new RewardChessField(); field.Color = color; field.IsOpened = false; field.IsFree = false; field.ParentId = -1; field.RewardCoin = 0; field.RewardMoney = 0; if (color != ChessFieldColor.Empty) { PlayerLogic p = new PlayerLogic(); p.SetUser(userId); double baseRewardCoin = GetBaseRewardCoin(p.MyPlayer.Level); field.RewardCoin = r.Next((int)Math.Round(baseRewardCoin * 0.5), (int)Math.Round(baseRewardCoin * 1.5)); } if (color == ChessFieldColor.RewardGray) { RandomDropLogic rd = RandomDropLogic.GetInstance(); var dropDict = new CacheDictionary <int, int>(); if (r.Next(100) < GameConsts.PlayerChess.TopRewardRate) { var dropData = CacheSet.DropTable.GetData(GameConsts.PlayerChess.TopRewardDropId); rd.GetDropDict(dropData, dropDict); } else { var dropData = CacheSet.DropTable.GetData(GameConsts.PlayerChess.MidRewardDropId); rd.GetDropDict(dropData, dropDict); } if (dropDict.ContainsKey((int)GiftItemType.Money)) { field.RewardMoney += dropDict[(int)GiftItemType.Money]; dropDict.Remove((int)GiftItemType.Money); } foreach (var kv in dropDict) { field.RewardItems.Add(kv); } } return(field); }
public void AddHandler(RoutedEvent routedEvent, Delegate handler, bool handledEventsToo = false) { routedEventHandlers.Add(routedEvent, new RoutedEventHandlerItem(handler, handledEventsToo)); routedEventHandlersCache.Remove(routedEvent); }
public virtual void RemovePendingServiceCallback(uint pCallbackId) { _usedServiceCallbackIds.Free(pCallbackId); _pendingServiceCallbacks.Remove(pCallbackId); }
public virtual void RemoveCacheLookup(string pSceneName) { _scenesCached.Remove(pSceneName); }