public JsonNetResult ProductFacets(bool includeHidden = false) { 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); } AccountManagementService.Account account = Common.GetAccountObject(accountNameKey); string localCacheKey = accountNameKey + ":product:search:facets:includeHidden:" + includeHidden; List <ProductSearchFacet> facets = null; List <ProductSearchFacetJson> facetsJson = null; if (account == null) { return(new JsonNetResult { Data = "Not found" }); //return Request.CreateResponse(HttpStatusCode.NotFound); } else { #region (Plan A) Get data from local cache try { facetsJson = (List <ProductSearchFacetJson>)HttpRuntime.Cache[localCacheKey]; } catch (Exception e) { var error = e.Message; //TODO: Log: error message for local cache call } #endregion if (facetsJson == 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) { facetsJson = JsonConvert.DeserializeObject <List <ProductSearchFacetJson> >(redisApiValue); executionType = ExecutionType.redis_secondary; } } catch { } #endregion if (facetsJson == null) { #region (Plan C) Get data from Redis Cache try { //Attempt to get facets from the Redis Cache string hashKey = accountNameKey + ":search"; string hashField = "facets:products:visible"; if (includeHidden) { hashField = "facets:products"; } try { var redisValue = cache.HashGet(hashKey, hashField); if (redisValue.HasValue) { facets = JsonConvert.DeserializeObject <List <ProductSearchFacet> >(redisValue); executionType = ExecutionType.redis_main; } } catch { } } catch (Exception e) { var error = e.Message; //Log error message for Redis call } #endregion if (facets == null) { #region (Plan D) Get data from WCF var applicationSearchServiceClient = new ApplicationSearchService.ApplicationSearchServiceClient(); try { applicationSearchServiceClient.Open(); facets = applicationSearchServiceClient.GetProductFacets(accountNameKey, includeHidden, Common.SharedClientKey).ToList(); //Close the connection WCFManager.CloseConnection(applicationSearchServiceClient); executionType = ExecutionType.wcf; } catch (Exception e) { #region Manage Exception string exceptionMessage = e.Message.ToString(); var currentMethod = System.Reflection.MethodBase.GetCurrentMethod(); string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name; // Abort the connection & manage the exception WCFManager.CloseConnection(applicationSearchServiceClient, exceptionMessage, currentMethodString); #endregion } #endregion } } #region Transform into json object, & cache locally or locally and redisAPI layer if (facetsJson != null) { //Just cache locally (we got json from the api redis layer) HttpRuntime.Cache.Insert(localCacheKey, facetsJson, null, DateTime.Now.AddMinutes(Common.SearchFacetsCacheTimeInMinutes), TimeSpan.Zero); } else if (facets != null) { //Transform categories into JSON and cache BOTH locally AND into redis facetsJson = Transforms.Json.SearchTransforms.ProductFacets(facets); HttpRuntime.Cache.Insert(localCacheKey, facetsJson, null, DateTime.Now.AddMinutes(Common.SearchFacetsCacheTimeInMinutes), TimeSpan.Zero); try { cache.HashSet(hashApiKey, hashApiField, JsonConvert.SerializeObject(facetsJson), When.Always, CommandFlags.FireAndForget); } catch { } } #endregion } } //Create results object ProductSearchFacetsJson facetsJsonResult = new ProductSearchFacetsJson(); facetsJsonResult.facets = facetsJson; //Add execution data stopWatch.Stop(); facetsJsonResult.executionType = executionType.ToString(); facetsJsonResult.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 = facetsJsonResult; return(jsonNetResult); }
public JsonNetResult Sortables() { var accountNameKey = Common.GetSubDomain(Request.Url); List <ProductSearchSortable> sortables = null; #region (Plan A) Get data from Redis Cache try { //First we attempt to get tags from the Redis Cache IDatabase cache = CoreServices.RedisConnectionMultiplexers.RedisMultiplexer.GetDatabase(); string hashKey = accountNameKey + ":search"; string hashField = "sortables:products"; var redisValue = cache.HashGet(hashKey, hashField); if (redisValue.HasValue) { sortables = JsonConvert.DeserializeObject <List <ProductSearchSortable> >(redisValue); } } catch (Exception e) { var error = e.Message; //Log error message for Redis call } #endregion if (sortables == null) { #region (Plan B) Get data from WCF var applicationSearchServiceClient = new ApplicationSearchService.ApplicationSearchServiceClient(); try { applicationSearchServiceClient.Open(); sortables = applicationSearchServiceClient.GetProductSortables(accountNameKey, Common.SharedClientKey).ToList(); //Close the connection WCFManager.CloseConnection(applicationSearchServiceClient); } catch (Exception e) { #region Manage Exception string exceptionMessage = e.Message.ToString(); var currentMethod = System.Reflection.MethodBase.GetCurrentMethod(); string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name; // Abort the connection & manage the exception WCFManager.CloseConnection(applicationSearchServiceClient, exceptionMessage, currentMethodString); #endregion } #endregion } JsonNetResult jsonNetResult = new JsonNetResult(); jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented; jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime jsonNetResult.Data = sortables; return(jsonNetResult); }