88#if NET45
99using System . Collections . Generic ;
1010using System . Linq ;
11+ using System . Threading ;
1112using System . Threading . Tasks ;
1213#if EF5 || EF6
1314using System ;
@@ -24,19 +25,19 @@ namespace Z.EntityFramework.Plus
2425{
2526 public static partial class QueryCacheExtensions
2627 {
27- #if EF5 || EF6
28- /// <summary>
29- /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
30- /// yet, the query is materialized asynchronously and cached before being returned.
31- /// </summary>
32- /// <typeparam name="T">The generic type of the query.</typeparam>
33- /// <param name="query">The query to cache in the QueryCacheManager.</param>
34- /// <param name="policy">The policy to use to cache the query.</param>
35- /// <param name="tags">
36- /// A variable-length parameters list containing tags to expire cached
37- /// entries.
38- /// </param>
39- /// <returns>The result of the query.</returns>
28+ #if EF5
29+ /// <summary>
30+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
31+ /// yet, the query is materialized asynchronously and cached before being returned.
32+ /// </summary>
33+ /// <typeparam name="T">The generic type of the query.</typeparam>
34+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
35+ /// <param name="policy">The policy to use to cache the query.</param>
36+ /// <param name="tags">
37+ /// A variable-length parameters list containing tags to expire cached
38+ /// entries.
39+ /// </param>
40+ /// <returns>The result of the query.</returns>
4041 public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , CacheItemPolicy policy , params string [ ] tags ) where T : class
4142 {
4243 var key = QueryCacheManager . GetCacheKey ( query , tags ) ;
@@ -58,23 +59,6 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, C
5859 return result ;
5960 }
6061
61- //public static async Task<IEnumerable<T>> FromCacheAsync2<T>(this IQueryable<T> query, CacheItemPolicy policy, params string[] tags) where T : class
62- //{
63- // var key = QueryCacheManager.GetCacheKey(query, tags);
64-
65- // var item = QueryCacheManager.Cache.Get(key);
66-
67- // if (item == null)
68- // {
69- // item = await query.AsNoTracking().ToListAsync().ConfigureAwait(false);
70- // item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
71- // QueryCacheManager.AddCacheTag(key, tags);
72- // }
73-
74- // var result = (IEnumerable<T>)item;
75- // return result;
76- //}
77-
7862 /// <summary>
7963 /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
8064 /// yet, the query is materialized asynchronously and cached before being returned.
@@ -123,6 +107,131 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, p
123107 {
124108 return query . FromCacheAsync ( QueryCacheManager . DefaultCacheItemPolicy , tags ) ;
125109 }
110+ #elif EF6
111+ /// <summary>
112+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
113+ /// yet, the query is materialized asynchronously and cached before being returned.
114+ /// </summary>
115+ /// <typeparam name="T">The generic type of the query.</typeparam>
116+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
117+ /// <param name="policy">The policy to use to cache the query.</param>
118+ /// <param name="cancellationToken">The cancellation token.</param>
119+ /// <param name="tags">
120+ /// A variable-length parameters list containing tags to expire cached
121+ /// entries.
122+ /// </param>
123+ /// <returns>The result of the query.</returns>
124+ public static async Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , CacheItemPolicy policy , CancellationToken cancellationToken = default ( CancellationToken ) , params string [ ] tags ) where T : class
125+ {
126+ var key = QueryCacheManager . GetCacheKey ( query , tags ) ;
127+
128+ var item = QueryCacheManager . Cache . Get ( key ) ;
129+
130+ if ( item == null )
131+ {
132+ item = await query . AsNoTracking ( ) . ToListAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
133+ item = QueryCacheManager . Cache . AddOrGetExisting ( key , item , policy ) ?? item ;
134+ QueryCacheManager . AddCacheTag ( key , tags ) ;
135+ }
136+
137+ return ( IEnumerable < T > ) item ;
138+ }
139+
140+ /// <summary>
141+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
142+ /// yet, the query is materialized asynchronously and cached before being returned.
143+ /// </summary>
144+ /// <typeparam name="T">Generic type parameter.</typeparam>
145+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
146+ /// <param name="policy">The policy to use to cache the query.</param>
147+ /// <param name="tags">
148+ /// A variable-length parameters list containing tags to expire cached
149+ /// entries.
150+ /// </param>
151+ /// <returns>The result of the query.</returns>
152+ public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , CacheItemPolicy policy , params string [ ] tags ) where T : class
153+ {
154+ return query . FromCacheAsync ( policy , default ( CancellationToken ) , tags ) ;
155+ }
156+
157+ /// <summary>
158+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
159+ /// yet, the query is materialized asynchronously and cached before being returned.
160+ /// </summary>
161+ /// <typeparam name="T">Generic type parameter.</typeparam>
162+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
163+ /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
164+ /// <param name="cancellationToken">The cancellation token.</param>
165+ /// <param name="tags">
166+ /// A variable-length parameters list containing tags to expire cached
167+ /// entries.
168+ /// </param>
169+ /// <returns>The result of the query.</returns>
170+ public static async Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , DateTimeOffset absoluteExpiration , CancellationToken cancellationToken = default ( CancellationToken ) , params string [ ] tags ) where T : class
171+ {
172+ var key = QueryCacheManager . GetCacheKey ( query , tags ) ;
173+
174+ var item = QueryCacheManager . Cache . Get ( key ) ;
175+
176+ if ( item == null )
177+ {
178+ item = await query . AsNoTracking ( ) . ToListAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
179+ item = QueryCacheManager . Cache . AddOrGetExisting ( key , item , absoluteExpiration ) ?? item ;
180+ QueryCacheManager . AddCacheTag ( key , tags ) ;
181+ }
182+
183+ return ( IEnumerable < T > ) item ;
184+ }
185+
186+ /// <summary>
187+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
188+ /// yet, the query is materialized asynchronously and cached before being returned.
189+ /// </summary>
190+ /// <typeparam name="T">Generic type parameter.</typeparam>
191+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
192+ /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
193+ /// <param name="tags">
194+ /// A variable-length parameters list containing tags to expire cached
195+ /// entries.
196+ /// </param>
197+ /// <returns>The result of the query.</returns>
198+ public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , DateTimeOffset absoluteExpiration , params string [ ] tags ) where T : class
199+ {
200+ return query . FromCacheAsync ( absoluteExpiration , default ( CancellationToken ) , tags ) ;
201+ }
202+
203+ /// <summary>
204+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
205+ /// yet, the query is materialized asynchronously and cached before being returned.
206+ /// </summary>
207+ /// <typeparam name="T">Generic type parameter.</typeparam>
208+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
209+ /// <param name="tags">
210+ /// A variable-length parameters list containing tags to expire cached
211+ /// entries.
212+ /// </param>
213+ /// <returns>The result of the query.</returns>
214+ public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , params string [ ] tags ) where T : class
215+ {
216+ return query . FromCacheAsync ( QueryCacheManager . DefaultCacheItemPolicy , default ( CancellationToken ) , tags ) ;
217+ }
218+
219+ /// <summary>
220+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
221+ /// yet, the query is materialized asynchronously and cached before being returned.
222+ /// </summary>
223+ /// <typeparam name="T">Generic type parameter.</typeparam>
224+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
225+ /// <param name="cancellationToken">The cancellation token.</param>
226+ /// <param name="tags">
227+ /// A variable-length parameters list containing tags to expire cached
228+ /// entries.
229+ /// </param>
230+ /// <returns>The result of the query.</returns>
231+ public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , CancellationToken cancellationToken = default ( CancellationToken ) , params string [ ] tags ) where T : class
232+ {
233+ return query . FromCacheAsync ( QueryCacheManager . DefaultCacheItemPolicy , cancellationToken , tags ) ;
234+ }
126235#elif EFCORE
127236 /// <summary>
128237 /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
@@ -131,29 +240,42 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, p
131240 /// <typeparam name="T">The generic type of the query.</typeparam>
132241 /// <param name="query">The query to cache in the QueryCacheManager.</param>
133242 /// <param name="options">The cache entry options to use to cache the query.</param>
243+ /// <param name="cancellationToken">The cancellation token.</param>
134244 /// <param name="tags">
135245 /// A variable-length parameters list containing tags to expire cached
136246 /// entries.
137247 /// </param>
138248 /// <returns>The result of the query.</returns>
139- public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , MemoryCacheEntryOptions options , params string [ ] tags ) where T : class
249+ public static async Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , MemoryCacheEntryOptions options , CancellationToken cancellationToken = default ( CancellationToken ) , params string [ ] tags ) where T : class
140250 {
141251 var key = QueryCacheManager . GetCacheKey ( query , tags ) ;
142252
143- var result = Task . Run ( ( ) =>
253+ object item ;
254+ if ( ! QueryCacheManager . Cache . TryGetValue ( key , out item ) )
144255 {
145- object item ;
146- if ( ! QueryCacheManager . Cache . TryGetValue ( key , out item ) )
147- {
148- item = query . AsNoTracking ( ) . ToList ( ) ;
149- item = QueryCacheManager . Cache . Set ( key , item , options ) ;
150- QueryCacheManager . AddCacheTag ( key , tags ) ;
151- }
256+ item = await query . AsNoTracking ( ) . ToListAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
257+ item = QueryCacheManager . Cache . Set ( key , item , options ) ;
258+ QueryCacheManager . AddCacheTag ( key , tags ) ;
259+ }
152260
153- return ( IEnumerable < T > ) item ;
154- } ) ;
261+ return ( IEnumerable < T > ) item ;
262+ }
155263
156- return result ;
264+ /// <summary>
265+ /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
266+ /// yet, the query is materialized and cached before being returned.
267+ /// </summary>
268+ /// <typeparam name="T">The generic type of the query.</typeparam>
269+ /// <param name="query">The query to cache in the QueryCacheManager.</param>
270+ /// <param name="options">The cache entry options to use to cache the query.</param>
271+ /// <param name="tags">
272+ /// A variable-length parameters list containing tags to expire cached
273+ /// entries.
274+ /// </param>
275+ /// <returns>The result of the query.</returns>
276+ public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , MemoryCacheEntryOptions options , params string [ ] tags ) where T : class
277+ {
278+ return query . FromCacheAsync ( options , default ( CancellationToken ) , tags ) ;
157279 }
158280
159281 /// <summary>
@@ -168,9 +290,26 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, M
168290 /// entries.
169291 /// </param>
170292 /// <returns>The result of the query.</returns>
171- public static async Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , params string [ ] tags ) where T : class
293+ public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , params string [ ] tags ) where T : class
294+ {
295+ return query . FromCacheAsync ( QueryCacheManager . DefaultMemoryCacheEntryOptions , default ( CancellationToken ) , tags ) ;
296+ }
297+
298+ /// <summary>
299+ /// Return the result of the <paramref name="query" /> from the cache if possible. Otherwise,
300+ /// materialize asynchronously the query and cache the result before being returned.
301+ /// </summary>
302+ /// <typeparam name="T">Generic type parameter.</typeparam>
303+ /// <param name="query">The query to cache.</param>
304+ /// <param name="cancellationToken">The cancellation token.</param>
305+ /// <param name="tags">
306+ /// A variable-length parameters list containing tags to expire cached
307+ /// entries.
308+ /// </param>
309+ /// <returns>The result of the query.</returns>
310+ public static Task < IEnumerable < T > > FromCacheAsync < T > ( this IQueryable < T > query , CancellationToken cancellationToken = default ( CancellationToken ) , params string [ ] tags ) where T : class
172311 {
173- return await query . FromCacheAsync ( QueryCacheManager . DefaultMemoryCacheEntryOptions , tags ) ;
312+ return query . FromCacheAsync ( QueryCacheManager . DefaultMemoryCacheEntryOptions , cancellationToken , tags ) ;
174313 }
175314#endif
176315 }
0 commit comments