Releasing DataCacheFactory on retries

This commit is contained in:
Sebastien Ros
2014-04-21 15:51:17 -07:00
parent 8346a08f95
commit 5f0df3ea38
3 changed files with 17 additions and 17 deletions

View File

@@ -40,9 +40,8 @@ namespace Orchard.Azure.Services.Caching {
} }
} }
public DataCache CreateCache() { public DataCacheFactory CreateCache() {
var dataCacheFactoryConfiguration = new DataCacheFactoryConfiguration { var dataCacheFactoryConfiguration = new DataCacheFactoryConfiguration {
MaxConnectionsToServer = 32,
UseLegacyProtocol = false, UseLegacyProtocol = false,
IsCompressionEnabled = CompressionIsEnabled IsCompressionEnabled = CompressionIsEnabled
}; };
@@ -51,13 +50,9 @@ namespace Orchard.Azure.Services.Caching {
if (!String.IsNullOrEmpty(AuthorizationToken)) if (!String.IsNullOrEmpty(AuthorizationToken))
dataCacheFactoryConfiguration.SecurityProperties = new DataCacheSecurity(AuthorizationToken, sslEnabled: false); dataCacheFactoryConfiguration.SecurityProperties = new DataCacheSecurity(AuthorizationToken, sslEnabled: false);
var dataCacheFactory = new DataCacheFactory(dataCacheFactoryConfiguration); return new DataCacheFactory(dataCacheFactoryConfiguration);
if (!String.IsNullOrEmpty(CacheName)) {
return dataCacheFactory.GetCache(CacheName);
}
return dataCacheFactory.GetDefaultCache();
} }
public override int GetHashCode() { public override int GetHashCode() {

View File

@@ -9,6 +9,7 @@ namespace Orchard.Azure.Services.Caching.Database {
public class AzureCacheProvider : ICacheProvider { public class AzureCacheProvider : ICacheProvider {
private DataCache _dataCache; private DataCache _dataCache;
private DataCacheFactory _dataCacheFactory;
public ICache BuildCache(string regionName, IDictionary<string, string> properties) { public ICache BuildCache(string regionName, IDictionary<string, string> 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); 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() { public void Stop() {
_dataCache = null; _dataCache = null;
_dataCacheFactory.Dispose();
} }
} }
} }

View File

@@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Microsoft.ApplicationServer.Caching; using Microsoft.ApplicationServer.Caching;
@@ -23,7 +22,7 @@ namespace Orchard.Azure.Services.Caching.Output {
public const int Retries = 2; public const int Retries = 2;
private CacheClientConfiguration _cacheClientConfiguration; private CacheClientConfiguration _cacheClientConfiguration;
private static ConcurrentDictionary<CacheClientConfiguration, DataCache> _dataCaches = new ConcurrentDictionary<CacheClientConfiguration, DataCache>(); private static ConcurrentDictionary<CacheClientConfiguration, DataCacheFactory> _dataCacheFactories = new ConcurrentDictionary<CacheClientConfiguration, DataCacheFactory>();
private static ConcurrentBag<string> _regions = new ConcurrentBag<string>(); private static ConcurrentBag<string> _regions = new ConcurrentBag<string>();
private readonly string _regionAlphaNumeric; private readonly string _regionAlphaNumeric;
@@ -84,11 +83,12 @@ namespace Orchard.Azure.Services.Caching.Output {
public DataCache Cache { public DataCache Cache {
get { get {
var cache = _dataCaches.GetOrAdd(CacheConfiguration, cfg => { var cacheFactory = _dataCacheFactories.GetOrAdd(CacheConfiguration, cfg => {
Logger.Debug("Creating a new cache client ({0})", CacheConfiguration.GetHashCode()); Logger.Debug("Creating a new cache client ({0})", CacheConfiguration.GetHashCode());
return cfg.CreateCache(); 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 // creating a region uses a network call, try to optimise it
if (!_regions.Contains(_regionAlphaNumeric)) { if (!_regions.Contains(_regionAlphaNumeric)) {
@@ -122,9 +122,11 @@ namespace Orchard.Azure.Services.Caching.Output {
return function.Invoke(); return function.Invoke();
} }
catch (DataCacheException) { catch (DataCacheException) {
DataCache cache; DataCacheFactory cacheFactory;
Logger.Debug("Retrying cache operation"); Logger.Debug("Retrying cache operation");
_dataCaches.TryRemove(CacheConfiguration, out cache); if (_dataCacheFactories.TryRemove(CacheConfiguration, out cacheFactory)) {
cacheFactory.Dispose();
}
return Retry(function, times--); return Retry(function, times--);
} }
} }