public static string SaveExpression(string key, dynamic value)
        {
            try
            {
                if (ExpressionCacheService == null || !ExpressionCacheService.IsConnectionActive())
                {
                    InitPropertyCacheConnection();
                }

                if (isExpressionCacheEnabled)
                {
                    var cacheKey   = HashUrl.GetHashAsString(key);
                    var cacheValue = JsonConvert.SerializeObject(value);

                    var task = ExpressionCacheService.Save(cacheKey, cacheValue, 60 * 12);
                    if (task.Wait(_cacheTimeout))
                    {
                        return($"{cacheKey}");
                    }
                    else
                    {
                        ConsoleLogger.Write($"ERROR: CACHE SaveExpression - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE SaveExpression - {ex.ToString()}");
            }

            return(null);
        }
        public static List <BsonDocument> GetExpression(string key)
        {
            try
            {
                if (ExpressionCacheService == null || !ExpressionCacheService.IsConnectionActive())
                {
                    InitPropertyCacheConnection();
                }

                if (isExpressionCacheEnabled)
                {
                    var objectHashKey = HashUrl.GetHashAsString(key);

                    var task = ExpressionCacheService.Get(objectHashKey);
                    if (task.Wait(_cacheTimeout))
                    {
                        return(JsonConvert.DeserializeObject <List <BsonDocument> >(task.Result));
                    }
                    else
                    {
                        ConsoleLogger.Write($"ERROR: CACHE GetExpression - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE GetExpression - {ex.ToString()}");
            }
            return(null);
        }
        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()}");
            }
        }
        internal static void UpdateThirdPartyAPI(string apiendpoint, string model, string rootAliasuri)
        {
            try
            {
                if (!String.IsNullOrEmpty(apiendpoint) && isApiCacheEnabled && !String.IsNullOrEmpty(rootAliasuri))
                {
                    var dnsHost = string.Empty;
                    try
                    {
                        dnsHost = new Uri(rootAliasuri).DnsSafeHost?.ToUpper();
                    }
                    catch { }

                    apiendpoint = apiendpoint.ToUpper();
                    var cacheKey     = HashUrl.GetHashAsString(apiendpoint);
                    var cacheHashKey = HashUrl.GetHashAsString(dnsHost);

                    var task = ApiCacheService.Save(cacheHashKey, cacheKey, model);
                    if (!task.Wait(_cacheTimeout))
                    {
                        ConsoleLogger.Write($"ERROR: CACHE UpdateThirdPartyAPI - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: Unable to UpdateThirdPartyAPI({apiendpoint}, {rootAliasuri}) exception: {ex.ToString()}");
            }
        }
        internal static void UpdateBusinessDetailsCache <T>(string key, T fpCacheModel, string rootAliasUri)
        {
            try
            {
                if (!String.IsNullOrEmpty(key) && isApiCacheEnabled)
                {
                    key = key.ToUpper();

                    var cacheObject = JsonConvert.SerializeObject(fpCacheModel);

                    var dnsHost = string.Empty;
                    try
                    {
                        dnsHost = new Uri(rootAliasUri).DnsSafeHost?.ToUpper();
                    }
                    catch { }

                    var cacheHashKey = HashUrl.GetHashAsString(dnsHost);
                    var cacheKey     = HashUrl.GetHashAsString(key);

                    var task = ApiCacheService.Save(cacheHashKey, cacheKey, cacheObject);
                    if (!task.Wait(_cacheTimeout))
                    {
                        ConsoleLogger.Write($"ERROR: CACHE UpdateBusinessDetailsCache - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: Unable to UpdateBusinessDetailsCache({key}) exception: {ex.ToString()}");
            }
        }
Beispiel #7
0
 /// <summary>
 /// Добавляем url и формируем его короткую версию.
 /// </summary>
 /// <param name="url">url-адресс.</param>
 /// <returns></returns>
 public async Task AddUrl(string url)
 {
     var hashUrl = new HashUrl();
     var result  = new StoreUrl()
     {
         LongUrl  = url,
         ShortUrl = hashUrl.Create(url),
         Created  = DateTime.Now,
         Count    = 0
     };
     await _repository.AddUrl(result);
 }
        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);
        }
        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);
        }
        internal static string GetThirdPartyAPIResponseFromCache(string apiendpoint, bool isCacheEnabled, string rootAliasuri)
        {
            try
            {
                if (isCacheEnabled && isApiCacheEnabled)
                {
                    var dnsHost = string.Empty;
                    try
                    {
                        dnsHost = new Uri(rootAliasuri).DnsSafeHost?.ToUpper();
                    }
                    catch { }

                    apiendpoint = apiendpoint.ToUpper();
                    var cacheKey     = HashUrl.GetHashAsString(apiendpoint);
                    var cacheHashKey = HashUrl.GetHashAsString(dnsHost);

                    var task = Task.Run(() => ApiCacheService.Get(cacheHashKey, cacheKey));
                    if (task.Wait(_cacheTimeout))
                    {
                        var response = task.Result;
                        if (!String.IsNullOrEmpty(response))
                        {
                            return(response);
                        }
                    }
                    else
                    {
                        ConsoleLogger.Write($"ERROR: CACHE GetThirdPartyAPIResponseFromCache - Redis TimeOut");
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: Unable to GetThirdPartyAPIResponseFromCache({apiendpoint}, {rootAliasuri}) exception: {ex.ToString()}");
            }

            return(null);
        }
        internal static T GetBusinessDetailsFromCache <T>(string fpTag, bool isCacheEnabled, string domainName, bool isNFSite, T businessClass, string schema = null, string developerId = null, string customerId = null, bool isEnterprise = false, string rootAliasUri = null)
        {
            try
            {
                fpTag = fpTag.ToUpper();

                if (isCacheEnabled && isApiCacheEnabled)
                {
                    string cacheServiceResponse = string.Empty;
                    try
                    {
                        try
                        {
                            var dnsHost = string.Empty;
                            try
                            {
                                dnsHost = new Uri(rootAliasUri).DnsSafeHost?.ToUpper();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }

                            var cacheKey     = HashUrl.GetHashAsString(fpTag);
                            var cacheHashKey = HashUrl.GetHashAsString(dnsHost);

                            var task = Task.Run(() => ApiCacheService.Get(GetMetaInfoCacheKey(cacheHashKey, cacheKey)));
                            if (task.Wait(_cacheTimeout))
                            {
                                cacheServiceResponse = task.Result;
                            }
                            else
                            {
                                ConsoleLogger.Write($"ERROR: CACHE GetExpression - Redis TimeOut");
                            }
                        }
                        catch (Exception ex)
                        {
                            ConsoleLogger.Write($"ERROR: Unable to GetBusinessDetailsFromCache() - Redis TimeOut");
                        }

                        if (!String.IsNullOrEmpty(cacheServiceResponse))
                        {
                            return(JsonConvert.DeserializeObject <T>(JsonConvert.DeserializeObject(cacheServiceResponse).ToString()));
                        }
                    }
                    catch (Exception ex) { throw ex; }
                }

                var temp = ApiHelper.GetBusinessDataFromNewAPI(fpTag, schema, customerId, developerId);

                if (isCacheEnabled && isApiCacheEnabled)
                {
                    try
                    {
                        UpdateBusinessDetailsCache(fpTag, temp, rootAliasUri);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                return(JsonConvert.DeserializeObject <T>(JsonConvert.SerializeObject(temp)));
            }
            catch (Exception ex)
            {
                ConsoleLogger.Write($"ERROR: CACHE GetBusinessDetailsFromCache - {ex.ToString()}");
            }

            return(JsonConvert.DeserializeObject <T>(null));
        }