Repository navigation
Expand file tree
/
Copy pathDefaultCacheProvider.cs
More file actions
214 lines (182 loc) · 6.82 KB
/
Copy pathDefaultCacheProvider.cs
File metadata and controls
214 lines (182 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
namespace uComponents.Mapping
{
/// <summary>
/// Cache provider for the <c>System.Web.Caching.Cache</c>.
/// </summary>
public class DefaultCacheProvider : ICacheProvider
{
private TimeSpan _slidingExpiration = TimeSpan.FromMinutes(10);
// Cannot insert null into cache, so use a static representation.
private static object _nullValue = new object();
private readonly Cache _cache;
private readonly Guid _keyPrefix;
/// <summary>
/// Instantiates a new cache provider using the default ASP.NET cache
/// (get it from <c>HttpContext.Current.Cache</c>).
/// </summary>
/// <param name="cache">The cache. Cannot be null.</param>
/// <param name="slidingExpiration">
/// Optional override for the sliding expiration period
/// of values added to the cache.
/// </param>
public DefaultCacheProvider(Cache cache, TimeSpan? slidingExpiration = null)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
if (slidingExpiration.HasValue)
{
_slidingExpiration = slidingExpiration.Value;
}
_cache = cache;
_keyPrefix = Guid.NewGuid();
}
/// <see cref="ICacheProvider.Insert"/>
public void Insert(string key, object value)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Key cannot be null or empty", "key");
}
else if (value == null)
{
value = _nullValue;
}
var qualifiedKey = GetQualifiedKey(key);
_cache.Insert(qualifiedKey, value, null, Cache.NoAbsoluteExpiration, _slidingExpiration);
}
/// <see cref="ICacheProvider.Get"/>
public object Get(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Key cannot be null or empty", "key");
}
var qualifiedKey = GetQualifiedKey(key);
var value = _cache[qualifiedKey];
return value == _nullValue ? null : value;
}
/// <see cref="ICacheProvider.ContainsKey"/>
public bool ContainsKey(string key)
{
var qualifiedKey = GetQualifiedKey(key);
return _cache[qualifiedKey] != null;
}
/// <summary>
/// Removes all cache items added by this cache provider.
/// </summary>
public void Clear()
{
var enumerator = _cache.GetEnumerator();
var keysToRemove = new List<string>();
while (enumerator.MoveNext())
{
var key = enumerator.Key as string;
if (key != null && key.StartsWith(_keyPrefix.ToString()))
{
keysToRemove.Add(key);
}
}
foreach (var key in keysToRemove)
{
_cache.Remove(key);
}
}
/// <summary>
/// Constructs a cache key specific to this cache provider.
/// </summary>
/// <param name="key">The key to qualify.</param>
private string GetQualifiedKey(string key)
{
return string.Format("{0}_{1}", _keyPrefix.ToString(), key);
}
}
/// <summary>
/// Convenience methods for the <see cref="ICacheProvider"/>, as required
/// by <see cref="INodeMappingEngine"/>.
/// </summary>
public static class CacheProviderExtensions
{
private const string _propertyValueFormat = "{0}_{1}";
private const string _aliasFormat = "{0}_DocumentTypeAlias";
/// <summary>
/// Inserts a node's property value into the cache.
/// </summary>
/// <param name="cache">The cache to use</param>
/// <param name="id">The ID of the node</param>
/// <param name="propertyName">The destination property name</param>
/// <param name="value">The mapped value of the property</param>
public static void InsertPropertyValue(this ICacheProvider cache, int id, string propertyName, object value)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
else if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentException("The property name cannot be null or empty", "propertyName");
}
var key = string.Format(_propertyValueFormat, id, propertyName);
cache.Insert(key, value);
}
/// <summary>
/// Inserts a node's alias into the cache.
/// </summary>
/// <param name="cache">The cache to use</param>
/// <param name="id">The ID of the node</param>
/// <param name="alias">The node's document type alias</param>
public static void InsertAlias(this ICacheProvider cache, int id, string alias)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
var key = string.Format(_aliasFormat, id);
cache.Insert(key, alias);
}
/// <summary>
/// Gets a model's cached property value.
/// </summary>
public static object GetPropertyValue(this ICacheProvider cache, int id, string propertyName)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
else if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentException("The property name cannot be null or empty", "propertyName");
}
var key = string.Format(_propertyValueFormat, id, propertyName);
return cache.Get(key);
}
/// <summary>
/// Gets a node's cached alias.
/// </summary>
public static string GetAlias(this ICacheProvider cache, int id)
{
var key = string.Format(_aliasFormat, id);
return cache.Get(key) as string;
}
/// <summary>
/// Check's if a model's property value is cached.
/// </summary>
public static bool ContainsPropertyValue(this ICacheProvider cache, int id, string propertyName)
{
return cache.ContainsKey(string.Format(_propertyValueFormat, id, propertyName));
}
/// <summary>
/// Check's if a node's alias is cached.
/// </summary>
public static bool ContainsAlias(this ICacheProvider cache, int id)
{
return cache.ContainsKey(string.Format(_aliasFormat, id));
}
}
}