public async Task <BitmapRepresentation> GetBitmap(string url) { if (string.IsNullOrEmpty(url)) { return(null); } var result = _cache.GetOrAdd(url, async s => { using (var client = new HttpClient()) using (var stream = client.GetStreamAsync(url)) using (var bitmap = (Bitmap)Image.FromStream(await stream)) return(new BitmapRepresentation(bitmap)); }); if (_cache.Count > CacheSize) { var victim = _cache.Keys.First(v => v != url); _cache.TryRemove(victim, out _); } return((await result).Clone()); }
public async Task <IDisposable> LockAsync(T key) { var semaphore = semaphoreDictionary.GetOrAdd(key, () => new SemaphoreSlim(1, 1)); await semaphore.WaitAsync(); return(new ActionDisposable(() => semaphore.Release())); }
public IConnectionMultiplexer PreserveAsyncOrderGetConnection(string connectionString) { if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentNullException(nameof(connectionString)); } var connectionMultiplexer = PreserveOrderConnectionMultiplexers.GetOrAdd(connectionString, cfg => { var options = ConfigurationOptions.Parse(connectionString); var conn = ConnectionMultiplexer.Connect(options); conn.PreserveAsyncOrder = true; return(conn); }); return(connectionMultiplexer); }
internal FontFamily GetFontFamilyByResourceName(string resourceName, byte[] resource) { return(_fontFamilies.GetOrAdd(resourceName, s => { using (Stream fontStream = new MemoryStream(resource)) { IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length); byte[] fontdata = new byte[fontStream.Length]; fontStream.Read(fontdata, 0, (int)fontStream.Length); Marshal.Copy(fontdata, 0, data, (int)fontStream.Length); _fontCollection.AddMemoryFont(data, (int)fontStream.Length); Marshal.FreeCoTaskMem(data); } return _fontCollection.Families.Last(); })); }
public IEnumerable <T> GetServices <T>() where T : IService { return(_initedServices.GetOrAdd(typeof(T), type => { var services = Services.ModulesService.GetModules <T>() .Where(s => !Options.DiabledServices.Contains(s.Id)).Cast <IService>().ToArray(); foreach (var service in services) { if (_initedServiceInstances.TryAdd(service, service)) { InitializeEntity(service); } } return services; }).OfType <T>()); }
private void PlaceElement(CompositeLayoutElementInfo elementInfo) { AddElement(elementInfo.Location, _initedElements.GetOrAdd(elementInfo, info => GlobalContext.CreateElement(info.ModuleInfo))); }