Repository navigation
Expand file tree
/
Copy pathDbCacheExtensions.cs
More file actions
247 lines (213 loc) · 7.65 KB
/
Copy pathDbCacheExtensions.cs
File metadata and controls
247 lines (213 loc) · 7.65 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Text;
using SmartStore.Core;
using SmartStore.Core.Data;
using SmartStore.Core.Infrastructure;
namespace SmartStore.Data.Caching
{
public static class DbCacheExtensions
{
internal static IDbCache GetDbCacheInstance()
{
try
{
return EngineContext.Current.Resolve<IDbCache>();
}
catch
{
return new NullDbCache();
}
}
#region Request Cache
/// <summary>
/// Returns the result of <paramref name="source"/> from the (HTTP)-REQUEST cache if available,
/// otherwise the query is materialized and cached before being returned. Cache result invalidation
/// is performed automatically by the underlying interceptor.
/// </summary>
/// <typeparam name="T">The type of the data in the data source.</typeparam>
/// <param name="source">The query to be materialized.</param>
/// <returns>The result of the query.</returns>
public static List<T> ToListCached<T>(this IQueryable<T> source)
{
return ToListCached(source, null);
}
/// <summary>
/// Returns the result of <paramref name="source"/> from the (HTTP)-REQUEST cache if available,
/// otherwise the query is materialized and cached before being returned. Cache result invalidation
/// is performed automatically by the underlying interceptor.
/// </summary>
/// <typeparam name="T">The type of the data in the data source.</typeparam>
/// <param name="source">The query to be materialized.</param>
/// <param name="customKey">A custom cache key to use instead of the computed one.</param>
/// <returns>The result of the query.</returns>
/// <remarks>
/// This overload is slightly faster, because the key does not need to be generated from the passed <paramref name="source"/>
/// </remarks>
public static List<T> ToListCached<T>(this IQueryable<T> source, string customKey)
{
var entry = GetCacheEntry(source, customKey, () => source.ToList());
if (entry == null)
{
return source.ToList();
}
return (List<T>)entry.Value;
}
/// <summary>
/// Returns the first element of <paramref name="source"/> from the (HTTP)-REQUEST cache if available,
/// otherwise the query is materialized and cached before being returned. Cache result invalidation
/// is performed automatically by the underlying interceptor.
/// </summary>
/// <typeparam name="T">The type of the data in the data source.</typeparam>
/// <param name="source">The query to be materialized.</param>
/// <returns>The result of the query.</returns>
public static T FirstOrDefaultCached<T>(this IQueryable<T> source)
{
return FirstOrDefaultCached(source, null);
}
/// <summary>
/// Returns the first element of <paramref name="source"/> from the (HTTP)-REQUEST cache if available,
/// otherwise the query is materialized and cached before being returned. Cache result invalidation
/// is performed automatically by the underlying interceptor.
/// </summary>
/// <typeparam name="T">The type of the data in the data source.</typeparam>
/// <param name="source">The query to be materialized.</param>
/// <param name="customKey">A custom cache key to use instead of the computed one.</param>
/// <returns>The result of the query.</returns>
/// <remarks>
/// This overload is slightly faster, because the key does not need to be generated from the passed <paramref name="source"/>
/// </remarks>
public static T FirstOrDefaultCached<T>(this IQueryable<T> source, string customKey)
{
var entry = GetCacheEntry(source, customKey, () => source.FirstOrDefault());
if (entry == null)
{
return source.FirstOrDefault();
}
return (T)entry.Value;
}
internal static DbCacheEntry GetCacheEntry<T>(this IQueryable<T> source, string customKey, Func<object> valueFactory)
{
// (Perf) Checking here means higher perf, as keys and entitysets does not need to be evaluated
var cache = GetDbCacheInstance();
if (!cache.Enabled)
{
return null;
}
Guard.NotNull(source, nameof(source));
Guard.NotNull(valueFactory, nameof(valueFactory));
var cacheKey = new CacheKey<T>(source, customKey);
if (cacheKey.Key.IsEmpty())
{
return null;
}
if (!cache.RequestTryGet(cacheKey.Key, out var entry))
{
entry = cache.RequestPut(cacheKey.Key, valueFactory(), cacheKey.AffectedEntitySets);
}
//else
//{
// Debug.WriteLine("FromRequestCache: " + cacheKey.Key);
//}
return entry;
}
#endregion
#region Repository extensions
/// <summary>
/// Gets an entity by id from the database, the local change tracker, or the (HTTP)-REQUEST cache.
/// Cache result invalidation is performed automatically by the underlying interceptor.
/// </summary>
/// <param name="id">The id of the entity. This can also be a composite key.</param>
/// <returns>The resolved entity</returns>
public static T GetByIdCached<T>(this IRepository<T> rs, object id)
where T : BaseEntity
{
return GetByIdCached(rs, id, null);
}
/// <summary>
/// Gets an entity by id from the database, the local change tracker, or the (HTTP)-REQUEST cache.
/// Cache result invalidation is performed automatically by the underlying interceptor.
/// </summary>
/// <param name="id">The id of the entity. This can also be a composite key.</param>
/// <param name="customKey">A custom cache key to use instead of the computed one.</param>
/// <returns>The resolved entity</returns>
/// <remarks>
/// This overload is slightly faster, because the key does not need to be generated.
/// </remarks>
public static T GetByIdCached<T>(this IRepository<T> rs, object id, string customKey)
where T : BaseEntity
{
var entry = rs.Table.GetCacheEntry(customKey, () => rs.GetById(id));
if (entry == null)
{
return rs.GetById(id);
}
return (T)entry.Value;
}
#endregion
/// <summary>
/// Forces query results to be cached.
/// Allows caching results for queries using non-deterministic functions.
/// </summary>
/// <typeparam name="T">Query element type.</typeparam>
/// <param name="source">Query whose results will be cached. Must not be null.</param>
public static IQueryable<T> Cached<T>(this IQueryable<T> source)
where T : class
{
Guard.NotNull(source, nameof(source));
// TODO: implement Uncached()
var objectQuery = DbCacheUtil.GetObjectQuery(source) ?? source as ObjectQuery;
if (objectQuery != null)
{
SingletonQueries.Current.AddCachedQuery(
objectQuery.Context.MetadataWorkspace,
objectQuery.ToTraceString());
}
return source;
}
private class CacheKey<T>
{
public CacheKey(IQueryable<T> source, string customKey = null)
{
Guard.NotNull(source, nameof(source));
GenerateKeyAndAffectedEntitySets(source, customKey);
}
private void GenerateKeyAndAffectedEntitySets(IQueryable<T> source, string customKey = null)
{
var objectQuery = DbCacheUtil.GetObjectQuery(source) ?? source as ObjectQuery;
var key = customKey.NullEmpty();
if (objectQuery != null && string.IsNullOrEmpty(key))
{
var commandInfo = objectQuery.GetCommandInfo();
var sb = new StringBuilder();
sb.AppendLine(commandInfo.Sql);
foreach (DbParameter parameter in commandInfo.Parameters)
{
sb.Append(parameter.ParameterName);
sb.Append(";");
sb.Append(parameter.Value);
sb.AppendLine(";");
}
key = sb.ToString();
}
Key = key;
AffectedEntitySets = objectQuery != null
? objectQuery.GetAffectedEntitySets()
: new string[0];
}
public string Key
{
get;
private set;
}
public string[] AffectedEntitySets
{
get;
private set;
}
}
}
}