Work items: 29899

This commit is contained in:
hartvig
2011-01-17 09:34:40 -01:00
parent db10f1ed26
commit 2fd905edb9
3 changed files with 87 additions and 1 deletions

View File

@@ -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
/// <summary>
/// This clears all compiled razor scripts, thus ensures that changes made to files or scripts causes a recompilation
/// </summary>
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

View File

@@ -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
{
/// <summary>
/// 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
/// </summary>
public class FileMonitor
{
private FileMonitor(Action<string> changeCallback)
: this(HostingEnvironment.VirtualPathProvider, changeCallback)
{
}
private FileMonitor(VirtualPathProvider vpp,
Action<string> changeCallback)
{
_vpp = vpp;
_changeCallback = changeCallback;
}
VirtualPathProvider _vpp;
Action<string> _changeCallback;
// When the file at the given path changes,
// we'll call the supplied action.
public static void Listen(string virtualPath, Action<string> 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<string>();
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);
}
}
}

View File

@@ -158,6 +158,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Interfaces\ILog.cs" />
<Compile Include="IO\FileMonitor.cs" />
<Compile Include="IO\IOHelper.cs" />
<Compile Include="IO\SystemDirectories.cs" />
<Compile Include="IO\SystemFiles.cs" />