public void Test_Utils_DoubleBufferCache() { TestEnv.LogTestMethodStart(); var cache = new DoubleBufferCache <string, string>(capacity: 20, evictionTimeSec: 10); // fill out 10 items for (int i = 0; i < 10; i++) { cache.Add($"Key-{i}", $"Value-{i}"); } // read 20 items, 10 must be in cache var hits = 0; var misses = 0; for (int i = 0; i < 20; i++) { if (cache.TryLookup($"Key-{i}", out _)) { hits++; } else { misses++; } } // check that we have 10 hits and 10 misses Assert.AreEqual(10, hits, "Invalid hit count"); Assert.AreEqual(10, misses, "Invalid miss count"); // move time forward, and add item that is already there. this will force swapping buffers // and finalizing metrics record AppTime.SetOffset(TimeSpan.FromSeconds(30)); cache.Add("Key-0", "Value-0"); // check metrics var metrics = cache.GetMetrics(); Assert.AreEqual(10, metrics.ItemCount); Assert.AreEqual(20, metrics.ReadCount); Assert.AreEqual(10, metrics.MissCount); AppTime.ClearOffset(); }
public void AddParsedRequest(RequestContext context) { if (!Enabled) { return; } if (context.Metrics.FromCache) { return; //already cached } var reqText = context.RawRequest.Query; var item = new RequestCacheItem() { CreatedOn = AppTime.UtcNow, Key = reqText, ParsedRequest = context.ParsedRequest }; _cache.Add(reqText, item); }