using System.Collections.Generic;
using System.Diagnostics;
private static LocalCache localCache = new LocalCache();
public static void Main()
var stopwatch = Stopwatch.StartNew();
var records1 = GenerateRecords(3000);
Console.WriteLine($"First call without cache Time: {stopwatch.ElapsedMilliseconds} ms");
var records2 = GenerateRecords(3000);
Console.WriteLine($"Second call with cache Time: {stopwatch.ElapsedMilliseconds} ms");
var records3 = GenerateRecords(3001);
Console.WriteLine($"Third call with cache Time: {stopwatch.ElapsedMilliseconds} ms");
public static List<KeyValuePair<string, string>> GenerateRecords(int count)
var cacheKey = $"GenerateRecords-{count}";
var cachedValue = localCache.Get(cacheKey);
return (List<KeyValuePair<string, string>>)cachedValue;
var records = new List<KeyValuePair<string, string>>();
for (int i = 0; i < count; i++)
records.Add(new KeyValuePair<string, string>("key" + i, "value" + i));
localCache.Set(cacheKey, records, TimeSpan.FromMinutes(10));
private Dictionary<string, (DateTime expiration, object value)> cache = new();
public void Set(string key, object value, TimeSpan ttl)
var expiration = DateTime.Now.Add(ttl);
cache[key] = (expiration, value);
public object Get(string key)
if (cache.TryGetValue(key, out var entry) && entry.expiration > DateTime.Now)