Skip to content

Instantly share code, notes, and snippets.

@ahjohannessen
Created February 20, 2011 22:26
Show Gist options
  • Save ahjohannessen/836375 to your computer and use it in GitHub Desktop.
Save ahjohannessen/836375 to your computer and use it in GitHub Desktop.
Spark caching
using System.Collections.Generic;
using System.Linq;
using Spark;
using Spark.Compiler;
using Spark.Web.FubuMVC;
using Spark.Web.FubuMVC.ViewCreation;
using Spark.Web.FubuMVC.ViewLocation;
namespace ZG.Mvc.Theming.Views.Spark
{
public class SparkLoader
{
private readonly SparkViewFactory _sparkViewFactory;
private readonly ActionContext _actionContext;
private static readonly IDictionary<BuildDescriptorParams, ISparkViewEntry> _cache;
static SparkLoader()
{
_cache = new Dictionary<BuildDescriptorParams, ISparkViewEntry>();
}
public SparkLoader(SparkViewFactory sparkViewFactory, ActionContext actionContext)
{
_sparkViewFactory = sparkViewFactory;
_actionContext = actionContext;
}
public virtual SparkView LoadPartial(string path)
{
var descriptorParams = getDescriptorParams(path, null);
var viewEntry = findViewEntry(descriptorParams);
var view = buildResult(viewEntry);
return (SparkView)view;
}
public virtual SparkView LoadView(string path, string master)
{
var descriptorParams = getDescriptorParams(path, master);
var viewEntry = findViewEntry(descriptorParams);
var view = buildResult(viewEntry);
return (SparkView)view;
}
private BuildDescriptorParams getDescriptorParams(string path, string master)
{
var parameters = _sparkViewFactory.DescriptorBuilder.GetExtraParameters(_actionContext);
var actionNamespace = _actionContext.ActionNamespace;
var actionName = _actionContext.ActionName;
return new BuildDescriptorParams(actionNamespace, actionName, path, master, false, parameters);
}
private ISparkViewEntry findViewEntry(BuildDescriptorParams descriptorParams)
{
ISparkViewEntry viewEntry;
//TODO: IMPLEMENT A SMARTER LOCKING MECHANISM HERE
lock (_cache)
{
if (_cache.TryGetValue(descriptorParams, out viewEntry))
{
// We have the entry in cache, but it has changed, we must invalidate the cache
if (!viewEntry.IsCurrent())
{
_cache.Remove(descriptorParams);
viewEntry = createViewEntry(descriptorParams);
_cache.Add(descriptorParams, viewEntry);
}
}
else
{
viewEntry = createViewEntry(descriptorParams);
_cache.Add(descriptorParams, viewEntry);
}
}
return viewEntry;
}
private ISparkViewEntry createViewEntry(BuildDescriptorParams descriptorParams)
{
var searchedLocations = new List<string>();
var descriptor = _sparkViewFactory.DescriptorBuilder.BuildDescriptor(descriptorParams, searchedLocations);
var viewEntry = _sparkViewFactory.Engine.CreateEntry(descriptor);
return viewEntry;
}
private ISparkView buildResult(ISparkViewEntry entry)
{
var view = entry.CreateInstance();
if (view is SparkView)
{
var sparkView = (SparkView)view;
sparkView.ResourcePathManager = _sparkViewFactory.Engine.ResourcePathManager;
sparkView.CacheService = _sparkViewFactory.CacheServiceProvider.GetCacheService();
}
return view;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment