public async Task Invoke(HttpContext context, IImageCacheHelper cacheHelper)
        {
            var imageId = context.Request?.Path.Value.Split('/').Last();

            if (context.Request.Path.Value.Contains("image", StringComparison.InvariantCultureIgnoreCase))
            {
                _cacheHelper.InvalidateCacheIfRequired();

                string fileExists = _cacheHelper.TryGetImage(imageId);

                if (string.IsNullOrEmpty(fileExists) == false)
                {
                    await context.Response.SendFileAsync($"{ _cacheHelper.CacheFolder}/{ imageId}.{ _cacheHelper.ImageExtension}");
                }
                else
                {
                    var imagesInCache = _cacheHelper.GetCachedImagesCount();

                    if (imagesInCache >= _options.MaxImages)
                    {
                        await _next(context);
                    }
                    else
                    {
                        await CacheImageAndProceed(context, imageId);
                    }
                }
            }
            else
            {
                await _next(context);
            }
        }
Пример #2
0
 public ImageCaching(RequestDelegate next, IImageCacheHelper cacheHelper, CachingOptions options)
 {
     _next        = next;
     _options     = options;
     _cacheHelper = cacheHelper;
     _cacheHelper.InitCacheOptions(_options);
 }
 public ImageCachingMiddleware(
     RequestDelegate next,
     IHostingEnvironment env,
     IImageCacheHelper cacheHelper,
     CachingOptions options)
 {
     _next        = next;
     _options     = options;
     _cacheHelper = cacheHelper;
     _cacheHelper.InitializeCache(_options);
 }