Simplify configuration cache code

Don't throw an exception and log a warning if the hash has changed.

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-07-15 17:51:59 -07:00
parent baebaec05d
commit 348933ac1d

View File

@@ -26,20 +26,17 @@ namespace Orchard.Data {
public ILogger Logger { get; set; } public ILogger Logger { get; set; }
public Configuration GetConfiguration(Func<Configuration> builder) { public Configuration GetConfiguration(Func<Configuration> builder) {
var hash = ComputeHash(_shellBlueprint).Value; var hash = ComputeHash().Value;
// Return previous configuration if it exsists and has the same hash as // Return previous configuration if it exsists and has the same hash as
// the current blueprint. // the current blueprint.
var previousConfig = ReadConfiguration(_shellSettings.Name); var previousConfig = ReadConfiguration(hash);
if (previousConfig != null) { if (previousConfig != null) {
if (previousConfig.ShellName == _shellSettings.Name && previousConfig.Hash == hash) { return previousConfig.Configuration;
return previousConfig.Configuration;
}
} }
// Create cache and persist it // Create cache and persist it
var cache = new ConfigurationCache { var cache = new ConfigurationCache {
ShellName = _shellSettings.Name,
Hash = hash, Hash = hash,
Configuration = builder() Configuration = builder()
}; };
@@ -50,51 +47,75 @@ namespace Orchard.Data {
[Serializable] [Serializable]
public class ConfigurationCache { public class ConfigurationCache {
public string ShellName { get; set; }
public string Hash { get; set; } public string Hash { get; set; }
public Configuration Configuration { get; set; } public Configuration Configuration { get; set; }
} }
private void StoreConfiguration(ConfigurationCache cache) { private void StoreConfiguration(ConfigurationCache cache) {
var pathName = GetPathName(cache.ShellName); var pathName = GetPathName(_shellSettings.Name);
var formatter = new BinaryFormatter();
using (var stream = _appDataFolder.CreateFile(pathName)) { using (var stream = _appDataFolder.CreateFile(pathName)) {
new BinaryFormatter().Serialize(stream, cache); formatter.Serialize(stream, cache.Hash);
formatter.Serialize(stream, cache.Configuration);
} }
} }
private ConfigurationCache ReadConfiguration(string shellName) { private ConfigurationCache ReadConfiguration(string hash) {
var pathName = GetPathName(shellName); var pathName = GetPathName(_shellSettings.Name);
if (!_appDataFolder.FileExists(pathName)) if (!_appDataFolder.FileExists(pathName))
return null; return null;
try { try {
var formatter = new BinaryFormatter();
using (var stream = _appDataFolder.OpenFile(pathName)) { using (var stream = _appDataFolder.OpenFile(pathName)) {
return new BinaryFormatter().Deserialize(stream) as ConfigurationCache;
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.");
return null;
}
var oldConfig = (Configuration)formatter.Deserialize(stream);
return new ConfigurationCache {
Hash = oldHash,
Configuration = oldConfig
};
} }
} }
catch (Exception e) { catch (Exception e) {
for (var scan = e; scan != null; scan = scan.InnerException) for (var scan = e; scan != null; scan = scan.InnerException)
Logger.Warning("The cached NHibernate configuration cache can't be read: {0}", scan.Message); Logger.Warning("Error reading the cached NHibernate configuration: {0}", scan.Message);
Logger.Information("A new one will be re-generated.");
return null; return null;
} }
} }
private Hash ComputeHash(ShellBlueprint shellBlueprint) { private Hash ComputeHash() {
// We need to hash the assemnly names, record names and property names
var hash = new Hash(); var hash = new Hash();
foreach (var tableName in shellBlueprint.Records.Select(x => x.TableName)) { hash.AddString(_shellSettings.Name);
// We need to hash the assemnly names, record names and property names
foreach (var tableName in _shellBlueprint.Records.Select(x => x.TableName)) {
hash.AddString(tableName); hash.AddString(tableName);
} }
foreach (var recordType in shellBlueprint.Records.Select(x => x.Type)) { foreach (var recordType in _shellBlueprint.Records.Select(x => x.Type)) {
hash.AddTypeReference(recordType); hash.AddTypeReference(recordType);
if (recordType.BaseType != null)
hash.AddTypeReference(recordType.BaseType);
foreach (var property in recordType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public)) { foreach (var property in recordType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public)) {
hash.AddString(property.Name); hash.AddString(property.Name);
hash.AddTypeReference(property.PropertyType); hash.AddTypeReference(property.PropertyType);
foreach(var attr in property.GetCustomAttributesData()) {
hash.AddTypeReference(attr.Constructor.DeclaringType);
}
} }
} }