public bool TryLookupParsedRequest(RequestContext context) { if (!Enabled) { return(false); } var query = context.RawRequest.Query; if (_cache.TryLookup(query, out var item)) { Interlocked.Increment(ref item.UseCount); context.ParsedRequest = item.ParsedRequest; context.Metrics.FromCache = true; return(true); } return(false); }
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(); }