diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs index 18a5f397b..4290d3cc3 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs @@ -40,9 +40,8 @@ namespace Orchard.Azure.Services.Caching { } } - public DataCache CreateCache() { + public DataCacheFactory CreateCache() { var dataCacheFactoryConfiguration = new DataCacheFactoryConfiguration { - MaxConnectionsToServer = 32, UseLegacyProtocol = false, IsCompressionEnabled = CompressionIsEnabled }; @@ -51,13 +50,9 @@ namespace Orchard.Azure.Services.Caching { if (!String.IsNullOrEmpty(AuthorizationToken)) dataCacheFactoryConfiguration.SecurityProperties = new DataCacheSecurity(AuthorizationToken, sslEnabled: false); - var dataCacheFactory = new DataCacheFactory(dataCacheFactoryConfiguration); - - if (!String.IsNullOrEmpty(CacheName)) { - return dataCacheFactory.GetCache(CacheName); - } - - return dataCacheFactory.GetDefaultCache(); + return new DataCacheFactory(dataCacheFactoryConfiguration); + + } public override int GetHashCode() { diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs index ee5b97865..ae83a2737 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs @@ -9,6 +9,7 @@ namespace Orchard.Azure.Services.Caching.Database { public class AzureCacheProvider : ICacheProvider { private DataCache _dataCache; + private DataCacheFactory _dataCacheFactory; public ICache BuildCache(string regionName, IDictionary properties) { @@ -49,11 +50,13 @@ namespace Orchard.Azure.Services.Caching.Database { throw new Exception(String.Format("The {0} configuration settings are missing or invalid.", Constants.DatabaseCacheFeatureName), ex); } - _dataCache = configuration.CreateCache(); + _dataCacheFactory = configuration.CreateCache(); + _dataCache = _dataCacheFactory.GetDefaultCache(); } public void Stop() { _dataCache = null; + _dataCacheFactory.Dispose(); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs index d0a8e1e68..542d4b769 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics; using System.Globalization; using System.Linq; using Microsoft.ApplicationServer.Caching; @@ -23,7 +22,7 @@ namespace Orchard.Azure.Services.Caching.Output { public const int Retries = 2; private CacheClientConfiguration _cacheClientConfiguration; - private static ConcurrentDictionary _dataCaches = new ConcurrentDictionary(); + private static ConcurrentDictionary _dataCacheFactories = new ConcurrentDictionary(); private static ConcurrentBag _regions = new ConcurrentBag(); private readonly string _regionAlphaNumeric; @@ -84,12 +83,13 @@ namespace Orchard.Azure.Services.Caching.Output { public DataCache Cache { get { - var cache = _dataCaches.GetOrAdd(CacheConfiguration, cfg => { + var cacheFactory = _dataCacheFactories.GetOrAdd(CacheConfiguration, cfg => { Logger.Debug("Creating a new cache client ({0})", CacheConfiguration.GetHashCode()); return cfg.CreateCache(); }); - - + + var cache = String.IsNullOrEmpty(CacheConfiguration.CacheName) ? cacheFactory.GetDefaultCache() : cacheFactory.GetCache(CacheConfiguration.CacheName); + // creating a region uses a network call, try to optimise it if (!_regions.Contains(_regionAlphaNumeric)) { Logger.Debug("Creating a new region: {0}", _regionAlphaNumeric); @@ -122,9 +122,11 @@ namespace Orchard.Azure.Services.Caching.Output { return function.Invoke(); } catch (DataCacheException) { - DataCache cache; + DataCacheFactory cacheFactory; Logger.Debug("Retrying cache operation"); - _dataCaches.TryRemove(CacheConfiguration, out cache); + if (_dataCacheFactories.TryRemove(CacheConfiguration, out cacheFactory)) { + cacheFactory.Dispose(); + } return Retry(function, times--); } }