public void Add_ItemIsAdded_CurrentSizeIncreasesByItemSize() { var lru = new LruCache<string, string>(1024, 2); Assert.AreEqual(0, lru.CurrentSize); lru.Add("k1", "v1"); Assert.AreEqual(2, lru.CurrentSize); lru.Add("k2", "v2"); Assert.AreEqual(4, lru.CurrentSize); lru.Add("k3", "v3"); Assert.AreEqual(6, lru.CurrentSize); lru.Add("k4", "v4"); Assert.AreEqual(8, lru.CurrentSize); lru.Add("k5", "v5"); }
public void Add_DuplicateKeyAdded_ThrowsArgumentException() { try { var lru = new LruCache<string, string>(5); lru.Add("k1", "v1"); lru.Add("k1", "v1"); Assert.Fail("Should not have reached this point"); } catch (ArgumentException) { Assert.Pass(); } }
public void Add_ItemIsAdded_ItemExistsInCache() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); Assert.IsTrue(lru.ContainsKey("k1")); }
private static LruCache<string, string> GetInitializedCache(int max, params string[] entries) { var cache = new LruCache<string, string>(max); foreach (var entry in entries) { cache.Add(entry, entry); } return cache; }
public override IEnumerator Init() { App.On(ApplicationEvents.OnStartComplete, (sender, e) => { ILruCache <string, string> cache = new LruCache <string, string>(3); cache.Add("key_1", "val_1"); cache.Add("key_2", "val_2"); cache.Add("key_3", "val_3"); foreach (var kv in cache) { Debug.Log(kv.Key + " , " + kv.Value); } var key = cache["key_2"]; Debug.Log("key is :" + key); Debug.Log("*******************"); foreach (var kv in cache) { Debug.Log(kv.Key + " , " + kv.Value); } cache.Add("key_4", "key_4"); Debug.Log("*******************"); foreach (var kv in cache) { Debug.Log(kv.Key + " , " + kv.Value); } }); yield return(base.Init()); }
public void LruUsageTest() { const int maxSize = 10; var ttl = new TimeSpan(0, 1, 0, 0); var maxAge = new TimeSpan(0, 1, 0, 0); var target = new LruCache <string, string>(maxSize, ttl, maxAge); // Fill the LRU with "1" through "10" for (var i = 1; i <= maxSize; i++) { var s = i.ToString(); target.Add(s, "item " + s); Thread.Sleep(10); } // Use "10", then "9", etc. for (var i = maxSize; i >= 1; i--) { var s = i.ToString(); target.TryGetValue(s, out var value); } // Add a new item to push the least recently used out -- which should be item "10" var s1 = (maxSize + 1).ToString(); target.Add(s1, "item " + s1); Assert.Equal(maxSize, target.Count); // "Cache has exceeded maximum size" var s0 = maxSize.ToString(); Assert.False(target.ContainsKey(s0), "Least recently used item was not expelled"); for (var i = 1; i < maxSize; i++) { var s = i.ToString(); Assert.True(target.ContainsKey(s), "Recently used item " + s + " was incorrectly expelled"); } }
public static TableSheets FromTableSheetsState(TableSheetsState tableSheetsState) { if (Cache.TryGetValue(tableSheetsState, out var cached)) { return(cached); } var tableSheets = new TableSheets(); tableSheets.InitializeWithTableSheetsState(tableSheetsState); Cache.Add(tableSheetsState, tableSheets); return(tableSheets); }
public void TestGet() { var cache = new LruCache <string, string>(5); cache.Add("10", "5"); string val; Assert.AreEqual(true, cache.Get("10", out val, "100")); Assert.AreEqual("5", val); Assert.AreEqual(false, cache.Get("11", out val, "100")); Assert.AreEqual("100", val); }
public void AddRemoveTest() { var cache = new LruCache <string, TestClass, TestClass>( 100, value => value.Text); TestClass item = new TestClass("1"); cache.Add(item); Assert.AreEqual(1, cache.Count); item = new TestClass("2"); cache.Add(item); Assert.AreEqual(2, cache.Count); Assert.AreEqual(item, cache[item.Text, false]); ICache <string, TestClass> icache = cache; Assert.AreEqual(item, icache[item.Text, false]); Assert.AreEqual(null, icache["3", false]); cache.Remove(item); Assert.AreEqual(1, cache.Count); cache.Clear(); Assert.AreEqual(0, cache.Count); }
public double EstimateArtaCorrelation(double arAutocorrelation) { var result = estimationsCache.Get(arAutocorrelation); if (result == 0) { var e = Integrate(-8, 8, arAutocorrelation); var mean = distribution.GetMean(); var variance = distribution.GetVariance(); result = (e - mean * mean) / variance; estimationsCache.Add(arAutocorrelation, result); } return(result); }
public void CacheAddKeyValuePair() { var cache = new LruCache <int, int>(10); for (int index = 0; index < 10; ++index) { cache.Add(new KeyValuePair <int, int>(index, index)); int test = cache.Get(index); Assert.AreEqual(index, test, "Item not correctly added"); } bool thrown = false; try { cache.Add(new KeyValuePair <int, int>(0, 0)); } catch (ArgumentException) { thrown = true; } Assert.IsTrue(thrown, "Item should not have been added"); }
public void MoveArgs4ToLeastRecentlyUsedAndSeeEjectedDescending() { var lruCache = new LruCache(); var args1 = new object[] { 1, 1 }; var args2 = new object[] { 1, 2 }; var args3 = new object[] { 1, 3 }; var args4 = new object[] { 1, 4 }; var args5 = new object[] { 1, 5 }; lruCache.Add(args1, 1 + 1); lruCache.Add(args2, 1 + 2); lruCache.Add(args3, 1 + 3); lruCache.Add(args4, 1 + 4); _ = lruCache.Get(args3); _ = lruCache.Get(args2); _ = lruCache.Get(args1); lruCache.Add(args5, 1 + 5); Assert.Null(lruCache.Get(args4)); }
public void RandomScenario() { Stopwatch s = new Stopwatch(); s.Start(); cache = new LruCache <int, int>(100, ReadItemFunc, WriteItemAction); Random rnd = new Random(1); const int TotalItemsCount = 10000; for (int i = 0; i < TotalItemsCount; i++) { cache.Add(i, 123); } for (int i = 0; i < 100000; i++) { cache.Get(rnd.Next(TotalItemsCount)); cache.Add(rnd.Next(TotalItemsCount), 125); } cache.Flush(); }
private Source LoadSource(string id, string language) { Source result = Cache.Get(id); if (result == null) { byte[] content = Comm.DownloadFile(id); if (content != null) { result = new Source(content, language); Cache.Add(id, result); } } return(result); }
public Course GetVersion(Guid versionId) { if (versionsCache.TryGet(versionId, out var version)) { return(version); } var versionFile = GetCourseVersionFile(versionId); version = LoadCourseFromZip(versionFile); /* Add version to cache for fast loading next time */ versionsCache.Add(versionId, version); return(version); }
/// <summary> /// 初始化服务提供者 /// </summary> public void Init() { App.On(ApplicationEvents.OnStartCompleted, (payload) => { var cache = new LruCache <string, string>(3); cache.Add("key_1", "val_1"); cache.Add("key_2", "val_2"); cache.Add("key_3", "val_3"); foreach (var kv in cache) { Debug.Log(kv.Key + " , " + kv.Value); } var key = cache["key_2"]; Debug.Log("key is :" + key); Debug.Log("*******************"); foreach (var kv in cache) { Debug.Log(kv.Key + " , " + kv.Value); } cache.Add("key_4", "key_4"); Debug.Log("*******************"); foreach (var kv in cache) { Debug.Log(kv.Key + " , " + kv.Value); } }); }
public void TestRemoveEvent() { var cache = new LruCache <string, string>(5); for (var i = 0; i < 5; i++) { cache.Add(i.ToString(), i.ToString()); } var callNum = 0; cache.OnRemoveLeastUsed += (key, val) => { if (callNum++ <= 0) { cache.Get(key); } }; cache.Add("10", "10"); Assert.AreEqual(default(string), cache.Get("1")); Assert.AreEqual(5, cache.Count); Assert.AreEqual(1, callNum); }
public void SameHashDoesNotAffectCacheEjection() { const int MaxSize = 4; var lruCache = new LruCache(); var args = new object[] { 1, 1 }; bool firstAdd = lruCache.Add(args, 1 + 1); Assert.True(firstAdd); for (int i = 0; i < MaxSize + 1; i++) { bool subsequentAdd = lruCache.Add(args, 1 + 1); Assert.False(subsequentAdd); } var entry = lruCache.Get(args); Assert.NotNull(entry); Assert.Equal(2, entry.Value); }
public void AddTest() { var cache = new LruCache <string, string>(5); for (var i = 0; i < 5; i++) { cache.Add(i.ToString(), i.ToString()); } var n = 5; foreach (var v in cache) { Assert.AreEqual((--n).ToString(), v.Value); } }
public void LruCache_Clear_EmptiesCollection() { const int expectedCount = 50; var cache = new LruCache <string, string>(100); foreach (var value in Enumerable.Range(0, expectedCount).Select(v => v.ToString())) { cache.Add(value, value); } Assert.Equal(expectedCount, cache.Count); cache.Clear(); Assert.Equal(0, cache.Count); }
public static void SetupIdentity(this ControllerBase controller, LruCache <string, TokenAndSecret> cache = null, string osmUserId = "42") { var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, osmUserId) })); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = user } }; if (cache != null) { cache.Add(osmUserId, new TokenAndSecret("", "")); } }
public void ReverseForeachTest() { var cache = new LruCache <string, string>(5); for (var i = 0; i < 5; i++) { cache.Add(i.ToString(), i.ToString()); } cache.ReverseForeach(); var n = 0; foreach (var v in cache) { Assert.AreEqual(n.ToString(), v.Value); n++; } }
public void Add_MaxSizeIsReached_OldestItemDiscarded() { var lru = new LruCache<string, string>(5); lru.Add("k1", "v1"); lru.Add("k2", "v2"); lru.Add("k3", "v3"); lru.Add("k4", "v4"); lru.Add("k5", "v5"); lru.Add("k6", "v6"); Assert.IsFalse(lru.ContainsKey("k1")); }
public void Put(K key, V value) { V existingValue; bool replaced = false; using (new CriticalRegion(Lock, CriticalRegion.LockType.Write)) { CheckTimer(key, CacheAction.Write, true); if (IsCapped) { Node <K, V> valueWrapper; bool existed = false; replaced = LruCache.TryGetValue(key, out valueWrapper) && !(existed = Objects.Equals(valueWrapper.Data, value)); if (replaced) { existingValue = valueWrapper.Data; valueWrapper.Data = value; AddNode(valueWrapper); } else { existingValue = default(V); valueWrapper = new Node <K, V> { Key = key, Data = value }; if (!existed) { LruCache.Add(key, valueWrapper); } AddNode(valueWrapper); LruEvict(); } } else { replaced = Cache.TryGetValue(key, out existingValue) && !Objects.Equals(existingValue, value); Cache[key] = value; } } if (replaced) { InternalNotifyOfRemoval(ref key, ref existingValue, RemovalCause.Replaced); } }
public void CacheCopyToWithIndex() { var cache = new LruCache <int, int>(10); for (int index = 0; index < 10; ++index) { cache.Add(new KeyValuePair <int, int>(index, index)); } KeyValuePair <int, int>[] array = new KeyValuePair <int, int> [20]; cache.CopyTo(array, 10); for (int index = 0; index < cache.Count; ++index) { Assert.AreEqual(9 - index, array[index + 10].Key, "Keys don't match"); Assert.AreEqual(9 - index, array[index + 10].Value, "Values don't match"); } }
public void GetCachedColumnIndexes(TypeInfo type, IEnumerable <PrefetchFieldDescriptor> descriptors, out SortedDictionary <int, ColumnInfo> columns, out List <int> columnsToBeLoaded) { var cacheKey = new RootContainerCacheKey(type, descriptors); var cacheEntry = columnsCache[cacheKey, true]; if (cacheEntry == null) { columns = PrefetchHelper.GetColumns(ExtractColumns(descriptors), type); columnsToBeLoaded = PrefetchHelper.GetColumnsToBeLoaded(columns, type); cacheEntry = new RootContainerCacheEntry(cacheKey, columns, columnsToBeLoaded); columnsCache.Add(cacheEntry); return; } columns = cacheEntry.Columns; columnsToBeLoaded = cacheEntry.ColumnsToBeLoaded; }
public void GetTest() { var cache = new LruCache <string, string>(5); for (var i = 0; i < 5; i++) { cache.Add(i.ToString(), i.ToString()); } var result = cache["0"]; result = cache["1"]; foreach (var v in new [] { "1", "0", "4", "3", "2" }) { Assert.AreEqual(v, cache[v]); } }
public void EnumeratorChangedRefreshNoMove() { var cache = new LruCache <int, int>(10); for (int index = 0; index < 10; ++index) { cache.Add(index, index); } var enumerator = cache.GetEnumerator(); bool success = enumerator.MoveNext(); Assert.IsTrue(success, "Enumerator should move"); cache.Refresh(9); success = enumerator.MoveNext(); Assert.IsTrue(success, "Enumerator should move"); }
public void EnumeratorChangedClear() { var cache = new LruCache <int, int>(10); for (int index = 0; index < 10; ++index) { cache.Add(index, index); } var enumerator = cache.GetEnumerator(); bool success = enumerator.MoveNext(); Assert.IsTrue(success, "Enumerator should move"); cache.Clear(); CheckForInvalidEnumerator(enumerator); }
public void Set(IEventSourcedAggregateRoot aggregateRoot) { if (aggregateRoot == null) { return; } var aggregateRootId = aggregateRoot.Id; var aggregateRootType = aggregateRoot.GetType(); var cacheKey = GetCacheKey(aggregateRootId, aggregateRootType); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"Setting aggregate root to memory cache[LRU], Id: {aggregateRootId}, Type: {aggregateRootType}."); } _cache.Add(cacheKey, aggregateRoot); }
public void Add_MaxSizeIsReached_DiscardedCountIncreases() { var lru = new LruCache<string, string>(5); lru.Add("k1", "v1"); lru.Add("k2", "v2"); lru.Add("k3", "v3"); lru.Add("k4", "v4"); lru.Add("k5", "v5"); Assert.AreEqual(0, lru.DiscardedCount); lru.Add("k6", "v6"); Assert.AreEqual(1, lru.DiscardedCount); lru.Add("k7", "v7"); Assert.AreEqual(2, lru.DiscardedCount); }
// // Public Methods // public void Add(T obj) { #if SAFETY_CHECK if (obj == null) { Debug.LogError("Trying to add a null object to the cache!"); return; } #endif // old: the object that was just kicked out of the cache T old = cache.Add(obj); // Check if the cache kicked out an old object if (old != null && old != obj) { // The cache was full and kicked the oldest value out. OnPushedOutFromCache(old); } }
public void CacheValues() { var cache = new LruCache <int, int>(10); for (int index = 0; index < 10; ++index) { cache.Add(new KeyValuePair <int, int>(index, index)); } bool[] found = new bool[10]; foreach (int key in cache.Values) { found[key] = true; } for (int index = 0; index < found.Length; ++index) { Assert.IsTrue(found[index], $"Index {index} wasn't found"); } }
public void RemoveWithHeaderAndTail() { var cache = new LruCache <string, string>(5); for (var i = 0; i < 5; i++) { cache.Add(i.ToString(), i.ToString()); } foreach (var v in new[] { "0", "2", "4" }) { cache.Remove(v); } foreach (var v in new[] { "3", "1" }) { Assert.AreEqual(v, cache[v]); } Assert.AreEqual(2, cache.Count); }
public void RemoveTest() { var cache = new LruCache <string, string>(5); for (var i = 0; i < 5; i++) { cache.Add(i.ToString(), i.ToString()); } foreach (var v in new[] { "2", "1", "3" }) { cache.Remove(v); } foreach (var v in new[] { "4", "0" }) { Assert.AreEqual(v, cache[v]); } Assert.AreEqual(2, cache.Count); }
public PE GetBinary(string PePath) { if (!File.Exists(PePath)) { return(null); } string PeHash = GetBinaryHash(PePath); // A sync lock is mandatory here in order not to load twice the // same binary from two differents workers lock (BinaryDatabaseLock) { bool hit = BinaryDatabase.ContainsKey(PeHash); // Cache "miss" if (!hit) { string DestFilePath = Path.Combine(BinaryCacheFolderPath, PeHash); if (!File.Exists(DestFilePath) && (DestFilePath != PePath)) { File.Copy(PePath, DestFilePath, true); } PE NewShadowBinary = new PE(DestFilePath); NewShadowBinary.Load(); LruCache.Add(PeHash); BinaryDatabase.Add(PeHash, NewShadowBinary); } } // Cache "Hit" UpdateLru(PeHash); PE ShadowBinary = BinaryDatabase[PeHash]; ShadowBinary.Filepath = PePath; return(ShadowBinary); }
public void Clear_CacheIsNotEmpty_CurrentSizeIsZero() { var lru = new LruCache<string, string>(5); lru.Add("k1", "v1"); lru.Add("k2", "v2"); lru.Add("k3", "v3"); lru.Add("k4", "v4"); lru.Add("k5", "v5"); Assert.AreEqual(5, lru.CurrentSize); lru.Clear(); Assert.AreEqual(0, lru.CurrentSize); }
public void IndexerSet_KeyExists_NewValueIsSet() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); Assert.AreEqual("v1", lru["k1"]); lru["k1"] = "NewValue"; Assert.AreEqual("NewValue", lru["k1"]); }
public void Remove_ItemIsRemoved_ContainsKeyReturnsFalse() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); lru.Remove("k1"); Assert.IsFalse(lru.ContainsKey("k1")); }
public void Contains_EntryExists_ReturnsTrue() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); var entry = new KeyValuePair<string, string>("k1", "v1"); Assert.IsTrue(lru.Contains(entry)); }
public void TryGetValue_KeyExists_ValuePopulated() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); string value; lru.TryGetValue("k1", out value); Assert.AreEqual("v1", value); }
public void TryGetValue_KeyExists_HitCountIncreased() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); Assert.AreEqual(0, lru.HitCount); string value; lru.TryGetValue("k1", out value); Assert.AreEqual(1, lru.HitCount); }
public void IndexerGet_KeyExists_ReturnsValue() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); var value = lru["k1"]; Assert.AreEqual("v1", value); }
public void Remove_ItemIsRemoved_CurrentSizeDecreasesByItemSize() { var lru = new LruCache<string, string>(5); lru.Add("k1", "v1"); lru.Add("k2", "v2"); lru.Add("k3", "v3"); lru.Add("k4", "v4"); lru.Add("k5", "v5"); Assert.AreEqual(5, lru.CurrentSize); lru.Remove("k1"); Assert.AreEqual(4, lru.CurrentSize); lru.Remove("k2"); Assert.AreEqual(3, lru.CurrentSize); lru.Remove("k3"); Assert.AreEqual(2, lru.CurrentSize); lru.Remove("k4"); Assert.AreEqual(1, lru.CurrentSize); lru.Remove("k5"); Assert.AreEqual(0, lru.CurrentSize); }
public void IndexerGet_KeyExists_MissCountNotIncreased() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); Assert.AreEqual(0, lru.MissCount); var value = lru["k1"]; Assert.AreEqual(0, lru.MissCount); }
public void Resize_NewSizeCausesTrim_OnlyOldestItemsDiscarded() { var lru = new LruCache<string, string>(5); lru.Add("k1", "v1"); lru.Add("k2", "v2"); lru.Add("k3", "v3"); lru.Add("k4", "v4"); lru.Add("k5", "v5"); lru.Resize(3); Assert.IsFalse(lru.ContainsKey("k1")); Assert.IsFalse(lru.ContainsKey("k2")); Assert.IsTrue(lru.ContainsKey("k3")); Assert.IsTrue(lru.ContainsKey("k4")); Assert.IsTrue(lru.ContainsKey("k5")); }
public void OldestItemValue_CacheNotEmpty_ReturnsOldestValue() { var lru = new LruCache<string, string>(2); lru.Add("k1", "v1"); lru.Add("k2", "v2"); Assert.AreEqual("v1", lru.OldestValue); }
public void NewestItemKey_CacheNotEmpty_ReturnsNewestKey() { var lru = new LruCache<string, string>(2); lru.Add("k1", "v1"); lru.Add("k2", "v2"); Assert.AreEqual("k2", lru.NewestKey); }
public void Add_SomeItemsAdded_FirstItemAddedIsOldest() { var lru = new LruCache<string, string>(1024); lru.Add("k1", "v1"); lru.Add("k2", "v2"); lru.Add("k3", "v3"); lru.Add("k4", "v4"); lru.Add("k5", "v5"); Assert.AreEqual("v1", lru.OldestValue); Assert.AreEqual("k1", lru.OldestKey); }
public void Remove_ItemIsRemoved_ReturnsTrue() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); Assert.IsTrue(lru.Remove("k1")); }
public void Remove_KeyDoesNotExist_ReturnsFalse() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); Assert.IsFalse(lru.Remove("k2")); }
public void ContainsKey_KeyExists_ReturnsTrue() { var lru = new LruCache<string, string>(1); lru.Add("k1", "v1"); Assert.IsTrue(lru.ContainsKey("k1")); }