Advertisement
Guest User

CachingTask

a guest
Mar 23rd, 2016
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1.     class AsyncCacheExample
  2.     {
  3.         public static void Run()
  4.         {
  5.             var service = new RealService();
  6.             var cache1 = new ZipCodeCache(service);
  7.             Console.Write("Caching ZipCodes: ");
  8.             MeasureCache(cache1);
  9.             var cache2 = new ZipCodeCache2(service);
  10.             Console.Write("   Caching Tasks: ");
  11.             MeasureCache(cache2);
  12.             Console.ReadKey();
  13.         }
  14.  
  15.         private static void MeasureCache(IZipCodeService zipCodeService)
  16.         {
  17.             const string cityName = "AYCIPUHC";
  18.             var collections0 = GC.CollectionCount(0);
  19.             var collections1 = GC.CollectionCount(1);
  20.             var collections2 = GC.CollectionCount(2);
  21.             var sw = Stopwatch.StartNew();
  22.             for (int i = 0; i < 100000000; i++)
  23.             {
  24.                 zipCodeService.GetZipCodesAsync(cityName).Wait();
  25.             }
  26.             sw.Stop();
  27.             var newCollections0 = GC.CollectionCount(0);
  28.             var newCollections1 = GC.CollectionCount(1);
  29.             var newCollections2 = GC.CollectionCount(2);
  30.             Console.WriteLine(sw.Elapsed);
  31.             Console.WriteLine("Gen0: {0} Gen1: {1} Gen2: {2}", newCollections0 - collections0, newCollections1 - collections1, newCollections2 - collections2);
  32.         }
  33.     }
  34.  
  35.     class RealService : IZipCodeService
  36.     {
  37.         Random random = new Random();
  38.  
  39.         public async Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName)
  40.         {
  41.             // fake some latency or similiar
  42.             await Task.Delay(random.Next(10) + 10);
  43.             // calculate two "zip codes". First: sum of all letters, Second: first letter only
  44.             var zip1 = cityName.Select(_ => _).Sum(_ => (int)_);
  45.             var zip2 = (int)cityName.First();
  46.             return new[] { new ZipCode(zip1), new ZipCode(zip2), };
  47.         }
  48.     }
  49.  
  50.     class ZipCode
  51.     {
  52.         public ZipCode(int code)
  53.         {
  54.             Code = code;
  55.         }
  56.  
  57.         public int Code { get; set; }
  58.     }
  59.  
  60.     interface IZipCodeService
  61.     {
  62.         Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName);
  63.     }
  64.  
  65.     class ZipCodeCache : IZipCodeService
  66.     {
  67.         private readonly IZipCodeService realService;
  68.         private readonly ConcurrentDictionary<string, ICollection<ZipCode>> zipCache = new ConcurrentDictionary<string, ICollection<ZipCode>>();
  69.  
  70.         public ZipCodeCache(IZipCodeService realService)
  71.         {
  72.             this.realService = realService;
  73.         }
  74.  
  75.         public Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName)
  76.         {
  77.             ICollection<ZipCode> zipCodes;
  78.             if (zipCache.TryGetValue(cityName, out zipCodes))
  79.             {
  80.                 return Task.FromResult(zipCodes);
  81.             }
  82.             return this.realService.GetZipCodesAsync(cityName).ContinueWith((task) =>
  83.             {
  84.                 this.zipCache.TryAdd(cityName, task.Result);
  85.                 return task.Result;
  86.             });
  87.         }
  88.     }
  89.  
  90.     class ZipCodeCache2 : IZipCodeService
  91.     {
  92.         private readonly IZipCodeService realService;
  93.         private readonly ConcurrentDictionary<string, Task<ICollection<ZipCode>>> zipCache = new ConcurrentDictionary<string, Task<ICollection<ZipCode>>>();
  94.  
  95.         public ZipCodeCache2(IZipCodeService realService)
  96.         {
  97.             this.realService = realService;
  98.         }
  99.  
  100.         public Task<ICollection<ZipCode>> GetZipCodesAsync(string cityName)
  101.         {
  102.             Task<ICollection<ZipCode>> zipCodes;
  103.             if (zipCache.TryGetValue(cityName, out zipCodes))
  104.             {
  105.                 return zipCodes;
  106.             }
  107.             return this.realService.GetZipCodesAsync(cityName).ContinueWith((task) =>
  108.             {
  109.                 this.zipCache.TryAdd(cityName, task);
  110.                 return task.Result;
  111.             });
  112.         }
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement