|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See the License.txt file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Microsoft.AspNet.OutputCache.CosmosDBTableAsyncOutputCacheProvider { |
| 5 | + using System; |
| 6 | + using System.Collections.Specialized; |
| 7 | + using System.Configuration; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using System.Web; |
| 10 | + using Microsoft.Azure.CosmosDB.Table; |
| 11 | + using Microsoft.Azure.Storage; |
| 12 | + using Resource; |
| 13 | + |
| 14 | + class CosmosDBTableOutputCacheRepository : ITableOutputCacheRepository { |
| 15 | + private const string TableNameKey = "tableName"; |
| 16 | + private const string ConnectionStringKey = "connectionStringName"; |
| 17 | + private const string FixedPartitionKey = "P"; |
| 18 | + |
| 19 | + private CloudTable _table; |
| 20 | + private string _connectionString; |
| 21 | + private string _tableName; |
| 22 | + private object _lock = new object(); |
| 23 | + |
| 24 | + public CosmosDBTableOutputCacheRepository(NameValueCollection providerConfig, NameValueCollection appSettings) { |
| 25 | + var connectionStringName = providerConfig[ConnectionStringKey]; |
| 26 | + if (string.IsNullOrEmpty(connectionStringName)) { |
| 27 | + throw new ConfigurationErrorsException(SR.Cant_find_connectionStringName); |
| 28 | + } |
| 29 | + |
| 30 | + _connectionString = appSettings[connectionStringName]; |
| 31 | + if (string.IsNullOrEmpty(_connectionString)) { |
| 32 | + throw new ConfigurationErrorsException(string.Format(SR.Cant_find_connectionString, connectionStringName)); |
| 33 | + } |
| 34 | + |
| 35 | + _tableName = providerConfig[TableNameKey]; |
| 36 | + if (string.IsNullOrEmpty(_tableName)) { |
| 37 | + throw new ConfigurationErrorsException(SR.TableName_cant_be_empty); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public object Add(string key, object entry, DateTime utcExpiry) { |
| 42 | + var retrieveOp = TableOperationHelper.Retrieve(key); |
| 43 | + var retrieveResult = _table.Execute(retrieveOp); |
| 44 | + var existingCacheEntry = retrieveResult.Result as CacheEntity; |
| 45 | + |
| 46 | + if (existingCacheEntry != null && existingCacheEntry.UtcExpiry > DateTime.UtcNow) { |
| 47 | + return existingCacheEntry.CacheItem; |
| 48 | + } else { |
| 49 | + Set(key, entry, utcExpiry); |
| 50 | + return entry; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public async Task<object> AddAsync(string key, object entry, DateTime utcExpiry) { |
| 55 | + // If there is already a value in the cache for the specified key, the provider must return that value if not expired |
| 56 | + // and must not store the data passed by using the Add method parameters. |
| 57 | + var retrieveOp = TableOperationHelper.Retrieve(key); |
| 58 | + var retrieveResult = await _table.ExecuteAsync(retrieveOp); |
| 59 | + var existingCacheEntry = retrieveResult.Result as CacheEntity; |
| 60 | + |
| 61 | + if (existingCacheEntry != null && existingCacheEntry.UtcExpiry > DateTime.UtcNow) { |
| 62 | + return existingCacheEntry.CacheItem; |
| 63 | + } else { |
| 64 | + await SetAsync(key, entry, utcExpiry); |
| 65 | + return entry; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public object Get(string key) { |
| 70 | + var retrieveOp = TableOperationHelper.Retrieve(key); |
| 71 | + var retrieveResult = _table.Execute(retrieveOp); |
| 72 | + var existingCacheEntry = retrieveResult.Result as CacheEntity; |
| 73 | + |
| 74 | + if (existingCacheEntry != null && existingCacheEntry.UtcExpiry < DateTime.UtcNow) { |
| 75 | + Remove(key); |
| 76 | + return null; |
| 77 | + } else { |
| 78 | + return existingCacheEntry?.CacheItem; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public async Task<object> GetAsync(string key) { |
| 83 | + // Outputcache module will always first call GetAsync |
| 84 | + // so only calling EnsureTableInitializedAsync here is good enough |
| 85 | + await EnsureTableInitializedAsync(); |
| 86 | + |
| 87 | + var retrieveOp = TableOperationHelper.Retrieve(key); |
| 88 | + var retrieveResult = await _table.ExecuteAsync(retrieveOp); |
| 89 | + var existingCacheEntry = retrieveResult.Result as CacheEntity; |
| 90 | + |
| 91 | + if (existingCacheEntry != null && existingCacheEntry.UtcExpiry < DateTime.UtcNow) { |
| 92 | + await RemoveAsync(key); |
| 93 | + return null; |
| 94 | + } else { |
| 95 | + return existingCacheEntry?.CacheItem; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public void Remove(string key) { |
| 100 | + var removeOp = TableOperationHelper.Delete(key); |
| 101 | + _table.Execute(removeOp); |
| 102 | + } |
| 103 | + |
| 104 | + public async Task RemoveAsync(string key) { |
| 105 | + var removeOp = TableOperationHelper.Delete(key); |
| 106 | + await _table.ExecuteAsync(removeOp); |
| 107 | + } |
| 108 | + |
| 109 | + public void Set(string key, object entry, DateTime utcExpiry) { |
| 110 | + var insertOp = TableOperationHelper.InsertOrReplace(key, entry, utcExpiry); |
| 111 | + _table.Execute(insertOp); |
| 112 | + } |
| 113 | + |
| 114 | + public async Task SetAsync(string key, object entry, DateTime utcExpiry) { |
| 115 | + //Check if the key is already in database |
| 116 | + //If there is already a value in the cache for the specified key, the Set method will update it. |
| 117 | + //Otherwise it will insert the entry. |
| 118 | + var insertOp = TableOperationHelper.InsertOrReplace(key, entry, utcExpiry); |
| 119 | + await _table.ExecuteAsync(insertOp); |
| 120 | + } |
| 121 | + |
| 122 | + private async Task EnsureTableInitializedAsync() { |
| 123 | + if (_table != null) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + try { |
| 128 | + lock (_lock) { |
| 129 | + if (_table != null) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + var storageAccount = CreateStorageAccount(); |
| 134 | + var tableClient = storageAccount.CreateCloudTableClient(); |
| 135 | + _table = tableClient.GetTableReference(_tableName); |
| 136 | + } |
| 137 | + |
| 138 | + // The sync version API causes deadlock when using CosmosDB table. |
| 139 | + await _table.CreateIfNotExistsAsync(); |
| 140 | + } catch (StorageException ex) { |
| 141 | + throw new HttpException(SR.Fail_to_create_table, ex); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private CloudStorageAccount CreateStorageAccount() { |
| 146 | + try { |
| 147 | + return CloudStorageAccount.Parse(_connectionString); |
| 148 | + } catch (FormatException) { |
| 149 | + throw new HttpException(SR.Invalid_storage_account_information); |
| 150 | + } catch (ArgumentException) { |
| 151 | + throw new HttpException(SR.Invalid_storage_account_information); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments