Ejemplo n.º 1
0
        public static Account GetAccountObject(string accountNameKey)
        {
            Account account = null;

            #region (Plan A) Get Account from Local Cache

            bool   localCacheEmpty = false;
            string localCacheKey   = accountNameKey + ":account";

            try
            {
                account = (Account)HttpRuntime.Cache[localCacheKey];
            }
            catch (Exception e)
            {
                var error = e.Message;
                //TODO: Log: error message for local cache call
            }

            #endregion

            if (account == null)
            {
                localCacheEmpty = true;

                #region (Plan B) Get Account from Redis Cache

                try
                {
                    //First we attempt to get the user from the Redis Cache

                    IDatabase cache = CoreServices.RedisConnectionMultiplexers.RedisMultiplexer.GetDatabase();

                    string hashKey   = "accountbyname:" + accountNameKey;
                    string hashField = "model";

                    try
                    {
                        var redisValue = cache.HashGet(hashKey, hashField);

                        if (redisValue.HasValue)
                        {
                            account = JsonConvert.DeserializeObject <Account>(redisValue);
                        }
                    }
                    catch
                    {
                    }
                }
                catch (Exception e)
                {
                    var error = e.Message;
                }

                #endregion
            }
            if (account == null)
            {
                #region (Plan C) Get Account from WCF

                //If a failure occurs, or the redis cache is empty we get the user from the WCF service
                var accountManagementServiceClient = new AccountManagementService.AccountManagementServiceClient();

                try
                {
                    accountManagementServiceClient.Open();
                    account = accountManagementServiceClient.GetAccount(accountNameKey, Common.SharedClientKey);

                    //Close the connection
                    WCFManager.CloseConnection(accountManagementServiceClient);
                }
                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(accountManagementServiceClient, exceptionMessage, currentMethodString);

                    #endregion
                }

                #endregion
            }

            if (localCacheEmpty)
            {
                #region store Account into local cache

                //store string in local cache for a few moments:
                HttpRuntime.Cache.Insert(localCacheKey, account, null, DateTime.Now.AddSeconds(Common.LocalAccountCacheTimeInSeconds), TimeSpan.Zero);

                #endregion
            }

            return(account);
        }