Exemple #1
0
        public string GetDefaultUrl(int serverId, string baseUrl, string connectionStr)
        {
            Uri    u             = new Uri(baseUrl);
            int    cacheDuration = _options.Value.CacheDefaultUrlsDurationInSceconds;
            string cacheKey      = $"{_options.Value.CacheDefaultUrlsKey}_{u.Host}";
            string defaultUrl    = "";
            IEnumerable <ServerDefaultUrl> serverUrls;

            if (!_cacheProvider.TryGetValue <IEnumerable <ServerDefaultUrl> >(cacheKey, out serverUrls))
            {
                //Not exists
                defaultUrl = _defaultUrlRepository.GetDefaultUrl(serverId, connectionStr);
                if (!string.IsNullOrEmpty(defaultUrl))
                {
                    AddToCache(cacheKey, cacheDuration,
                               new ServerDefaultUrl()
                    {
                        defaultUrl = defaultUrl, serverId = serverId
                    },
                               new List <ServerDefaultUrl>());
                }
            }
            else
            {
                //Cache Exists
                defaultUrl = serverUrls.Where(x => x.serverId == serverId).Select(y => y.defaultUrl).FirstOrDefault();
                if (string.IsNullOrEmpty(defaultUrl))
                {
                    defaultUrl = _defaultUrlRepository.GetDefaultUrl(serverId, connectionStr);
                    if (!string.IsNullOrEmpty(defaultUrl))
                    {
                        AddToCache(cacheKey, cacheDuration,
                                   new ServerDefaultUrl()
                        {
                            defaultUrl = defaultUrl, serverId = serverId
                        },
                                   serverUrls.ToList());
                    }
                }
            }

            return(defaultUrl);
        }
Exemple #2
0
        public void Test_DefaultUrlReposity_no_connection_string_return_empty()
        {
            string connectionString = "fake_connection_string";
            string sqlScript        = "SELECT TOP 1 url FROM click_server_urls WHERE default_p=1 AND server_id=@serverId";
            string retUrl           = "returnUrl";

            mockDatabaseProvider = new Mock <IDatabaseProvider>();
            mockDatabaseProvider.Setup(p => p.GetCellValue <string>(connectionString, sqlScript, It.IsAny <object>(), CommandType.Text)).Returns(retUrl);

            defaultUrlRepository = new DefaultUrlRepository(mockDatabaseProvider.Object);

            var connString = defaultUrlRepository.GetDefaultUrl(0, connectionString);

            Assert.Empty(connString);
        }