/// <summary> /// Inserts an item into the cache, if it already exists in the cache it will be replaced /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cacheKey"></param> /// <param name="priority"></param> /// <param name="getCacheItem"></param> public void InsertCacheItem <T>(string cacheKey, CacheItemPriority priority, Func <T> getCacheItem) { if (_enableCache == false) { _nullHttpCache.InsertCacheItem <T>(cacheKey, getCacheItem, priority: priority); } else { _httpCache.InsertCacheItem <T>(cacheKey, getCacheItem, priority: priority); } }
public override Attempt <IShipmentRateQuote> QuoteShipment(IShipment shipment) { try { // TODO this should be made configurable var visitor = new FedExShipmentLineItemVisitor() { UseOnSalePriceIfOnSale = false }; shipment.Items.Accept(visitor); var province = ShipMethod.Provinces.FirstOrDefault(x => x.Code == shipment.ToRegion); var shippingPrice = 0M; try { var service = new RateService { Url = "https://wsbeta.fedex.com:443/web-services/rate" }; var collection = GetCollectionFromCache(shipment); if (collection == null) { var reply = service.getRates(RateRequest(shipment, visitor.TotalWeight, visitor.Quantity)); if (reply.HighestSeverity == NotificationSeverityType.SUCCESS || reply.HighestSeverity == NotificationSeverityType.NOTE || reply.HighestSeverity == NotificationSeverityType.WARNING) { collection = BuildDeliveryOptions(reply, shipment); _runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection); } } var firstCarrierRate = collection.FirstOrDefault(option => option.Service.Contains(_shipMethod.ServiceCode)); if (firstCarrierRate != null) { shippingPrice = firstCarrierRate.Rate; } } catch (Exception ex) { return(Attempt <IShipmentRateQuote> .Fail( new Exception("An error occured during your request : " + ex.Message + " Please contact your administrator or try again."))); } return(Attempt <IShipmentRateQuote> .Succeed(new ShipmentRateQuote(shipment, _shipMethod) { Rate = shippingPrice })); } catch (Exception ex) { return(Attempt <IShipmentRateQuote> .Fail( new Exception("An error occured during your request : " + ex.Message + " Please contact your administrator or try again."))); } }
public static void InsertCacheItem <T>(this IRuntimeCacheProvider provider, string cacheKey, Func <T> getCacheItem, TimeSpan?timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null) { provider.InsertCacheItem(cacheKey, () => getCacheItem(), timeout, isSliding, priority, removedCallback, dependentFiles); }
public override Attempt <IShipmentRateQuote> QuoteShipment(IShipment shipment) { try { // TODO this should be made configurable var visitor = new UPSShipmentLineItemVisitor() { UseOnSalePriceIfOnSale = false }; shipment.Items.Accept(visitor); var province = ShipMethod.Provinces.FirstOrDefault(x => x.Code == shipment.ToRegion); var collection = GetCollectionFromCache(shipment); if (collection == null) { try { var http = new UpsHttpRequestHandler(); var rateXml = http.Post(RateRequest(shipment, visitor)); collection = UpsParseRates(rateXml); _runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection); } catch (Exception ex) { return(Attempt <IShipmentRateQuote> .Fail( new Exception("An error occured during your request : " + ex.Message + " Please contact your administrator or try again."))); } } var shippingPrice = 0M; var firstCarrierRate = collection.FirstOrDefault(option => option.Service == _shipMethod.ServiceCode); if (firstCarrierRate != null) { shippingPrice = firstCarrierRate.Rate; } return(Attempt <IShipmentRateQuote> .Succeed(new ShipmentRateQuote(shipment, _shipMethod) { Rate = shippingPrice })); } catch (Exception ex) { return(Attempt <IShipmentRateQuote> .Fail( new Exception("An error occured during your request : " + ex.Message + " Please contact your administrator or try again."))); } }
/// <summary> /// Gets an entity by the passed in Key /// </summary> /// <param name="key"> /// The key. /// </param> /// <returns> /// The entity retrieved /// </returns> public TEntity Get(Guid Key) { var fromCache = TryGetFromCache(Key); if (fromCache.Success) { return(fromCache.Result); } var entity = PerformGet(Key); if (entity != null) { _cache.InsertCacheItem(GetCacheKey(Key), () => entity); //_cache.GetCacheItem(GetCacheKey(Key), () => entity); } return(entity); }
public static string GetCropUrl( this IPublishedContent mediaItem, int?width = null, int?height = null, string propertyAlias = Constants.Conventions.Media.File, string cropAlias = null, int?quality = null, ImageCropMode?imageCropMode = null, ImageCropAnchor?imageCropAnchor = null, bool preferFocalPoint = false, bool useCropDimensions = false, bool cacheBuster = true, string furtherOptions = null, ImageCropRatioMode?ratioMode = null, bool upScale = true, bool resolveCdnPath = false) { string cropUrl = ImageCropperTemplateExtensions.GetCropUrl( mediaItem, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale); if (!resolveCdnPath) { return(cropUrl); } string cachePrefix = "Jaywing-AzureBlobCache"; string cacheKey = $"{cachePrefix}{cropUrl}"; string currentDomain = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); string absoluteCropPath = $"{currentDomain}{cropUrl}"; IRuntimeCacheProvider runtimeCache = DependencyResolver.Current.GetService <ApplicationContext>()?.ApplicationCache?.RuntimeCache; if (runtimeCache == null) { return(cropUrl); } var cachedItem = runtimeCache.GetCacheItem <CachedImage>(cacheKey); if (cachedItem != null) { return(cachedItem.CacheUrl); } var newCachedImage = new CachedImage { WebUrl = cropUrl }; try { var request = (HttpWebRequest)WebRequest.Create(absoluteCropPath); request.Method = WebRequestMethods.Http.Head; using (var response = (HttpWebResponse)request.GetResponse()) { HttpStatusCode responseCode = response.StatusCode; if (!responseCode.Equals(HttpStatusCode.OK)) { return(cachedItem.CacheUrl); } newCachedImage.CacheUrl = response.ResponseUri.AbsoluteUri; runtimeCache.InsertCacheItem <CachedImage>(cacheKey, () => newCachedImage); return(response.ResponseUri.AbsoluteUri); } } catch { return(cropUrl); } }
public void SetCacheValue(string cacheKey, object cacheValue) { _runtimeCache.InsertCacheItem($"TeaCommerce_{cacheKey}", () => cacheValue); }