private IFileInfo GetImageFromCache(IImageStorage imageStorage, DateTime requestTime)
        {
            KeyValuePair <string, DateTime> cachedPath;
            IFileInfo fileInfo = null;

            if (imageChache.TryGetValue(imageStorage.ImagePath, out cachedPath))
            {
                if (requestTime.Subtract(cachedPath.Value).Seconds > MainCfg.MaxImagesMiddlewareCacheSeconds)
                {
                    if (imageChache.TryRemove(imageStorage.ImagePath, out cachedPath))
                    {
                        logger.LogTrace(
                            MainCfg.LogEventId,
                            "Path '{0}' was removed from the image cache due to time expiration!",
                            imageStorage.ImagePath);
                    }
                    else
                    {
                        logger.LogInformation(
                            MainCfg.LogEventId,
                            "Couldn't remove the path '{0}' from the image cache due to time expiration!",
                            imageStorage.ImagePath);
                    }
                }

                fileInfo = imageStorage.GetImageDirectly(imageStorage.ImagePath);
            }

            return(fileInfo);
        }
        private async Task <IFileInfo> GetImageFromStorageAsync(IImageStorage imageStorage, DateTime requestTime)
        {
            IFileInfo fileInfo = null;
            KeyValuePair <string, DateTime> cacheItem;

            fileInfo = imageStorage.GetImageDirectly();

            if (fileInfo == null || !fileInfo.Exists)
            {
                fileInfo = await imageStorage.GetImageVariantAsync();
            }

            if (fileInfo != null && fileInfo.Exists)
            {
                if (!imageChache.TryGetValue(imageStorage.ImagePath, out cacheItem))
                {
                    cacheItem = new KeyValuePair <string, DateTime>(fileInfo.PhysicalPath, requestTime);

                    if (imageChache.TryAdd(imageStorage.ImagePath, cacheItem))
                    {
                        logger.LogInformation(
                            MainCfg.LogEventId,
                            "Path '{0}' was succesfully aedded to image cache.",
                            imageStorage.ImagePath);

                        if (imageChache.Count > MainCfg.MaxImagesMiddlewareCachedRequests)
                        {
                            KeyValuePair <string, KeyValuePair <string, DateTime> >?lastCachedItem = imageChache.OrderBy(c => c.Value.Value).LastOrDefault();

                            if (lastCachedItem.HasValue)
                            {
                                KeyValuePair <string, DateTime> removedCachedValue;

                                if (imageChache.TryRemove(lastCachedItem.Value.Key, out removedCachedValue))
                                {
                                    logger.LogTrace(
                                        MainCfg.LogEventId,
                                        "Path '{0}' was removed from the image cache due to the maximum value of the cahce items has been exceeded!",
                                        imageStorage.ImagePath);
                                }
                                else
                                {
                                    logger.LogInformation(
                                        MainCfg.LogEventId,
                                        "Couldn't remove the path '{0}' from the image cache due the maximum value of the cahce items has been exceeded!",
                                        imageStorage.ImagePath);
                                }
                            }
                        }
                    }
                    else
                    {
                        logger.LogInformation(MainCfg.LogEventId, "Couldn't add the path '{0}' to image cache.", imageStorage.ImagePath);
                    }
                }
            }

            return(fileInfo);
        }