2013-08-28 21:46:14 +02:00
|
|
|
|
using Microsoft.ApplicationServer.Caching;
|
|
|
|
|
|
using Orchard.Azure.Services.Environment.Configuration;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Azure.Services.Caching {
|
|
|
|
|
|
|
|
|
|
|
|
public class CacheClientConfiguration {
|
|
|
|
|
|
|
2014-03-07 02:03:40 +01:00
|
|
|
|
public static CacheClientConfiguration FromPlatformConfiguration(string tenant, string settingNamePrefix, IPlatformConfigurationAccessor pca) {
|
2013-08-30 12:50:39 -07:00
|
|
|
|
return new CacheClientConfiguration {
|
2014-03-07 02:03:40 +01:00
|
|
|
|
HostIdentifier = pca.GetSetting(Constants.CacheHostIdentifierSettingName, tenant, settingNamePrefix),
|
|
|
|
|
|
CacheName = pca.GetSetting(Constants.CacheCacheNameSettingName, tenant, settingNamePrefix),
|
|
|
|
|
|
AuthorizationToken = pca.GetSetting(Constants.CacheAuthorizationTokenSettingName, tenant, settingNamePrefix),
|
2013-08-28 21:46:14 +02:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string HostIdentifier {
|
|
|
|
|
|
get;
|
|
|
|
|
|
protected set;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string CacheName {
|
|
|
|
|
|
get;
|
|
|
|
|
|
protected set;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string AuthorizationToken {
|
|
|
|
|
|
get;
|
|
|
|
|
|
protected set;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool CompressionIsEnabled {
|
|
|
|
|
|
get;
|
2013-08-31 13:52:06 +02:00
|
|
|
|
set;
|
2013-08-28 21:46:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Validate() {
|
2013-09-04 21:57:48 +02:00
|
|
|
|
if (String.IsNullOrWhiteSpace(HostIdentifier)) {
|
|
|
|
|
|
throw new Exception("The HostIdentifier value is missing or empty.");
|
2013-08-28 21:46:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DataCache CreateCache() {
|
2013-08-30 12:50:39 -07:00
|
|
|
|
var dataCacheFactoryConfiguration = new DataCacheFactoryConfiguration {
|
2013-08-28 21:46:14 +02:00
|
|
|
|
MaxConnectionsToServer = 32,
|
|
|
|
|
|
UseLegacyProtocol = false,
|
|
|
|
|
|
IsCompressionEnabled = CompressionIsEnabled
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2013-09-04 21:57:48 +02:00
|
|
|
|
dataCacheFactoryConfiguration.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, HostIdentifier);
|
|
|
|
|
|
if (!String.IsNullOrEmpty(AuthorizationToken))
|
|
|
|
|
|
dataCacheFactoryConfiguration.SecurityProperties = new DataCacheSecurity(AuthorizationToken, sslEnabled: false);
|
2013-08-28 21:46:14 +02:00
|
|
|
|
|
|
|
|
|
|
var dataCacheFactory = new DataCacheFactory(dataCacheFactoryConfiguration);
|
|
|
|
|
|
|
2013-09-04 21:57:48 +02:00
|
|
|
|
if (!String.IsNullOrEmpty(CacheName)) {
|
|
|
|
|
|
return dataCacheFactory.GetCache(CacheName);
|
2013-08-30 12:50:39 -07:00
|
|
|
|
}
|
2013-08-28 21:46:14 +02:00
|
|
|
|
|
2013-09-04 21:57:48 +02:00
|
|
|
|
return dataCacheFactory.GetDefaultCache();
|
2013-08-28 21:46:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-09-04 21:57:48 +02:00
|
|
|
|
//public override string ToString() {
|
|
|
|
|
|
// var key = HostIdentifier + "_" + CacheName + "_" + Hostname + "_" + Port + "_" + AuthorizationToken + "_" + IsSharedCaching + "_" + CompressionIsEnabled;
|
|
|
|
|
|
// return key;
|
|
|
|
|
|
//}
|
2013-08-28 21:46:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|