Skip to content

Commit 6446299

Browse files
committed
Added caching option
1 parent db56937 commit 6446299

18 files changed

+345
-74
lines changed

Perplex.Umbraco.Forms/App_Plugins/PerplexUmbracoForms/PerplexUmbracoForms.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@
4444
<PerplexRecaptchConfig>
4545
<ErrorMessage />
4646
</PerplexRecaptchConfig>
47+
<PerplexCacheConfig>
48+
<EnableCache>true</EnableCache>
49+
<CacheDurationInMinutes>10</CacheDurationInMinutes>
50+
</PerplexCacheConfig>
4751
</PerplexUmbracoFormsConfig>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
3+
namespace PerplexUmbraco.Forms.Code
4+
{
5+
public static class ArrayExtensions
6+
{
7+
public static void ForEach(this Array array, Action<Array, int[]> action)
8+
{
9+
if (array.LongLength == 0) return;
10+
ArrayTraverse walker = new ArrayTraverse(array);
11+
do action(array, walker.Position);
12+
while (walker.Step());
13+
}
14+
}
15+
16+
internal class ArrayTraverse
17+
{
18+
public int[] Position;
19+
private int[] maxLengths;
20+
21+
public ArrayTraverse(Array array)
22+
{
23+
maxLengths = new int[array.Rank];
24+
for (int i = 0; i < array.Rank; ++i)
25+
{
26+
maxLengths[i] = array.GetLength(i) - 1;
27+
}
28+
Position = new int[array.Rank];
29+
}
30+
31+
public bool Step()
32+
{
33+
for (int i = 0; i < Position.Length; ++i)
34+
{
35+
if (Position[i] < maxLengths[i])
36+
{
37+
Position[i]++;
38+
for (int j = 0; j < i; j++)
39+
{
40+
Position[j] = 0;
41+
}
42+
return true;
43+
}
44+
}
45+
return false;
46+
}
47+
}
48+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
3+
using Umbraco.Core;
4+
using Umbraco.Core.Logging;
5+
6+
namespace PerplexUmbraco.Forms.Code
7+
{
8+
public class CacheService : ICacheService
9+
{
10+
public void SetRequestCache(string cacheKey, object cacheObject)
11+
{
12+
if (cacheObject != null)
13+
{
14+
ApplicationContext.Current.ApplicationCache.RequestCache.GetCacheItem(cacheKey, () => cacheObject);
15+
return;
16+
}
17+
18+
LogHelper.Warn<CacheService>($"Failed to cache null object with key: {cacheKey}");
19+
}
20+
21+
public T GetRequestCache<T>(string cacheKey)
22+
{
23+
var cachedObject = ApplicationContext.Current.ApplicationCache.RequestCache.GetCacheItem(cacheKey);
24+
25+
if (cachedObject == null)
26+
{
27+
return default(T);
28+
}
29+
30+
return (T)cachedObject;
31+
}
32+
33+
public void SetRuntimeCache(string cacheKey, object cacheObject, TimeSpan duration)
34+
{
35+
SetRuntimeCache(cacheKey, cacheObject, duration, false);
36+
}
37+
38+
public void SetRuntimeCache(string cacheKey, object cacheObject, TimeSpan duration, bool isSliding)
39+
{
40+
if (cacheObject != null)
41+
{
42+
ApplicationContext.Current.ApplicationCache.RuntimeCache.InsertCacheItem(cacheKey, cacheObject.Copy, duration, isSliding);
43+
return;
44+
}
45+
46+
LogHelper.Warn<CacheService>($"Failed to cache null object with key: {cacheKey}");
47+
}
48+
49+
public void ClearRuntimeCacheByKey(string cacheKey)
50+
{
51+
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(cacheKey);
52+
}
53+
54+
public void ClearRuntimeCacheByPrefix(string prefix)
55+
{
56+
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheByKeySearch(prefix);
57+
}
58+
59+
public T GetRuntimeCache<T>(string cacheKey)
60+
{
61+
var cachedObject = ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem(cacheKey);
62+
63+
if (cachedObject == null)
64+
{
65+
return default(T);
66+
}
67+
68+
return ((T)cachedObject).Copy();
69+
}
70+
}
71+
}

Perplex.Umbraco.Forms/Code/Configuration/ExtensionConfig.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Xml.Serialization;
1+
using System.Xml.Serialization;
72

83
namespace PerplexUmbraco.Forms.Code.Configuration
94
{

Perplex.Umbraco.Forms/Code/Configuration/FieldTypeConfig.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62
using System.Xml.Serialization;
73

84
namespace PerplexUmbraco.Forms.Code.Configuration

Perplex.Umbraco.Forms/Code/Configuration/PerplexBaseFileConfig.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
62
using System.Xml.Serialization;
73

84
namespace PerplexUmbraco.Forms.Code.Configuration
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Xml.Serialization;
2+
3+
namespace PerplexUmbraco.Forms.Code.Configuration
4+
{
5+
[XmlType("PerplexCacheConfig")]
6+
public class PerplexCacheConfig
7+
{
8+
[XmlElement("CacheDurationInMinutes")]
9+
public int CacheDurationInMinutes { get; set; }
10+
11+
[XmlElement("EnableCache")]
12+
public bool EnableCache { get; set; }
13+
}
14+
}

Perplex.Umbraco.Forms/Code/Configuration/PerplexFileUploadConfig.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Xml.Serialization;
1+
using System.Xml.Serialization;
72

83
namespace PerplexUmbraco.Forms.Code.Configuration
94
{

Perplex.Umbraco.Forms/Code/Configuration/PerplexImageUploadConfig.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Xml.Serialization;
1+
using System.Xml.Serialization;
72

83
namespace PerplexUmbraco.Forms.Code.Configuration
94
{

Perplex.Umbraco.Forms/Code/Configuration/PerplexRecaptchaConfig.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Xml.Serialization;
1+
using System.Xml.Serialization;
72

83
namespace PerplexUmbraco.Forms.Code.Configuration
94
{

0 commit comments

Comments
 (0)