Making azure db cache provider tenant aware again

ICacheProvider calls Start() and provides the region prefix as a
property, which lets us create a dedicated DataCache.

The BuildCache method is called once per record, and reuse by
Nhibernate.
This commit is contained in:
Sebastien Ros 2013-08-30 15:42:11 -07:00
parent 6ca9b41fab
commit 1ac989ee72
4 changed files with 33 additions and 31 deletions

View File

@ -6,15 +6,15 @@ namespace Orchard.Azure.Services.Caching {
public class CacheClientConfiguration {
public static CacheClientConfiguration FromPlatformConfiguration(string settingNamePrefix) {
var portString = PlatformConfiguration.GetSetting(Constants.CachePortSettingName, settingNamePrefix);
var isSharedCachingString = PlatformConfiguration.GetSetting(Constants.CacheIsSharedCachingSettingName, settingNamePrefix);
public static CacheClientConfiguration FromPlatformConfiguration(string tenant, string settingNamePrefix) {
var portString = PlatformConfiguration.GetSetting(Constants.CachePortSettingName, tenant, settingNamePrefix);
var isSharedCachingString = PlatformConfiguration.GetSetting(Constants.CacheIsSharedCachingSettingName, tenant, settingNamePrefix);
return new CacheClientConfiguration {
HostIdentifier = PlatformConfiguration.GetSetting(Constants.CacheHostIdentifierSettingName, settingNamePrefix),
CacheName = PlatformConfiguration.GetSetting(Constants.CacheCacheNameSettingName, settingNamePrefix),
Hostname = PlatformConfiguration.GetSetting(Constants.CacheHostnameSettingName, settingNamePrefix),
HostIdentifier = PlatformConfiguration.GetSetting(Constants.CacheHostIdentifierSettingName, tenant, settingNamePrefix),
CacheName = PlatformConfiguration.GetSetting(Constants.CacheCacheNameSettingName, tenant, settingNamePrefix),
Hostname = PlatformConfiguration.GetSetting(Constants.CacheHostnameSettingName, tenant, settingNamePrefix),
Port = String.IsNullOrWhiteSpace(portString) ? 0 : Int32.Parse(portString),
AuthorizationToken = PlatformConfiguration.GetSetting(Constants.CacheAuthorizationTokenSettingName, settingNamePrefix),
AuthorizationToken = PlatformConfiguration.GetSetting(Constants.CacheAuthorizationTokenSettingName, tenant, settingNamePrefix),
IsSharedCaching = !String.IsNullOrWhiteSpace(isSharedCachingString) && Boolean.Parse(isSharedCachingString)
};
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.ApplicationServer.Caching;
using NHibernate.Cache;
using System;
@ -6,29 +7,26 @@ namespace Orchard.Azure.Services.Caching.Database {
public class AzureCacheProvider : ICacheProvider {
public ICache BuildCache(string regionName, IDictionary<string, string> properties) {
CacheClientConfiguration configuration;
try {
configuration = CacheClientConfiguration.FromPlatformConfiguration(Constants.DatabaseCacheSettingNamePrefix);
configuration.Validate();
}
catch (Exception ex) {
throw new Exception(String.Format("The {0} configuration settings are missing or invalid.", Constants.DatabaseCacheFeatureName), ex);
}
private DataCache _dataCache;
private bool _sharedCaching;
public ICache BuildCache(string regionName, IDictionary<string, string> properties) {
if (_dataCache == null) {
throw new ApplicationException("DataCache should be available");
}
string enableCompressionString;
properties.TryGetValue("compression_enabled", out enableCompressionString);
var cache = configuration.CreateCache();
TimeSpan? expiration = null;
string expirationString;
if (properties.TryGetValue("expiration", out expirationString) || properties.TryGetValue(NHibernate.Cfg.Environment.CacheDefaultExpiration, out expirationString)) {
expiration = TimeSpan.FromSeconds(Int32.Parse(expirationString));
}
return new AzureCacheClient(cache, configuration.IsSharedCaching, regionName, expiration);
return new AzureCacheClient(_dataCache, _sharedCaching, regionName, expiration);
}
public long NextTimestamp() {
@ -36,6 +34,20 @@ namespace Orchard.Azure.Services.Caching.Database {
}
public void Start(IDictionary<string, string> properties) {
CacheClientConfiguration configuration;
try {
var tenant = properties["region_prefix"];
configuration = CacheClientConfiguration.FromPlatformConfiguration(tenant, Constants.DatabaseCacheSettingNamePrefix);
configuration.Validate();
}
catch (Exception ex) {
throw new Exception(String.Format("The {0} configuration settings are missing or invalid.", Constants.DatabaseCacheFeatureName), ex);
}
_dataCache = configuration.CreateCache();
_sharedCaching = configuration.IsSharedCaching;
}
public void Stop() {

View File

@ -17,7 +17,7 @@ namespace Orchard.Azure.Services.Caching.Output {
public AzureOutputCacheStorageProvider(ShellSettings shellSettings) {
try {
_cacheConfig = CacheClientConfiguration.FromPlatformConfiguration(Constants.OutputCacheSettingNamePrefix);
_cacheConfig = CacheClientConfiguration.FromPlatformConfiguration(shellSettings.Name, Constants.OutputCacheSettingNamePrefix);
_cacheConfig.Validate();
}
catch (Exception ex) {

View File

@ -17,15 +17,5 @@ namespace Orchard.Azure.Services.Environment.Configuration {
var fallbackName = namePrefix + name;
return CloudConfigurationManager.GetSetting(tenantName) ?? CloudConfigurationManager.GetSetting(fallbackName);
}
/// <summary>
/// Reads a setting from platform configuration
/// </summary>
/// <param name="name">The name of the setting to read.</param>
/// <param name="namePrefix">An optional prefix to prepend the setting name with.</param>
/// <returns>The value of the setting if found with or without tenant name prefix, otherwise null.</returns>
public static string GetSetting(string name, string namePrefix = null) {
return CloudConfigurationManager.GetSetting(namePrefix + name);
}
}
}