public static PropertyResultJson Property(string accountNameKey, PropertyModel propertyIn) { var propertyJsonOut = new PropertyResultJson(); propertyJsonOut.property = new PropertyJson(); var propertyJson = new PropertyJson { propertyName = propertyIn.PropertyName, propertyNameKey = propertyIn.PropertyNameKey, propertyType = propertyIn.PropertyTypeNameKey, searchField = propertyIn.SearchFieldName }; if (propertyIn.PropertyTypeNameKey == "number" && propertyIn.Symbol != null) { propertyJson.symbol = new PropertySymbolJson { value = propertyIn.Symbol, placement = propertyIn.SymbolPlacement }; } if (propertyIn.PropertyTypeNameKey == "swatch" && propertyIn.Swatches != null) { propertyJson.swatches = new List <PropertySwatchesJson>(); foreach (PropertySwatchModel swatch in propertyIn.Swatches) { propertyJson.swatches.Add(new PropertySwatchesJson { label = swatch.PropertySwatchLabel, image = swatch.PropertySwatchImage, imageMedium = swatch.PropertySwatchImageMedium, imageSmall = swatch.PropertySwatchImageSmall }); } } propertyJsonOut.property = (propertyJson); return(propertyJsonOut); }
public JsonNetResult GetProductProperty(string propertyNameKey) { ExecutionType executionType = ExecutionType.local; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); //Get the subdomain (if exists) for the api call string accountNameKey = Common.GetSubDomain(Request.Url); if (String.IsNullOrEmpty(accountNameKey)) { return(new JsonNetResult { Data = "Not found" }); //return Request.CreateResponse(HttpStatusCode.NotFound); } PropertyModel property = null; PropertyResultJson propertyJson = null; string localCacheKey = accountNameKey + ":property:" + propertyNameKey; #region (Plan A) Get json from local cache try { propertyJson = (PropertyResultJson)HttpRuntime.Cache[localCacheKey]; } catch (Exception e) { var error = e.Message; //TODO: Log: error message for local cache call } #endregion if (propertyJson == null) { #region (Plan B) Get Public json from second layer of Redis Cache IDatabase cache = CoreServices.RedisConnectionMultiplexers.RedisMultiplexer.GetDatabase(); string pathAndQuery = Common.GetApiPathAndQuery(Request.Url); string hashApiKey = accountNameKey + ":apicache"; string hashApiField = pathAndQuery; try { var redisApiValue = cache.HashGet(hashApiKey, hashApiField); if (redisApiValue.HasValue) { propertyJson = JsonConvert.DeserializeObject <PropertyResultJson>(redisApiValue); executionType = ExecutionType.redis_secondary; } } catch { } #endregion if (propertyJson == null) { #region (Plan C) Get property data from Redis Cache or WCF and EXTRACT var properties = DataAccess.Properties.GetProperties(accountNameKey, PropertyListType.All, out executionType); property = properties.Find(x => x.PropertyNameKey.ToLower().Replace(" ", "") == propertyNameKey.ToLower().Replace(" ", "")); if (property == null) { JsonNetResult jsonNetResultEmpty = new JsonNetResult(); jsonNetResultEmpty.Formatting = Newtonsoft.Json.Formatting.Indented; jsonNetResultEmpty.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime jsonNetResultEmpty.Data = null; return(jsonNetResultEmpty); } #endregion } #region Transform into json object, add images & cache locally or locally and radisAPI layer if (propertyJson != null) { //Just cache locally (we got json from the api redis layer) HttpRuntime.Cache.Insert(localCacheKey, propertyJson, null, DateTime.Now.AddMinutes(Common.PropertiesCacheTimeInMinutes), TimeSpan.Zero); } else if (property != null) { //Transform properties into JSON and cache BOTH locally AND into redis propertyJson = Transforms.Json.PropertyTransforms.Property(accountNameKey, property); HttpRuntime.Cache.Insert(localCacheKey, propertyJson, null, DateTime.Now.AddMinutes(Common.PropertiesCacheTimeInMinutes), TimeSpan.Zero); try { cache.HashSet(hashApiKey, hashApiField, JsonConvert.SerializeObject(propertyJson), When.Always, CommandFlags.FireAndForget); } catch { } } #endregion } //Add execution data stopWatch.Stop(); propertyJson.executionType = executionType.ToString(); propertyJson.executionTime = stopWatch.Elapsed.TotalMilliseconds + "ms"; JsonNetResult jsonNetResult = new JsonNetResult(); jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented; jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime jsonNetResult.Data = propertyJson; return(jsonNetResult); }