Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added support to cache IQueryable<int>
  • Loading branch information
ghost1face committed Mar 4, 2014
commit 04a891cb46fee4e2b72473885bf4d0c6a68d99e0
124 changes: 124 additions & 0 deletions Source/EntityFramework.Extended/Extensions/CacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ public static IEnumerable<TEntity> FromCache<TEntity>(this IQueryable<TEntity> q
return result;
}

/// <summary>
/// Returns the result of the <paramref name="query" />; if possible from the cache,
/// otherwise the query is materialized and the result cached before being returned.
/// </summary>
/// <param name="query">The query to be materialized.</param>
/// <param name="cachePolicy">The cache policy for the query.</param>
/// <param name="tags">The list of tags to use for cache expiration.</param>
/// <returns>
/// The result of the query.
/// </returns>
public static IEnumerable<int> FromCache(this IQueryable<int> query, CachePolicy cachePolicy = null, IEnumerable<string> tags = null)
{
string key = query.GetCacheKey();
var cacheKey = new CacheKey(key,
tags ?? Enumerable.Empty<string>());

// allow override of CacheManager
var manager = Locator.Current.Resolve<CacheManager>();

var result = manager.GetOrAdd(
cacheKey,
k => query.ToList(),
cachePolicy ?? CachePolicy.Default
) as IEnumerable<int>;

return result;
}

/// <summary>
/// Returns the result of the <paramref name="query"/>; if possible from the cache,
/// otherwise the query is materialized asynchronously and the result cached before being returned.
Expand Down Expand Up @@ -73,6 +101,34 @@ public static async Task<IEnumerable<TEntity>> FromCacheAsync<TEntity>(this IQue
return result;
}

/// <summary>
/// Returns the result of the <paramref name="query" />; if possible from the cache,
/// otherwise the query is materialized asynchronously and the result cached before being returned.
/// </summary>
/// <param name="query">The query to be materialized.</param>
/// <param name="cachePolicy">The cache policy for the query.</param>
/// <param name="tags">The list of tags to use for cache expiration.</param>
/// <returns>
/// The result of the query.
/// </returns>
public static async Task<IEnumerable<int>> FromCacheAsync(this IQueryable<int> query, CachePolicy cachePolicy = null, IEnumerable<string> tags = null)
{
string key = query.GetCacheKey();
var cacheKey = new CacheKey(key,
tags ?? Enumerable.Empty<string>());

// allow override of CacheManager
var manager = Locator.Current.Resolve<CacheManager>();

var result = await manager.GetOrAddAsync(
cacheKey,
async k => await query.ToListAsync(),
cachePolicy ?? CachePolicy.Default
) as IEnumerable<int>;

return result;
}

/// <summary>
/// Returns the first element of the <paramref name="query"/>; if possible from the cache,
/// otherwise the query is materialized and the result cached before being returned.
Expand All @@ -91,6 +147,24 @@ public static TEntity FromCacheFirstOrDefault<TEntity>(this IQueryable<TEntity>
.FirstOrDefault();
}

/// <summary>
/// Returns the first element of the <paramref name="query" />; if possible from the cache,
/// otherwise the query is materialized and the result cached before being returned.
/// </summary>
/// <param name="query">The query to be materialized.</param>
/// <param name="cachePolicy">The cache policy for the query.</param>
/// <param name="tags">The list of tags to use for cache expiration.</param>
/// <returns>
/// default(T) if source is empty; otherwise, the first element in source.
/// </returns>
public static int FromCacheFirstOrDefault(this IQueryable<int> query, CachePolicy cachePolicy = null, IEnumerable<string> tags = null)
{
return query
.Take(1)
.FromCache(cachePolicy, tags)
.FirstOrDefault();
}

/// <summary>
/// Returns the first element of the <paramref name="query"/>; if possible from the cache,
/// otherwise the query is materialized asynchronously and the result cached before being returned.
Expand All @@ -109,6 +183,24 @@ public static async Task<TEntity> FromCacheFirstOrDefaultAsync<TEntity>(this IQu
).FirstOrDefault();
}

/// <summary>
/// Returns the first element of the <paramref name="query" />; if possible from the cache,
/// otherwise the query is materialized asynchronously and the result cached before being returned.
/// </summary>
/// <param name="query">The query to be materialized.</param>
/// <param name="cachePolicy">The cache policy for the query.</param>
/// <param name="tags">The list of tags to use for cache expiration.</param>
/// <returns>
/// default(T) if source is empty; otherwise, the first element in source.
/// </returns>
public static async Task<int> FromCacheFirstOrDefaultAsync(this IQueryable<int> query, CachePolicy cachePolicy = null, IEnumerable<string> tags = null)
{
return (await query
.Take(1)
.FromCacheAsync(cachePolicy, tags)
).FirstOrDefault();
}

/// <summary>
/// Removes the cached query from cache.
/// </summary>
Expand Down Expand Up @@ -144,5 +236,37 @@ public static IQueryable<TEntity> RemoveCache<TEntity>(this IQueryable<TEntity>
removed = manager.Remove(key) as IEnumerable<TEntity>;
return query;
}

/// <summary>
/// Removes the cached query from cache.
/// </summary>
/// <param name="query">The query to be materialized.</param>
/// <returns>
/// The original <paramref name="query" /> for fluent chaining.
/// </returns>
public static IQueryable<int> RemoveCache(this IQueryable<int> query)
{
IEnumerable<int> removed;
return RemoveCache(query, out removed);
}

/// <summary>
/// Removes the cached query from cache.
/// </summary>
/// <param name="query">The query to be materialized.</param>
/// <param name="removed">The removed items for cache.</param>
/// <returns>
/// The original <paramref name="query" /> for fluent chaining.
/// </returns>
public static IQueryable<int> RemoveCache(this IQueryable<int> query, out IEnumerable<int> removed)
{
string key = query.GetCacheKey();

// allow override of CacheManager
var manager = Locator.Current.Resolve<CacheManager>();

removed = manager.Remove(key) as IEnumerable<int>;
return query;
}
}
}