/* * PRIVATE METHODS */ // Randomize values for a new pair private void GenerateNewPair() { if (RandomPairsList == null) { RandomPairsList = new List <PairItem>(); } Random rand = new Random(DateTime.UtcNow.Millisecond); int second = rand.Next(MIN_TTL, MAX_TTL); int value = rand.Next(MIN_VALUE, MAX_VALUE); RandomPairsList.Add(new PairItem(second, value)); }
private void RemoveExceedPairs() { try { int exceedNumber = RandomPairsList.Count - MAX_PAIRS + 1; if (exceedNumber > 0) { RandomPairsList.RemoveRange(0, exceedNumber); } } catch (Exception ex) { // TODO: log error } }
private void RemoveExpiredPairs() { if (RandomPairsList == null || RandomPairsList.Count == 0) { return; } try { // reduce TTL count for all pairs, assuming this method is called once per second RandomPairsList.ForEach(p => p.TTL--); // then remove the expired ones RandomPairsList.RemoveAll(p => p.TTL <= 0); } catch (Exception ex) { // TODO: log error } }