Skip to content

Commit

Permalink
Added caching option
Browse files Browse the repository at this point in the history
Updated cache key

Refactored
  • Loading branch information
jameskimsca committed Aug 26, 2018
1 parent db56937 commit 2dc7ed1
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@
<PerplexRecaptchConfig>
<ErrorMessage />
</PerplexRecaptchConfig>
<PerplexCacheConfig>
<EnableCache>true</EnableCache>
<CacheDurationInMinutes>10</CacheDurationInMinutes>
</PerplexCacheConfig>
</PerplexUmbracoFormsConfig>
7 changes: 1 addition & 6 deletions Perplex.Umbraco.Forms/Code/Configuration/ExtensionConfig.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml.Serialization;

namespace PerplexUmbraco.Forms.Code.Configuration
{
Expand Down
4 changes: 0 additions & 4 deletions Perplex.Umbraco.Forms/Code/Configuration/FieldTypeConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace PerplexUmbraco.Forms.Code.Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace PerplexUmbraco.Forms.Code.Configuration
Expand Down
14 changes: 14 additions & 0 deletions Perplex.Umbraco.Forms/Code/Configuration/PerplexCacheConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Xml.Serialization;

namespace PerplexUmbraco.Forms.Code.Configuration
{
[XmlType("PerplexCacheConfig")]
public class PerplexCacheConfig
{
[XmlElement("CacheDurationInMinutes")]
public int CacheDurationInMinutes { get; set; }

[XmlElement("EnableCache")]
public bool EnableCache { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml.Serialization;

namespace PerplexUmbraco.Forms.Code.Configuration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml.Serialization;

namespace PerplexUmbraco.Forms.Code.Configuration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml.Serialization;

namespace PerplexUmbraco.Forms.Code.Configuration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;
using System.Xml.Serialization;

using static PerplexUmbraco.Forms.Code.Constants;

namespace PerplexUmbraco.Forms.Code.Configuration
Expand Down Expand Up @@ -73,6 +71,8 @@ public static void CreateIfNotExists()

public PerplexRecaptchaConfig PerplexRecaptchaConfig { get; set; }

public PerplexCacheConfig PerplexCacheConfig { get; set; }

private static string GetFilePath()
{
return HostingEnvironment.MapPath(Constants.CONFIGURATION_FILE_PATH);
Expand Down Expand Up @@ -139,6 +139,12 @@ private static string GetFilePath()
PerplexRecaptchaConfig = new PerplexRecaptchaConfig
{
ErrorMessage = ""
},

PerplexCacheConfig = new PerplexCacheConfig
{
EnableCache = false,
CacheDurationInMinutes = 10
}
};
}
Expand Down
32 changes: 26 additions & 6 deletions Perplex.Umbraco.Forms/Code/UmbracoEvents.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Linq;
using System;
using System.Linq;
using System.Web;

using PerplexUmbraco.Forms.Code.Configuration;

using Umbraco.Core;
using Umbraco.Core.Logging;
using PerplexUmbraco.Forms.Code.Configuration;
using Umbraco.Web;
using Umbraco.Forms.Data.Storage;
using System;
using Umbraco.Forms.Core;
using Umbraco.Forms.Data.Storage;
using Umbraco.Web;

namespace PerplexUmbraco.Forms.Code
{
Expand Down Expand Up @@ -64,6 +66,8 @@ void FormStorage_Created(object sender, FormEventArgs e)

folder.Forms.Add(form.Id.ToString());
PerplexFolder.SaveAll();

ClearFormsCache(folderId.ToString());
}

void FormStorage_Deleted(object sender, FormEventArgs e)
Expand All @@ -75,7 +79,23 @@ void FormStorage_Deleted(object sender, FormEventArgs e)
{
folder.Forms.Remove(form.Id.ToString());
PerplexFolder.SaveAll();

ClearFormsCache(folder.Id);
}
}

void ClearFormsCache(string folderId)
{
var cacheConfig = PerplexUmbracoFormsConfig.Get.PerplexCacheConfig;

if (cacheConfig.EnableCache)
{
var cacheKey = $"PerplexFormTreeController_GetTreeNodes_id:{folderId}";
var rtCache = ApplicationContext.Current.ApplicationCache.RuntimeCache;

if (rtCache.GetCacheItemsByKeySearch(cacheKey).Any())
rtCache.ClearCacheByKeySearch(cacheKey);
}
}
}
}
}
85 changes: 57 additions & 28 deletions Perplex.Umbraco.Forms/Controllers/PerplexFormTreeController.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
using Newtonsoft.Json;
using PerplexUmbraco.Forms.Code;
using System;
using System.Collections.Generic;
using System.IO;
using System;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using Umbraco.Forms.Data;

