public void Add_CacheCapacityReached_MRUKVPRemovedAndNewKVPAdded() { _cache = new CacheDictionary<int, int>(5,CachePurgeStatergy.MRU); FillCache(); _cache.Add(5, 5); Assert.IsFalse(_cache.ContainsKey(4)); Assert.IsTrue(_cache.ContainsKey(5)); Assert.AreEqual(_cache.CacheCapacity, _cache.Count); }
public int?Get(int key) { int?retValue = null; DoublyLinkedListNode <KeyValuePair <int, int> > currentNode = (CacheDictionary.ContainsKey(key))?CacheDictionary[key]:null; // Get the return value from the CacheDictionary retValue = (currentNode != null) ? (int?)currentNode.Data.Value : null; // Move enqueue the value to the queue (and delete it if it is present in the queue) if (currentNode != null) { // delete the node from the queue QueueForLRU.Delete(currentNode); // Put the node in the Last of Queue QueueForLRU.Enqueue(currentNode); LimitQueueToCacheCapacity(); } else // the page requested is not present in the cache { // We need to add the page from memory to the cache retValue = MimicGettingPageFromMemory(key); if (retValue != null) { Put(key, (int)retValue); } else { // the page number is not present in cache or memory return(null); } } return(retValue); }
public static GameContext GetInstance(int actionId, int userId, int timeOut = TimeOut) { string key = string.Format("{0}_{1}", actionId, userId); if (!_contextSet.ContainsKey(key)) { _contextSet.Add(key, new GameContext(actionId, userId, timeOut)); } var context = _contextSet[key]; if (context != null) { context.ExpireDate = MathUtils.Now; } return(context); }
public void GetDropDict(DTDrop dropData, CacheDictionary <int, int> dropDict) { for (int i = 0; i < dropData.RepeatCount; i++) { int randomValue = random.Next(0, GameConsts.DropItemTotalWeight); foreach (DropItem di in dropData.DropList) { randomValue -= di.ItemWeight; if (randomValue < 0) { int itemId, itemCount; if (di.ItemId < 0) { switch ((RandomDropSetType)di.ItemId) { case RandomDropSetType.RandomWhiteGear: itemId = GetOneSpecifiedQualityGear(GearQuality.White); break; case RandomDropSetType.RandomGreenGear: itemId = GetOneSpecifiedQualityGear(GearQuality.Green); break; case RandomDropSetType.RandomBlueGear: itemId = GetOneSpecifiedQualityGear(GearQuality.Blue); break; case RandomDropSetType.RandomPurpleGear: itemId = GetOneSpecifiedQualityGear(GearQuality.Purple); break; case RandomDropSetType.RandomOrangeGear: itemId = GetOneSpecifiedQualityGear(GearQuality.Orange); break; default: continue; } itemCount = di.ItemCount; } else { itemId = di.ItemId; itemCount = di.ItemCount; } if (dropDict.ContainsKey(itemId)) { dropDict[itemId] += itemCount; } else { dropDict[itemId] = itemCount; } break; } } } }
/// <summary> /// 获得当前请求上下文 /// </summary> /// <param name="sessionId"></param> /// <param name="actionId"></param> /// <param name="userId"></param> /// <param name="timeOut"></param> /// <returns></returns> public static GameContext GetInstance(string sessionId, int actionId, int userId, int timeOut = TimeOut) { if (string.IsNullOrEmpty(sessionId)) { sessionId = Guid.NewGuid().ToString("N"); } string key = CreateContextKey(sessionId, actionId); if (!_contextSet.ContainsKey(key)) { _contextSet.Add(key, new GameContext(sessionId, actionId, userId, timeOut)); } var context = _contextSet[key]; if (context != null) { context.UserId = userId; context.ExpireDate = MathUtils.Now; } return(context); }
public void Add <T>(T entity) where T : class, IReportingEntity { lock (lockr) { var type = typeof(T); var data = new CacheData(entity); if (_cache.ContainsKey(type)) { var newId = _cache[type].Max(x => x.Key) + 1; _cache[type].Add(newId, data); } else { _cache.Add(type, new Dictionary <int, CacheData>() { { 1, data } }); } } }
public static string GetString(string name, string defaultValue = "") { if (m_Configs.Count <= 0) { Reload(); } if (m_Configs.ContainsKey(name)) { return(m_Configs[name]); } else { return(defaultValue); } }
/// <summary> /// Put a page number(key) and the page content/or reference to content as value in the cache /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Put(int key, int value) { // Algo: if the key is present in dictionary, do nothing // if the key is not present in the dictionary, add to dictionary and also enqueue in queue // while enqueue, make sure that the capacity is not reached, else dequeue the LRU element if (!CacheDictionary.ContainsKey(key)) { DoublyLinkedListNode <KeyValuePair <int, int> > nodeToAdd = new DoublyLinkedListNode <KeyValuePair <int, int> >(new KeyValuePair <int, int>(key, value)); // Add to cacheDictionary CacheDictionary[key] = nodeToAdd; // Put the node in the Last of Queue QueueForLRU.Enqueue(nodeToAdd); LimitQueueToCacheCapacity(); } }
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 TryGetValue_MRUDicAndKeyPresent_KeyMarkedLastForPurge() { _cache = new CacheDictionary<int, int>(5, CachePurgeStatergy.MRU); FillCache(); int value; Assert.IsTrue(_cache.TryGetValue(0, out value)); //0 moved to top of MRU list Assert.AreEqual(0, value); _cache.Add(5, 5); Assert.IsTrue(_cache.ContainsKey(5)); //newly added key 5 is present Assert.IsFalse(_cache.ContainsKey(0)); //0 is not present }
public void IndexerSet_MRUDicAndKeyPresent_ValueUpdatedAndMarkedForPurge() { _cache = new CacheDictionary<int, int>(5, CachePurgeStatergy.MRU); FillCache(); _cache[0] = 100; Assert.AreEqual(100, _cache[0]); _cache.Add(5, 5); Assert.IsTrue(_cache.ContainsKey(5)); //newly added key 5 is present Assert.IsFalse(_cache.ContainsKey(0)); //0 is not present }
public void IndexerSet_MRUDicAndKeyNotPresent_KVPAddedAndMarkedForPurge() { _cache = new CacheDictionary<int, int>(5,CachePurgeStatergy.MRU); FillCache(); _cache[5] = 5; Assert.AreEqual(5, _cache[5]); Assert.IsTrue(_cache.ContainsKey(5)); //newly added key 5 is present Assert.IsFalse(_cache.ContainsKey(4)); //0 is not present _cache.Add(6,6); Assert.IsFalse(_cache.ContainsKey(5)); //key 5 is not present }
private bool RefreshData(PlayerChessLogic playerChess) { int costFieldCount = 0; int freeFieldCount = 0; int totalCostFreeCount = 0; Transaction t = new Transaction(); t.DumpEntity(playerChess.MyChess); PlayerPackageLogic package = new PlayerPackageLogic(); package.SetUser(m_UserId); foreach (var field in m_RequestPacket.ModifiedChessField) { var oldField = playerChess.MyChess.ChessBoard[field.Index]; int oldColor = oldField.Color == ChessFieldColor.Empty || oldField.Color == ChessFieldColor.EmptyGray || oldField.Color == ChessFieldColor.RewardGray ? (int)ChessFieldColor.EmptyGray : (int)oldField.Color; if (field.Color != oldColor) { t.RollBack(); ErrorCode = (int)ErrorType.CannotOpenChance; ErrorInfo = "illegal params"; return(false); } if (field.Color == (int)ChessFieldColor.EmptyGray) { RewardChessField oldRewardField = oldField as RewardChessField; if (!oldRewardField.IsOpened && field.IsOpened) { if (!oldRewardField.IsFree) { if (m_RequestPacket.ModifiedChessField.Count == 1) { costFieldCount += 1; } else { freeFieldCount += 1; } } else { freeFieldCount += 1; } m_GotCoin += oldRewardField.RewardCoin; m_GotMoney += oldRewardField.RewardMoney; m_GotStarEnergy += oldRewardField.RewardStarEnergy; foreach (var reward in oldRewardField.RewardItems) { if (m_GotItems.ContainsKey(reward.Key)) { m_GotItems[reward.Key] += reward.Value; } else { m_GotItems.Add(reward); } } PlayerPackageLogic pp = new PlayerPackageLogic(); pp.SetUser(m_UserId); if (!pp.CheckPackageSlot(m_GotItems)) { ErrorCode = (int)ErrorType.PackageSlotFull; ErrorInfo = "item count if full"; return(false); } playerChess.DeductOpenCount(); } oldRewardField.IsFree = field.IsFree; oldRewardField.IsOpened = field.IsOpened; oldRewardField.ParentId = field.Parent; } else { BattleChessField oldBattleField = oldField as BattleChessField; if (!oldBattleField.IsOpened && field.IsOpened) { m_ResponsePacket.FreeCount = oldBattleField.Count; costFieldCount += 1; oldBattleField.IsOpened = field.IsOpened; PlayerDailyQuestLogic.GetInstance(m_UserId).UpdateDailyQuest(DailyQuestType.WinTurnOverChessBattle, 1); } else { if (field.FreeCount < 0 || field.FreeCount > oldBattleField.Count) { ErrorCode = (int)ErrorType.CannotOpenChance; ErrorInfo = "illegal params"; return(false); } totalCostFreeCount += oldBattleField.Count - field.FreeCount; oldBattleField.Count = field.FreeCount; } oldBattleField.ChildrenId.AddRange(field.Children); } } if (freeFieldCount != totalCostFreeCount) { t.RollBack(); ErrorCode = (int)ErrorType.CannotOpenChance; ErrorInfo = "illegal params"; return(false); } if (costFieldCount > 1) { t.RollBack(); ErrorCode = (int)ErrorType.CannotOpenChance; ErrorInfo = "illegal params"; return(false); } playerChess.MyChess.Count -= costFieldCount; return(true); }