Fixes hash generation and checking with the trees.config file - since this is changed during startup our plugin cache was never active :(

This commit is contained in:
Shannon
2013-11-21 14:45:52 +11:00
parent 474dcd0754
commit e6ae88c016
2 changed files with 32 additions and 15 deletions

View File

@@ -171,17 +171,18 @@ namespace Umbraco.Core
if (_currentAssembliesHash != -1)
return _currentAssembliesHash;
_currentAssembliesHash = GetAssembliesHash(
new FileSystemInfo[]
_currentAssembliesHash = GetFileHash(
new List<Tuple<FileSystemInfo, bool>>
{
//add the bin folder and everything in it
new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Bin)),
new Tuple<FileSystemInfo, bool>(new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Bin)), false),
//add the app code folder and everything in it
new DirectoryInfo(IOHelper.MapPath("~/App_Code")),
new Tuple<FileSystemInfo, bool>(new DirectoryInfo(IOHelper.MapPath("~/App_Code")), false),
//add the global.asax (the app domain also monitors this, if it changes will do a full restart)
new FileInfo(IOHelper.MapPath("~/global.asax")),
//add the trees.config
new FileInfo(IOHelper.MapPath(SystemDirectories.Config + "/trees.config"))
new Tuple<FileSystemInfo, bool>(new FileInfo(IOHelper.MapPath("~/global.asax")), false),
//add the trees.config - use the contents to create the has since this gets resaved on every app startup!
new Tuple<FileSystemInfo, bool>(new FileInfo(IOHelper.MapPath(SystemDirectories.Config + "/trees.config")), true)
}
);
return _currentAssembliesHash;
@@ -200,18 +201,34 @@ namespace Umbraco.Core
/// <summary>
/// Returns a unique hash for the combination of FileInfo objects passed in
/// </summary>
/// <param name="filesAndFolders"></param>
/// <param name="filesAndFolders">
/// A collection of files and whether or not to use their file contents to determine the hash or the file's properties
/// (true will make a hash based on it's contents)
/// </param>
/// <returns></returns>
internal static long GetAssembliesHash(IEnumerable<FileSystemInfo> filesAndFolders)
internal static long GetFileHash(IEnumerable<Tuple<FileSystemInfo, bool>> filesAndFolders)
{
using (DisposableTimer.TraceDuration<PluginManager>("Determining hash of code files on disk", "Hash determined"))
{
var hashCombiner = new HashCodeCombiner();
//add each unique folder to the hash
foreach (var i in filesAndFolders.DistinctBy(x => x.FullName))
//get the file info's to check
var fileInfos = filesAndFolders.Where(x => x.Item2 == false).ToArray();
var fileContents = filesAndFolders.Except(fileInfos);
//add each unique folder/file to the hash
foreach (var i in fileInfos.Select(x => x.Item1).DistinctBy(x => x.FullName))
{
hashCombiner.AddFileSystemItem(i);
}
//add each unique file's contents to the hash
foreach (var i in fileContents.Select(x => x.Item1).DistinctBy(x => x.FullName))
{
var content = File.ReadAllText(i.FullName).Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty);
hashCombiner.AddCaseInsensitiveString(content);
}
return ConvertPluginsHashFromHex(hashCombiner.GetCombinedHashCode());
}
}

View File

@@ -242,16 +242,16 @@ namespace Umbraco.Tests
var list3 = new[] { f1, f3, f5, f7 };
//Act
var hash1 = PluginManager.GetAssembliesHash(list1);
var hash2 = PluginManager.GetAssembliesHash(list2);
var hash3 = PluginManager.GetAssembliesHash(list3);
var hash1 = PluginManager.GetFileHash(list1);
var hash2 = PluginManager.GetFileHash(list2);
var hash3 = PluginManager.GetFileHash(list3);
//Assert
Assert.AreNotEqual(hash1, hash2);
Assert.AreNotEqual(hash1, hash3);
Assert.AreNotEqual(hash2, hash3);
Assert.AreEqual(hash1, PluginManager.GetAssembliesHash(list1));
Assert.AreEqual(hash1, PluginManager.GetFileHash(list1));
}
[Test]