示例#1
0
        public IActionResult Index(CarPricingForm form)
        {
            var model = new CarPriceViewModel();

            model.Form = form;
            if (ModelState.IsValid)
            {
                var startTime = DateTime.Now;
                var result    = _service.GetPrice(form);
                model.Price       = result;
                model.ElapsedTime = DateTime.Now - startTime;
                model.CachedItems = GetCachedItemsFromCouchbase();
            }
            else
            {
                model.ErrorMessage = "Invalid submission.";
            }
            return(View(model));
        }
        public CarPrice GetPrice(CarPricingForm form)
        {
            var key = $"{form.Make}-{form.Model}-{form.Year}";

            var cachedValue = _cache.Get <CarPrice>(key);

            if (cachedValue != null)
            {
                return(cachedValue);
            }

            var result = _inner.GetPrice(form);

            _cache.Set(key, result,
                       new DistributedCacheEntryOptions {
                SlidingExpiration = TimeSpan.FromSeconds(30)
            });

            return(result);
        }