From 2fd905edb982ea56765afd8718dd3dfef14daea2 Mon Sep 17 00:00:00 2001 From: hartvig Date: Mon, 17 Jan 2011 09:34:40 -0100 Subject: [PATCH] Work items: 29899 --- umbraco.MacroEngines.Juno/RazorEngine.cs | 17 ++++- umbraco/businesslogic/IO/FileMonitor.cs | 70 +++++++++++++++++++ .../umbraco.businesslogic.csproj | 1 + 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 umbraco/businesslogic/IO/FileMonitor.cs diff --git a/umbraco.MacroEngines.Juno/RazorEngine.cs b/umbraco.MacroEngines.Juno/RazorEngine.cs index 17422c7f79..a298af799b 100644 --- a/umbraco.MacroEngines.Juno/RazorEngine.cs +++ b/umbraco.MacroEngines.Juno/RazorEngine.cs @@ -40,7 +40,7 @@ namespace umbraco.MacroEngines try { string parsedResult; - if (!GetResult("RazorValidation", code, currentPage, out parsedResult)) { + if (!GetResult(null, code, currentPage, out parsedResult)) { errorMessage = parsedResult; return false; } @@ -68,11 +68,26 @@ namespace umbraco.MacroEngines : loadScript(IOHelper.MapPath(SystemDirectories.Python + "/" + macro.ScriptName)); string parsedResult; GetResult(macro.CacheIdentifier, template, currentPage, out parsedResult); + + // if it's a file we'll monitor changes to ensure that any updates to the file clears the cache + if (String.IsNullOrEmpty(macro.ScriptCode)) { + FileMonitor.Listen(SystemDirectories.Python + "/" + macro.ScriptName, action => RazorEngine.ClearRazorCompilationCache()); + } + return parsedResult; } #endregion + /// + /// This clears all compiled razor scripts, thus ensures that changes made to files or scripts causes a recompilation + /// + public static void ClearRazorCompilationCache() + { + Razor.SetTemplateBaseType(typeof(HtmlTemplateBase<>)); + Razor.SetTemplateBaseType(typeof(UmbracoTemplateBase<>)); + } + private bool GetResult(string cacheIdentifier, string template, INode currentPage, out string result) { try diff --git a/umbraco/businesslogic/IO/FileMonitor.cs b/umbraco/businesslogic/IO/FileMonitor.cs new file mode 100644 index 0000000000..257e8622d5 --- /dev/null +++ b/umbraco/businesslogic/IO/FileMonitor.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; +using System.Web.Caching; +using System.Web.Hosting; + +namespace umbraco.IO +{ + /// + /// This class can be used to monitor file changes and update accordingly. This is copied + /// from http://haacked.com/archive/2010/01/17/editable-routes.aspx and based on work in Dynamic Data + /// + public class FileMonitor + { + private FileMonitor(Action changeCallback) + : this(HostingEnvironment.VirtualPathProvider, changeCallback) + { + } + + private FileMonitor(VirtualPathProvider vpp, + Action changeCallback) + { + _vpp = vpp; + _changeCallback = changeCallback; + } + + VirtualPathProvider _vpp; + Action _changeCallback; + + // When the file at the given path changes, + // we'll call the supplied action. + public static void Listen(string virtualPath, Action action) + { + var notifier = new FileMonitor(action); + notifier.ListenForChanges(virtualPath); + } + + void ListenForChanges(string virtualPath) + { + // Get a CacheDependency from the BuildProvider, + // so that we know anytime something changes + var virtualPathDependencies = new List(); + virtualPathDependencies.Add(virtualPath); + CacheDependency cacheDependency = _vpp.GetCacheDependency( + virtualPath, virtualPathDependencies, DateTime.UtcNow); + HttpRuntime.Cache.Insert(virtualPath /*key*/, + virtualPath /*value*/, + cacheDependency, + Cache.NoAbsoluteExpiration, + Cache.NoSlidingExpiration, + CacheItemPriority.NotRemovable, + new CacheItemRemovedCallback(OnConfigFileChanged)); + } + + void OnConfigFileChanged(string key, object value, + CacheItemRemovedReason reason) + { + // We only care about dependency changes + if (reason != CacheItemRemovedReason.DependencyChanged) + return; + + _changeCallback(key); + + // Need to listen for the next change + ListenForChanges(key); + } + } +} diff --git a/umbraco/businesslogic/umbraco.businesslogic.csproj b/umbraco/businesslogic/umbraco.businesslogic.csproj index 42a6e40bbc..ebfb76d9ad 100644 --- a/umbraco/businesslogic/umbraco.businesslogic.csproj +++ b/umbraco/businesslogic/umbraco.businesslogic.csproj @@ -158,6 +158,7 @@ Code +