HTTPContext.Cache [ключ] ASP. NET CORE эквивалент - PullRequest
0 голосов
/ 17 февраля 2020

Я пытаюсь визуализировать фотоизображение из байтового массива. Проект, в котором я беру логи c из сборок CacheKey и использует BinaryWrite для httpResponse. Я использую dot. net core 3 и HttpContext.Cache больше не использует. Есть ли способ конвертировать этот код в точку. net core?

У меня есть этот контроллер

public class ImageController : Controller
    {
          public void Get(Guid id, int? width, int? height, int? maxWidth, int? maxHeight)
               {
                var key = this.BuildChacheKey(id, width, height, maxWidth, maxHeight);
                byte[] data = HttpContext.Cache[key] as byte[];
                if (data == null)
                {
                    data = photo.Content;
                    if ((width.HasValue && (maxHeight.HasValue || height.HasValue)) || (height.HasValue && (width.HasValue || maxWidth.HasValue)))
                        data = ImageUtil.SmartResize(photo.Content, width, height, maxWidth, maxHeight);
                    HttpContext.Cache[key] = data;
                }
                HttpContext.Response.BinaryWrite(data);
                HttpContext.Response.ContentType = photo.ContentType;
                HttpContext.Response.StatusCode = 200;
            }
            HttpContext.Response.End();
        }

        private string BuildChacheKey(Guid id, int? width, int? height, int? maxWidth, int? maxHeight)
        {
            var key = String.Format("{0}{1}{2}{3}{4}",
                                     id.ToString(),
                                     width.HasValue ? width.Value.ToString() : "null",
                                     height.HasValue ? height.Value.ToString() : "null",
                                     maxWidth.HasValue ? maxWidth.Value.ToString() : "null",
                                     maxHeight.HasValue ? maxHeight.Value.ToString() : "null"
            );
            return key;
        }
    }

У меня есть промежуточное ПО под названием SecurityManager с использованием IhttpContextAccessor, я попытался добавить IMemoryCache

  public class SecurityManager
    {
        private MyPhotoDbContext _context;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private IMemoryCache _cache;

        public SecurityManager(IHttpContextAccessor httpContextAccessor, MyPhotoDbContext context, IMemoryCache cache)
        {
            _httpContextAccessor = httpContextAccessor;
            _context = context;
            _cache = cache;
        }

        private static ConcurrentDictionary<String, UserInfo> authenticatedUsers = new ConcurrentDictionary<String, UserInfo>();

        public IMemoryCache ReturnCache()
        {
            return _cache;
        }
}

и вызвать его обратно в контроллере :

_securityManager.ReturnCache ()

, но этот интерфейс имеет только 3 метода CreateEntry, Remove и TrygetValue

...