using PerplexUmbraco.Forms.Code;
using PerplexUmbraco.Forms.Code.Configuration;

using umbraco;
using umbraco.BusinessLogic.Actions;
using Umbraco.Forms.Web.Trees;
using Umbraco.Web;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Mvc;
using Umbraco.Web.Trees;
using Umbraco.Web;
using umbraco.BusinessLogic.Actions;
using umbraco;

namespace PerplexUmbraco.Forms.Controllers
{
Expand All @@ -41,27 +36,61 @@ public class PerplexFormTreeController : FormTreeController
{
// We load our custom menu actions from our own folder
private const string VIEWS_ROOT = "/App_Plugins/PerplexUmbracoForms/views/";
private readonly PerplexCacheConfig _cacheConfig;

public PerplexFormTreeController() { }
public PerplexFormTreeController()
{
_cacheConfig = PerplexUmbracoFormsConfig.Get.PerplexCacheConfig;
}

protected override Umbraco.Web.Models.Trees.TreeNodeCollection GetTreeNodes(string id, System.Net.Http.Formatting.FormDataCollection queryStrings)
protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
{
var cacheKey = $"PerplexFormTreeController_GetTreeNodes_id:{queryStrings["id"]}";
var rtCache = ApplicationContext.ApplicationCache.RuntimeCache;

// If this is a form, use Umbraco's default behavior
var folder = PerplexFolder.Get(id);
if (folder == null)
{
return base.GetTreeNodes(id, queryStrings);
var treeNodeCollection = _cacheConfig.EnableCache ? (TreeNodeCollection)rtCache.GetCacheItem(cacheKey) : (TreeNodeCollection)null;

if (treeNodeCollection == null)
{
treeNodeCollection = base.GetTreeNodes(id, queryStrings);

if (_cacheConfig.EnableCache)
{
if (rtCache.GetCacheItemsByKeySearch(cacheKey).Any())
rtCache.ClearCacheByKeySearch(cacheKey);

rtCache.InsertCacheItem(cacheKey, () => treeNodeCollection, new TimeSpan(0, _cacheConfig.CacheDurationInMinutes, 0), true);
}
}

return treeNodeCollection;
}

// This is a folder
var baseTreeNodes = _cacheConfig.EnableCache ? (TreeNodeCollection)rtCache.GetCacheItem(cacheKey) : (TreeNodeCollection)null;

if (baseTreeNodes == null)
{
// We require all forms, and apply filtering based on folders later
baseTreeNodes = base.GetTreeNodes("-1", queryStrings);

// We require all forms, and apply filtering based on folders later
var baseTreeNodes = base.GetTreeNodes("-1", queryStrings);
if (_cacheConfig.EnableCache)
{
if (rtCache.GetCacheItemsByKeySearch(cacheKey).Any())
rtCache.ClearCacheByKeySearch(cacheKey);

rtCache.InsertCacheItem(cacheKey, () => baseTreeNodes, new TimeSpan(0, _cacheConfig.CacheDurationInMinutes, 0), true);
}
}

// Sanity check; make sure there are no orphan forms around
// (forms not contained within any folder). If so, move them to the root folder
var orphans = baseTreeNodes.Where(n => PerplexFolder.Get(f => f.Forms.Any(formId => formId == n.Id.ToString())) == null).ToList();
if(orphans.Count > 0)
if (orphans.Count > 0)
{
foreach (var orphan in orphans)
{
Expand All @@ -74,7 +103,7 @@ protected override Umbraco.Web.Models.Trees.TreeNodeCollection GetTreeNodes(stri
// Hide all Forms that are not contained within this folder
// If this folder itself is disabled (due to the user not having access),
// we also hide all its forms
baseTreeNodes.RemoveAll(n =>
baseTreeNodes.RemoveAll(n =>
!folder.Forms.Contains(n.Id.ToString()) ||
(folder.Disabled && folder.Forms.Contains(n.Id.ToString()))
);
Expand All @@ -92,7 +121,7 @@ protected override Umbraco.Web.Models.Trees.TreeNodeCollection GetTreeNodes(stri

// Add any subfolders of this node
// We loop through the list in reverse as we add every folder at the start of the list (before forms)
foreach (var subFolder in folder.Folders.Reverse<PerplexFolder>())
foreach (var subFolder in folder.Folders.Reverse())
{
// If this subfolder is disabled, and it is not on a path towards
// a folder that is NOT disabled, it should not be listed at all.
Expand Down Expand Up @@ -139,7 +168,7 @@ protected override TreeNode CreateRootNode(FormDataCollection queryStrings)

// If none are set, all folders are allowed so we just use default behavior
// Likewise if the common ancestors of all allowed folders is the root.
PerplexFolder commonAncestor = PerplexFolder.GetCommonAncestor(startFolders);
PerplexFolder commonAncestor = PerplexFolder.GetCommonAncestor(startFolders);

if (!startFolders.Any() || commonAncestor == PerplexFolder.GetRootFolder())
{
Expand All @@ -157,11 +186,11 @@ protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
// if this is the common ancestor of this user's start nodes but not
// a start node itself. In that case it should also show as disabled in
// the UI, and we hide its URL.
rootNode.RoutePath = "forms/perplexForms/folder/" + commonAncestor.Id;
if(commonAncestor.Disabled)
rootNode.RoutePath = "forms/perplexForms/folder/" + commonAncestor.Id;
if (commonAncestor.Disabled)
{
rootNode.CssClasses.Add("disabled");
}
}

// Folder has children if it has either forms or folders
rootNode.HasChildren = commonAncestor.Forms.Any() || commonAncestor.Folders.Any();
Expand Down Expand Up @@ -197,10 +226,10 @@ protected override MenuItemCollection GetMenuForNode(string id, FormDataCollecti
{
menu.Items.RemoveAll(m => m.Alias != ActionRefresh.Instance.Alias);
return menu;
}
}

// Create Form (default Umbraco view, hence alias)
AddMenuItem(menu, "Create Form", alias: "create", icon: "icon icon-add");
AddMenuItem(menu, "Create Form", alias: "create", icon: "icon icon-add");

// Create Folder
AddMenuItem(menu, "Create Folder", view: "createFolder", icon: "icon icon-folder");
Expand Down Expand Up @@ -239,7 +268,7 @@ protected override MenuItemCollection GetMenuForNode(string id, FormDataCollecti
var root = PerplexFolder.GetRootFolder();

// If the root folder is disabled, remove all menu actions except Reload
if(root.Disabled)
if (root.Disabled)
{
menu.Items.RemoveAll(m => m.Alias != ActionRefresh.Instance.Alias);
return menu;
Expand Down
18 changes: 5 additions & 13 deletions Perplex.Umbraco.Forms/Controllers/PerplexUmbracoFormController.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
using Newtonsoft.Json;
using PerplexUmbraco.Forms.Code;
using PerplexUmbraco.Forms.Code.Configuration;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using Umbraco.Core.IO;
using Umbraco.Forms.Core.Providers;
using Umbraco.Forms.Data;

using PerplexUmbraco.Forms.Code;
using PerplexUmbraco.Forms.Code.Configuration;

using Umbraco.Forms.Data.Storage;
using Umbraco.Forms.Mvc.Models.Backoffice;
using Umbraco.Forms.Web.Models.Backoffice;
using Umbraco.Web.Editors;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;

namespace PerplexUmbraco.Forms.Controllers
Expand Down
1 change: 1 addition & 0 deletions Perplex.Umbraco.Forms/Perplex.Umbraco.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Code\Configuration\PerplexCacheConfig.cs" />
<Compile Include="Code\Configuration\ExtensionConfig.cs" />
<Compile Include="Code\Configuration\FieldTypeConfig.cs" />
<Compile Include="Code\Configuration\PerplexBaseFileConfig.cs" />
Expand Down

0 comments on commit 2dc7ed1

Please sign in to comment.