Пример #1
0
 internal static void InvalidateSettingsCache(string accountNameKey)
 {
     try
     {
         //Switch to Application specific redis server?
         //IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.AccountManager_Multiplexer.GetDatabase();
         IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.RedisMultiplexer.GetDatabase();
         cache.HashDelete(AccountSettingsHash.Key(), AccountSettingsHash.Fields.Document(accountNameKey));
     }
     catch
     {
     }
 }
        public static AccountSettingsDocumentModel GetAccountSettings(Account account, bool useCachedVersion = true)
        {
            AccountSettingsDocumentModel settingsDocument = null;

            #region Create Cache & HashField

            string redisHashField = string.Empty;

            //IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.AccountManager_Multiplexer.GetDatabase();
            IDatabase cache = Sahara.Core.Settings.Azure.Redis.RedisMultiplexers.RedisMultiplexer.GetDatabase();

            redisHashField = AccountSettingsHash.Fields.Document(account.AccountNameKey);


            #endregion

            if (useCachedVersion)
            {
                #region Get settings from cache

                try
                {
                    var redisValue = cache.HashGet(AccountSettingsHash.Key(), redisHashField);
                    if (redisValue.HasValue)
                    {
                        settingsDocument = JsonConvert.DeserializeObject <AccountSettingsDocumentModel>(redisValue);
                    }
                }
                catch
                {
                }

                #endregion
            }

            if (settingsDocument == null)
            {
                #region Get settings from DocumentDB

                /**/
                //Get the DocumentDB Client
                //var client = Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient;
                //var dbSelfLink = Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseSelfLink;
                //Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient.OpenAsync();

                //Build a collection Uri out of the known IDs
                //(These helpers allow you to properly generate the following URI format for Document DB:
                //"dbs/{xxx}/colls/{xxx}/docs/{xxx}"
                Uri collectionUri = UriFactory.CreateDocumentCollectionUri(Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseId, account.DocumentPartition);

                string sqlQuery = "SELECT * FROM Settings s WHERE s.id ='AccountSettings'";

                var settingsResults = Sahara.Core.Settings.Azure.DocumentDbClients.AccountDocumentClient.CreateDocumentQuery <AccountSettingsDocumentModel>(collectionUri.ToString(), sqlQuery);

                //var accountCollection = client.Crea

                //applicationImages = result.ToList();
                settingsDocument = settingsResults.AsEnumerable().FirstOrDefault();


                #endregion

                #region Get settings from storage partition (Retired, now using DocumentDB)

                /*
                 *
                 * //Get storage partition for this account and create a BLOB client:
                 * CloudBlobClient blobClient = Sahara.Core.Settings.Azure.Storage.GetStoragePartitionAccount(account.StoragePartition).CreateCloudBlobClient();
                 *
                 * //Create and set retry policy
                 * IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromMilliseconds(500), 8);
                 * blobClient.DefaultRequestOptions.RetryPolicy = exponentialRetryPolicy;
                 *
                 * //CloudBlobContainer blobContainer = blobClient.GetContainerReference(account.AccountID.ToString());
                 * CloudBlobContainer blobContainer = blobClient.GetContainerReference(account.AccountNameKey);
                 *
                 *
                 * //Get reference to the text blob or create if not exists.
                 * CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference("settings/" + "accountSettings.json");
                 *
                 * //Deserialize new version of settings document
                 * settingsDocument = JsonConvert.DeserializeObject<AccountSettingsDocumentModel>(blockBlob.DownloadText());
                 */

                #endregion

                if (settingsDocument != null)
                {
                    #region Set into cache

                    try
                    {
                        cache.HashSet(AccountSettingsHash.Key(),
                                      AccountSettingsHash.Fields.Document(redisHashField),
                                      JsonConvert.SerializeObject(settingsDocument), When.Always, CommandFlags.FireAndForget);
                    }
                    catch
                    {
                    }

                    #endregion
                }
            }

            return(settingsDocument);
        }