Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AsyncCacheExample
- {
- public static void Run()
- {
- var service = new RealService();
- var cache1 = new ZipCodeCache(service);
- Console.Write("Caching ZipCodes: ");
- MeasureCache(cache1);
- var cache2 = new ZipCodeCache2(service);
- Console.Write(" Caching Tasks: ");
- MeasureCache(cache2);
- Console.ReadKey();
- }
- private static void MeasureCache(IZipCodeService zipCodeService)
- {
- const string cityName = "AYCIPUHC";
- var collections0 = GC.CollectionCount(0);
- var collections1 = GC.CollectionCount(1);
- var collections2 = GC.CollectionCount(2);
- var sw = Stopwatch.StartNew();
- for (int i = 0; i < 100000000; i++)
- {
- zipCodeService.GetZipCodesAsync(cityName).Wait();
- }
- sw.Stop();
- var newCollections0 = GC.CollectionCount(0);
- var newCollections1 = GC.CollectionCount(1);
- var newCollections2 = GC.CollectionCount(2);
- Console.WriteLine(sw.Elapsed);
- Console.WriteLine("Gen0: {0} Gen1: {1} Gen2: {2}", newCollections0 - collections0, newCollections1 - collections1, newCollections2 - collections2);
- }
- }
- class RealService : IZipCodeService
- {
- Random random = new Random();
- public async Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName)
- {
- // fake some latency or similiar
- await Task.Delay(random.Next(10) + 10);
- // calculate two "zip codes". First: sum of all letters, Second: first letter only
- var zip1 = cityName.Select(_ => _).Sum(_ => (int)_);
- var zip2 = (int)cityName.First();
- return new[] { new ZipCode(zip1), new ZipCode(zip2), };
- }
- }
- class ZipCode
- {
- public ZipCode(int code)
- {
- Code = code;
- }
- public int Code { get; set; }
- }
- interface IZipCodeService
- {
- Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName);
- }
- class ZipCodeCache : IZipCodeService
- {
- private readonly IZipCodeService realService;
- private readonly ConcurrentDictionary<string, ICollection<ZipCode>> zipCache = new ConcurrentDictionary<string, ICollection<ZipCode>>();
- public ZipCodeCache(IZipCodeService realService)
- {
- this.realService = realService;
- }
- public Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName)
- {
- ICollection<ZipCode> zipCodes;
- if (zipCache.TryGetValue(cityName, out zipCodes))
- {
- return Task.FromResult(zipCodes);
- }
- return this.realService.GetZipCodesAsync(cityName).ContinueWith((task) =>
- {
- this.zipCache.TryAdd(cityName, task.Result);
- return task.Result;
- });
- }
- }
- class ZipCodeCache2 : IZipCodeService
- {
- private readonly IZipCodeService realService;
- private readonly ConcurrentDictionary<string, Task<ICollection<ZipCode>>> zipCache = new ConcurrentDictionary<string, Task<ICollection<ZipCode>>>();
- public ZipCodeCache2(IZipCodeService realService)
- {
- this.realService = realService;
- }
- public Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName)
- {
- Task<ICollection<ZipCode>> zipCodes;
- if (zipCache.TryGetValue(cityName, out zipCodes))
- {
- return zipCodes;
- }
- return this.realService.GetZipCodesAsync(cityName).ContinueWith((task) =>
- {
- this.zipCache.TryAdd(cityName, task);
- return task.Result;
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement