public async Task <string> ParseAsync(string token) { CacheEntry entry = Access(token); if (entry != null) { hitCounterChannel.PublishAsync(true); } else { entry = new CacheEntry() { RawToken = token, ParsedToken = await Next.ParseAsync(token), Expiration = DateTime.Now + TimeSpan.FromMilliseconds(Parameters.CacheEntryTtlMs) }; lock (cacheListLock) { entry.InsertAfter(lruListRoot); cache[token] = entry; if (cache.Count > Parameters.CacheMaxEntries) { Scavenge(); } } hitCounterChannel.PublishAsync(false); } return(entry.ParsedToken); }
public async Task RunAsync() { Console.WriteLine("Request source starting up"); CreateTokens(); int hotIndex = 0; int maxRequests = (int)(Parameters.RunLengthMs * (Parameters.RequestsPerSecond / 1000.0)); for (int requestCount = 0; requestCount < maxRequests; ++requestCount) { int index; if (requestCount % Parameters.HotEntryInterval == 0) { index = hotIndex; hotIndex = (hotIndex + 1) % Parameters.NumHotEntries; } else { index = rng.Next(tokens.Length); } await requestChannel.PublishAsync(tokens[index]); // Delay generation a bit just to keep from blowing out memory in queues. //await RandomDelay(); } Console.WriteLine("Request source finished, closing channel and exiting"); requestChannel.Close(); }
public async Task <string> ParseAsync(string token) { string cacheHit; if (cache.TryGetValue(token, out cacheHit)) { hitCounterChannel.PublishAsync(true); return(cacheHit); } string result = await Next.ParseAsync(token); cache[token] = result; expirationList.Enqueue(Tuple.Create(DateTime.Now + cacheExpiration, token)); hitCounterChannel.PublishAsync(false); return(result); }
public async Task <string> ParseAsync(string token) { Tuple <string, DateTime> cacheEntry; if (cache.TryGetValue(token, out cacheEntry)) { if (cacheEntry.Item2 > DateTime.Now) { Scavenge(); hitCounterChannel.PublishAsync(true); return(cacheEntry.Item1); } } Scavenge(); string result = await Next.ParseAsync(token); var newValue = Tuple.Create(result, DateTime.Now + TimeSpan.FromMilliseconds(Parameters.CacheEntryTtlMs)); cache.AddOrUpdate(token, newValue, (k, v) => newValue); hitCounterChannel.PublishAsync(false); return(result); }