public DashboardController()
    {
      ICacheService cacheService = new WebCacheService();

      //singletonish ftw
      if (TeamCityDataService == null)
      {
        TeamCityDataService = new TeamCityDataService(
          ConfigurationManager.AppSettings["teamcity.baseUrl"],
          ConfigurationManager.AppSettings["teamcity.username"],
          ConfigurationManager.AppSettings["teamcity.password"],
          cacheService
        );
      }

      //singletonish ftw
      if (SonarDataService == null)
      {
        SonarDataService = new SonarDataService(
          ConfigurationManager.AppSettings["sonar.baseUrl"],
          ConfigurationManager.AppSettings["sonar.username"],
          ConfigurationManager.AppSettings["sonar.password"],
          cacheService
        );
      }

      if (GithubDataService == null)
      {
        GithubDataService = new TeamCityDashboard.Services.GithubDataService(
          (string)ConfigurationManager.AppSettings["github.oauth2token"],
          (string)ConfigurationManager.AppSettings["github.api.events.url"]
          , cacheService
        );
      }
    }
        public DashboardController()
        {
            ICacheService cacheService = new WebCacheService();

            //singletonish ftw
            if (TeamCityDataService == null)
            {
                TeamCityDataService = new TeamCityDataService(
                    ConfigurationManager.AppSettings["teamcity.baseUrl"],
                    ConfigurationManager.AppSettings["teamcity.username"],
                    ConfigurationManager.AppSettings["teamcity.password"],
                    cacheService
                    );
            }

            //singletonish ftw
            if (SonarDataService == null)
            {
                SonarDataService = new SonarDataService(
                    ConfigurationManager.AppSettings["sonar.baseUrl"],
                    ConfigurationManager.AppSettings["sonar.username"],
                    ConfigurationManager.AppSettings["sonar.password"],
                    cacheService
                    );
            }

            if (GithubDataService == null)
            {
                GithubDataService = new GithubDataService(
                    ConfigurationManager.AppSettings["github.oauth2token"],
                    ConfigurationManager.AppSettings["github.api.events.url"]
                    , cacheService
                    );
            }
        }
        public static void DeleteCacheEntityWithUrl(string url)
        {
            if (WebCacheService == null)
            {
                InitCacheConnection();
            }

            if (isWebCacheEnabled && WebCacheService != null)
            {
                var dnsHost = string.Empty;
                try
                {
                    dnsHost = new Uri(url).DnsSafeHost?.ToUpper();
                }
                catch { }

                var cacheHashKey = HashUrl.GetHashAsString(dnsHost);

                var task = Task.Run(() => WebCacheService.RemoveAllKeysInHash(cacheHashKey));
                if (!task.Wait(_cacheTimeout))
                {
                    ConsoleLogger.Write($"ERROR: CACHE DeleteCacheEntityWithUrl - Redis TimeOut");
                }
            }
        }
        public static void DeleteCacheEntity(string host)
        {
            try
            {
                if (WebCacheService == null)
                {
                    InitCacheConnection();
                }

                if (WebCacheService != null)
                {
                    var cacheHashKey = HashUrl.GetHashAsString(host.ToUpper());

                    var task = Task.Run(() => WebCacheService.RemoveAllKeysInHash(cacheHashKey));
                    if (!task.Wait(_cacheTimeout))
                    {
                        ConsoleLogger.Write($"ERROR: CACHE DeleteCacheEntity - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE DeleteCacheEntity - {ex.ToString()}");
            }
        }
        public static WebCacheFileModel GetCacheEntityFromUrl(string url, out string cacheKey)
        {
            cacheKey = null;
            try
            {
                if (WebCacheService == null)
                {
                    InitCacheConnection();
                }

                if (isWebCacheEnabled && !String.IsNullOrEmpty(url) && WebCacheService != null)
                {
                    var dnsHost       = string.Empty;
                    var pathWithQuery = string.Empty;
                    try
                    {
                        var uri = new Uri(url);
                        dnsHost       = uri.DnsSafeHost?.ToUpper();
                        pathWithQuery = uri?.PathAndQuery;
                    }
                    catch (Exception ex)
                    {
                        ConsoleLogger.Write($"ERROR: CACHE GetCacheEntityFromUrl->>dnsHost {url} - {ex.ToString()}");
                    }

                    var cacheHashKey  = HashUrl.GetHashAsString(dnsHost);
                    var objectHashKey = HashUrl.GetHashAsString(pathWithQuery);
                    cacheKey = $"{dnsHost}-{pathWithQuery}";
                    var task = Task.Run(() => WebCacheService.GetRedisValue(cacheHashKey, objectHashKey));
                    if (task.Wait(_cacheTimeout))
                    {
                        return(MessagePackSerializer.Deserialize <WebCacheFileModel>(task.Result));
                    }
                    else
                    {
                        ConsoleLogger.Write($"ERROR: CACHE GetCacheEntityFromUrl - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE GetCacheEntityFromUrl {url} - {ex.ToString()}");
            }
            return(null);
        }
        public static async Task <string> SaveCacheEntity(string url, WebCacheFileModel cacheEntity, long ttl = -1)
        {
            try
            {
                if (WebCacheService == null)
                {
                    InitCacheConnection();
                }

                if (isWebCacheEnabled && WebCacheService != null)
                {
                    var dnsHost       = string.Empty;
                    var pathWithQuery = string.Empty;
                    try
                    {
                        var uri = new Uri(url);
                        dnsHost       = uri.DnsSafeHost?.ToUpper();
                        pathWithQuery = uri?.PathAndQuery;
                    }
                    catch { }

                    var cacheHashKey = HashUrl.GetHashAsString(dnsHost);
                    var cacheKey     = HashUrl.GetHashAsString(pathWithQuery);
                    var cacheValue   = MessagePackSerializer.Serialize <WebCacheFileModel>(cacheEntity);

                    var task = WebCacheService.Save(cacheHashKey, cacheKey, cacheValue);
                    if (task.Wait(_cacheTimeout))
                    {
                        return($"{dnsHost}-{pathWithQuery}");
                    }
                    else
                    {
                        ConsoleLogger.Write($"ERROR: CACHE SaveCacheEntity(url, entity, ttl) - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE SaveCacheEntity(url, entity, ttl) - {ex.ToString()}");
            }

            return(null);
        }
        private static void InitCacheConnection()
        {
            try
            {
                if (isApiCacheEnabled && (ApiCacheService == null || !ApiCacheService.IsConnectionActive()))
                {
                    ApiCacheService = new RedisCacheService(KLMApiCacheUrl, 6379);
                }

                if (isWebCacheEnabled && (WebCacheService == null || !WebCacheService.IsConnectionActive()))
                {
                    WebCacheService = new RedisCacheService(KLMWebCacheUrl, 6379);
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE InitCacheConnection - {ex.ToString()}");
            }
        }
        public static async Task <string> SaveCacheEntity(string url, string key, WebCacheFileModel cacheEntity, long ttl = -1)
        {
            try
            {
                if (WebCacheService == null)
                {
                    InitCacheConnection();
                }

                if (isWebCacheEnabled && WebCacheService != null)
                {
                    var dnsHost = string.Empty;
                    try
                    {
                        dnsHost = new Uri(url).DnsSafeHost?.ToUpper();
                    }
                    catch { }

                    var cacheHashKey = HashUrl.GetHashAsString(dnsHost);
                    var cacheKey     = HashUrl.GetHashAsString(key);
                    var cacheValue   = JsonConvert.SerializeObject(cacheEntity);

                    var task = WebCacheService.Save(cacheHashKey, cacheKey, cacheValue);
                    if (task.Wait(_cacheTimeout))
                    {
                        return($"{cacheHashKey}-{cacheKey}");
                    }
                    else
                    {
                        ConsoleLogger.Write($"ERROR: CACHE SaveCacheEntity(url, key, entity, ttl) - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE SaveCacheEntity(url, key, entity, ttl) - {ex.ToString()}");
            }

            return(null);
        }
        protected override void RenderChildren(HtmlTextWriter writer)
        {
            ICacheService cache = new WebCacheService();

            try
            {
                Control scriptManager = ((Control)ReflectionHelper.GetFieldValue(this, "m_scriptManagerProxy", cache));
                Control elementDrag   = ((Control)ReflectionHelper.GetFieldValue(this, "m_elementDragHelper", cache));
                Control mainSection   = ((Control)ReflectionHelper.GetFieldValue(this, "m_mainSection", cache));
                Control middleSection = ((Control)ReflectionHelper.GetFieldValue(this, "m_middleSection", cache));

                scriptManager.RenderControl(writer);
                elementDrag.RenderControl(writer);
                this.StateProxy.RenderControl(writer);
                mainSection.RenderControl(writer);
                middleSection.RenderControl(writer);
            }
            catch (KeyNotFoundException)
            {
                base.RenderChildren(writer);
            }
        }