Exemplo n.º 1
0
        public async Task <IActionResult> Get(string name, int width, int height, string type, string watermark = "", string backgroundColor = "")
        {
            // Get the requested ETag
            string requestETag = "";

            if (Request.Headers.ContainsKey("If-None-Match"))
            {
                requestETag = Request.Headers["If-None-Match"].First();
            }

            //check distributed cache (Redis) if we had this exact request previously and we have it in cache!
            string concatenatedKey = GetConcatenatedKey(name, width, height, type, watermark, backgroundColor);

            FileContentResult fcr = null;
            string            key = GenerateHashKey(concatenatedKey);

            byte[] imageBytes;
            if (redisCacheService.HasValudBeenCached(key).Result)
            {
                //we have this in cache!
                imageBytes = redisCacheService.GetCachedValueAsync(key).Result;
            }
            else
            {
                GetImageQuery query = GetImageQueryObject(name, width, height, type, watermark, backgroundColor);

                //Using CQRS technique to send the query to the mediatR

                var result = await Mediator.Send(query);

                imageBytes = result.Data.ImageByteArray;
                //Cache it now
                redisCacheService.SetCacheValueAsync(key, imageBytes);
            }



            // Construct the new ETag
            string responseETag = key;

            // Return a 304 if the ETag of the current record matches the ETag in the "If-None-Match" HTTP header
            if (Request.Headers.ContainsKey("If-None-Match") && responseETag == requestETag)
            {
                return(Ok(StatusCode((int)HttpStatusCode.NotModified)));
            }

            // Add the current ETag to the HTTP header
            Response.Headers.Add("ETag", responseETag);

            fcr = GetImageFile(type, imageBytes);
            return(fcr);
        }