Invalidate mapping configuration when a shell is rebuilt

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-07-14 16:05:05 -07:00
parent d3a0dca89d
commit 236b743a04
3 changed files with 64 additions and 17 deletions

View File

@@ -1,7 +1,9 @@
using System.IO; using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using NHibernate; using NHibernate;
using NHibernate.Cfg; using NHibernate.Cfg;
using Orchard.Data;
using Orchard.Data.Providers; using Orchard.Data.Providers;
using Orchard.Environment.Configuration; using Orchard.Environment.Configuration;
using Orchard.Environment.ShellBuilders.Models; using Orchard.Environment.ShellBuilders.Models;
@@ -16,11 +18,57 @@ namespace Orchard.Data {
SessionFactoryParameters GetSessionFactoryParameters(); SessionFactoryParameters GetSessionFactoryParameters();
} }
public interface ISessionConfigurationCache {
void StoreConfig(string shellName, Configuration config);
void DeleteConfig(string shellName);
Configuration GetConfiguration(string shellName);
}
public class SessionConfigurationCache : ISessionConfigurationCache {
private readonly IAppDataFolder _appDataFolder;
public SessionConfigurationCache(IAppDataFolder appDataFolder) {
_appDataFolder = appDataFolder;
}
public void StoreConfig(string shellName, Configuration config) {
var filename = GetFileName(shellName);
using ( var stream = File.OpenWrite(filename) ) {
new BinaryFormatter().Serialize(stream, config);
}
}
public void DeleteConfig(string shellName) {
var filename = GetFileName(shellName);
if(File.Exists(filename)) {
File.Delete(filename);
}
}
public Configuration GetConfiguration(string shellName) {
var filename = GetFileName(shellName);
if (!_appDataFolder.FileExists(filename)) {
return null;
}
using (var stream = File.OpenRead(filename)) {
return new BinaryFormatter().Deserialize(stream) as Configuration;
}
}
private string GetFileName(string shellName) {
return _appDataFolder.MapPath(_appDataFolder.Combine("Sites", shellName, "mappings.bin"));
}
}
public class SessionFactoryHolder : ISessionFactoryHolder { public class SessionFactoryHolder : ISessionFactoryHolder {
private readonly ShellSettings _shellSettings; private readonly ShellSettings _shellSettings;
private readonly ShellBlueprint _shellBlueprint; private readonly ShellBlueprint _shellBlueprint;
private readonly IDataServicesProviderFactory _dataServicesProviderFactory; private readonly IDataServicesProviderFactory _dataServicesProviderFactory;
private readonly IAppDataFolder _appDataFolder; private readonly IAppDataFolder _appDataFolder;
private readonly ISessionConfigurationCache _sessionConfigurationCache;
private ISessionFactory _sessionFactory; private ISessionFactory _sessionFactory;
private Configuration _configuration; private Configuration _configuration;
@@ -29,11 +77,13 @@ namespace Orchard.Data {
ShellSettings shellSettings, ShellSettings shellSettings,
ShellBlueprint shellBlueprint, ShellBlueprint shellBlueprint,
IDataServicesProviderFactory dataServicesProviderFactory, IDataServicesProviderFactory dataServicesProviderFactory,
IAppDataFolder appDataFolder) { IAppDataFolder appDataFolder,
ISessionConfigurationCache sessionConfigurationCache) {
_shellSettings = shellSettings; _shellSettings = shellSettings;
_shellBlueprint = shellBlueprint; _shellBlueprint = shellBlueprint;
_dataServicesProviderFactory = dataServicesProviderFactory; _dataServicesProviderFactory = dataServicesProviderFactory;
_appDataFolder = appDataFolder; _appDataFolder = appDataFolder;
_sessionConfigurationCache = sessionConfigurationCache;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
@@ -70,26 +120,16 @@ namespace Orchard.Data {
private Configuration BuildConfiguration() { private Configuration BuildConfiguration() {
var parameters = GetSessionFactoryParameters(); var parameters = GetSessionFactoryParameters();
Configuration config = null; var config = _sessionConfigurationCache.GetConfiguration(_shellSettings.Name);
var bf = new BinaryFormatter();
var filename = _appDataFolder.MapPath(_appDataFolder.Combine("Sites", "mappings.bin")); if ( config == null ) {
if(_appDataFolder.FileExists(filename)) {
Logger.Debug("Loading mappings from cached file");
using ( var stream = File.OpenRead(filename) ) {
config = bf.Deserialize(stream) as Configuration;
}
}
else {
Logger.Debug("Generating mappings and cached file");
config = _dataServicesProviderFactory config = _dataServicesProviderFactory
.CreateProvider(parameters) .CreateProvider(parameters)
.BuildConfiguration(parameters); .BuildConfiguration(parameters);
using ( var stream = File.OpenWrite(filename) ) { _sessionConfigurationCache.StoreConfig(_shellSettings.Name, config);
bf.Serialize(stream, config);
}
} }
return config; return config;
} }

View File

@@ -6,6 +6,7 @@ using System.Web.Hosting;
using Autofac; using Autofac;
using Autofac.Configuration; using Autofac.Configuration;
using Orchard.Caching; using Orchard.Caching;
using Orchard.Data;
using Orchard.Environment.AutofacUtil; using Orchard.Environment.AutofacUtil;
using Orchard.Environment.Configuration; using Orchard.Environment.Configuration;
using Orchard.Environment.Extensions; using Orchard.Environment.Extensions;
@@ -52,6 +53,7 @@ namespace Orchard.Environment {
builder.RegisterType<DefaultOrchardHost>().As<IOrchardHost>().As<IEventHandler>().SingleInstance(); builder.RegisterType<DefaultOrchardHost>().As<IOrchardHost>().As<IEventHandler>().SingleInstance();
{ {
builder.RegisterType<ShellSettingsManager>().As<IShellSettingsManager>().SingleInstance(); builder.RegisterType<ShellSettingsManager>().As<IShellSettingsManager>().SingleInstance();
builder.RegisterType<SessionConfigurationCache>().As<ISessionConfigurationCache>().SingleInstance();
builder.RegisterType<ShellContextFactory>().As<IShellContextFactory>().SingleInstance(); builder.RegisterType<ShellContextFactory>().As<IShellContextFactory>().SingleInstance();
{ {

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using Autofac; using Autofac;
using Orchard.Data;
using Orchard.Environment.Configuration; using Orchard.Environment.Configuration;
using Orchard.Environment.Descriptor; using Orchard.Environment.Descriptor;
using Orchard.Environment.Descriptor.Models; using Orchard.Environment.Descriptor.Models;
@@ -35,14 +36,17 @@ namespace Orchard.Environment.ShellBuilders {
private readonly IShellDescriptorCache _shellDescriptorCache; private readonly IShellDescriptorCache _shellDescriptorCache;
private readonly ICompositionStrategy _compositionStrategy; private readonly ICompositionStrategy _compositionStrategy;
private readonly IShellContainerFactory _shellContainerFactory; private readonly IShellContainerFactory _shellContainerFactory;
private readonly ISessionConfigurationCache _sessionConfigurationCache;
public ShellContextFactory( public ShellContextFactory(
IShellDescriptorCache shellDescriptorCache, IShellDescriptorCache shellDescriptorCache,
ICompositionStrategy compositionStrategy, ICompositionStrategy compositionStrategy,
IShellContainerFactory shellContainerFactory) { IShellContainerFactory shellContainerFactory,
ISessionConfigurationCache sessionConfigurationCache) {
_shellDescriptorCache = shellDescriptorCache; _shellDescriptorCache = shellDescriptorCache;
_compositionStrategy = compositionStrategy; _compositionStrategy = compositionStrategy;
_shellContainerFactory = shellContainerFactory; _shellContainerFactory = shellContainerFactory;
_sessionConfigurationCache = sessionConfigurationCache;
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
} }
@@ -70,6 +74,7 @@ namespace Orchard.Environment.ShellBuilders {
if (currentDescriptor != null && knownDescriptor.SerialNumber != currentDescriptor.SerialNumber) { if (currentDescriptor != null && knownDescriptor.SerialNumber != currentDescriptor.SerialNumber) {
Logger.Information("Newer descriptor obtained. Rebuilding shell container."); Logger.Information("Newer descriptor obtained. Rebuilding shell container.");
_sessionConfigurationCache.DeleteConfig(settings.Name);
_shellDescriptorCache.Store(settings.Name, currentDescriptor); _shellDescriptorCache.Store(settings.Name, currentDescriptor);
blueprint = _compositionStrategy.Compose(settings, currentDescriptor); blueprint = _compositionStrategy.Compose(settings, currentDescriptor);
shellScope = _shellContainerFactory.CreateContainer(settings, blueprint); shellScope = _shellContainerFactory.CreateContainer(settings, blueprint);