From 2f2f9e6dc37bfe9cc5c3f80fa1f668975fee4b7a Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Thu, 31 Mar 2011 18:04:43 -0700 Subject: [PATCH] Improving mapping stream read if hash has not changed --HG-- branch : 1.x --- .../Data/Migration/DataMigrationManager.cs | 2 +- src/Orchard/Data/SessionConfigurationCache.cs | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Orchard/Data/Migration/DataMigrationManager.cs b/src/Orchard/Data/Migration/DataMigrationManager.cs index 9ecce96c5..dc76d400c 100644 --- a/src/Orchard/Data/Migration/DataMigrationManager.cs +++ b/src/Orchard/Data/Migration/DataMigrationManager.cs @@ -74,7 +74,7 @@ namespace Orchard.Data.Migration { var migrations = GetDataMigrations(feature); - // apply update methods to each migration class for the module)))) + // apply update methods to each migration class for the module foreach ( var migration in migrations ) { // copy the objet for the Linq query var tempMigration = migration; diff --git a/src/Orchard/Data/SessionConfigurationCache.cs b/src/Orchard/Data/SessionConfigurationCache.cs index 00c529904..ba6df2451 100644 --- a/src/Orchard/Data/SessionConfigurationCache.cs +++ b/src/Orchard/Data/SessionConfigurationCache.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Reflection; +using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using NHibernate.Cfg; using Orchard.Environment; @@ -16,12 +17,14 @@ namespace Orchard.Data { private readonly ShellBlueprint _shellBlueprint; private readonly IAppDataFolder _appDataFolder; private readonly IHostEnvironment _hostEnvironment; + private ConfigurationCache _currentConfig; public SessionConfigurationCache(ShellSettings shellSettings, ShellBlueprint shellBlueprint, IAppDataFolder appDataFolder, IHostEnvironment hostEnvironment) { _shellSettings = shellSettings; _shellBlueprint = shellBlueprint; _appDataFolder = appDataFolder; _hostEnvironment = hostEnvironment; + _currentConfig = null; Logger = NullLogger.Instance; } @@ -31,21 +34,27 @@ namespace Orchard.Data { public Configuration GetConfiguration(Func builder) { var hash = ComputeHash().Value; - // Return previous configuration if it exsists and has the same hash as + // if the current configuration is unchanged, return it + if(_currentConfig != null && _currentConfig.Hash == hash) { + return _currentConfig.Configuration; + } + + // Return previous configuration if it exists and has the same hash as // the current blueprint. var previousConfig = ReadConfiguration(hash); if (previousConfig != null) { + _currentConfig = previousConfig; return previousConfig.Configuration; } // Create cache and persist it - var cache = new ConfigurationCache { + _currentConfig = new ConfigurationCache { Hash = hash, Configuration = builder() }; - StoreConfiguration(cache); - return cache.Configuration; + StoreConfiguration(_currentConfig); + return _currentConfig.Configuration; } private class ConfigurationCache { @@ -66,11 +75,11 @@ namespace Orchard.Data { formatter.Serialize(stream, cache.Configuration); } } - catch (Exception e) { + catch (SerializationException e) { //Note: This can happen when multiple processes/AppDomains try to save // the cached configuration at the same time. Only one concurrent // writer will win, and it's harmless for the other ones to fail. - for (var scan = e; scan != null; scan = scan.InnerException) + for (Exception scan = e; scan != null; scan = scan.InnerException) Logger.Warning("Error storing new NHibernate cache configuration: {0}", scan.Message); } } @@ -88,6 +97,11 @@ namespace Orchard.Data { var formatter = new BinaryFormatter(); using (var stream = _appDataFolder.OpenFile(pathName)) { + // if the stream is empty, stop here + if(stream.Length == 0) { + return null; + } + var oldHash = (string)formatter.Deserialize(stream); if (hash != oldHash) { Logger.Information("The cached NHibernate configuration is out of date. A new one will be re-generated.");