Skip to content

Commit

Permalink
Make it more unit testing friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Apr 5, 2024
1 parent 140bc3d commit 0622bb2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 57 deletions.
131 changes: 77 additions & 54 deletions src/EFCoreSecondLevelCacheInterceptor/EFCachedQueryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -26,12 +27,14 @@ public static class EFCachedQueryExtensions
/// <param name="expirationMode">Defines the expiration mode of the cache item.</param>
/// <param name="timeout">The expiration timeout.</param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this IQueryable<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout)
public static IQueryable<TType> Cacheable<TType>(this IQueryable<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout)
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)));

return query.TagWith(
EFCachePolicy.Configure(options => options.ExpirationMode(expirationMode).Timeout(timeout)));
}

/// <summary>
Expand All @@ -52,15 +55,17 @@ public static IQueryable<TType> Cacheable<TType>(
/// value.
/// </param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this IQueryable<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout, string[] cacheDependencies,
public static IQueryable<TType> Cacheable<TType>(this IQueryable<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout,
string[] cacheDependencies,
string saltKey)
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)
.CacheDependencies(cacheDependencies)
.SaltKey(saltKey)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(expirationMode).Timeout(timeout).CacheDependencies(cacheDependencies)
.SaltKey(saltKey)));
}

/// <summary>
Expand All @@ -77,13 +82,15 @@ public static IQueryable<TType> Cacheable<TType>(
/// This array will be used to invalidate the related cache of all related queries automatically.
/// </param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this IQueryable<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout, string[] cacheDependencies)
public static IQueryable<TType> Cacheable<TType>(this IQueryable<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout,
string[] cacheDependencies)
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)
.CacheDependencies(cacheDependencies)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(expirationMode).Timeout(timeout).CacheDependencies(cacheDependencies)));
}

/// <summary>
Expand All @@ -98,13 +105,15 @@ public static IQueryable<TType> Cacheable<TType>(
/// value.
/// </param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this IQueryable<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout, string saltKey)
public static IQueryable<TType> Cacheable<TType>(this IQueryable<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout,
string saltKey)
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)
.SaltKey(saltKey)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(expirationMode).Timeout(timeout).SaltKey(saltKey)));
}

/// <summary>
Expand All @@ -113,13 +122,13 @@ public static IQueryable<TType> Cacheable<TType>(
/// <typeparam name="TType">Entity type.</typeparam>
/// <param name="query">The input EF query.</param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this IQueryable<TType> query)
public static IQueryable<TType> Cacheable<TType>(this IQueryable<TType> query)
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(CacheExpirationMode.Absolute)
.Timeout(_thirtyMinutes).DefaultCacheableMethod(true)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(CacheExpirationMode.Absolute).Timeout(_thirtyMinutes)
.DefaultCacheableMethod(true)));
}

/// <summary>
Expand All @@ -128,13 +137,14 @@ public static IQueryable<TType> Cacheable<TType>(
/// <typeparam name="TType">Entity type.</typeparam>
/// <param name="query">The input EF query.</param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this DbSet<TType> query) where TType : class
public static IQueryable<TType> Cacheable<TType>(this DbSet<TType> query)
where TType : class
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(CacheExpirationMode.Absolute)
.Timeout(_thirtyMinutes).DefaultCacheableMethod(true)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(CacheExpirationMode.Absolute).Timeout(_thirtyMinutes)
.DefaultCacheableMethod(true)));
}

/// <summary>
Expand All @@ -145,12 +155,15 @@ public static IQueryable<TType> Cacheable<TType>(
/// <param name="expirationMode">Defines the expiration mode of the cache item.</param>
/// <param name="timeout">The expiration timeout.</param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this DbSet<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout) where TType : class
public static IQueryable<TType> Cacheable<TType>(this DbSet<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout)
where TType : class
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)));

return query.TagWith(
EFCachePolicy.Configure(options => options.ExpirationMode(expirationMode).Timeout(timeout)));
}

/// <summary>
Expand All @@ -171,15 +184,18 @@ public static IQueryable<TType> Cacheable<TType>(
/// value.
/// </param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this DbSet<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout, string[] cacheDependencies,
string saltKey) where TType : class
public static IQueryable<TType> Cacheable<TType>(this DbSet<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout,
string[] cacheDependencies,
string saltKey)
where TType : class
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)
.CacheDependencies(cacheDependencies)
.SaltKey(saltKey)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(expirationMode).Timeout(timeout).CacheDependencies(cacheDependencies)
.SaltKey(saltKey)));
}

/// <summary>
Expand All @@ -196,14 +212,16 @@ public static IQueryable<TType> Cacheable<TType>(
/// This array will be used to invalidate the related cache of all related queries automatically.
/// </param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this DbSet<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout, string[] cacheDependencies)
public static IQueryable<TType> Cacheable<TType>(this DbSet<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout,
string[] cacheDependencies)
where TType : class
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)
.CacheDependencies(cacheDependencies)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(expirationMode).Timeout(timeout).CacheDependencies(cacheDependencies)));
}

/// <summary>
Expand All @@ -218,14 +236,16 @@ public static IQueryable<TType> Cacheable<TType>(
/// value.
/// </param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> Cacheable<TType>(
this DbSet<TType> query, CacheExpirationMode expirationMode, TimeSpan timeout, string saltKey)
public static IQueryable<TType> Cacheable<TType>(this DbSet<TType> query,
CacheExpirationMode expirationMode,
TimeSpan timeout,
string saltKey)
where TType : class
{
sanityCheck(query);
return query.TagWith(EFCachePolicy.Configure(options =>
options.ExpirationMode(expirationMode).Timeout(timeout)
.SaltKey(saltKey)));

return query.TagWith(EFCachePolicy.Configure(options
=> options.ExpirationMode(expirationMode).Timeout(timeout).SaltKey(saltKey)));
}

/// <summary>
Expand All @@ -237,6 +257,7 @@ public static IQueryable<TType> Cacheable<TType>(
public static IQueryable<TType> NotCacheable<TType>(this IQueryable<TType> query)
{
sanityCheck(query);

return query.TagWith(IsNotCachableMarker);
}

Expand All @@ -246,9 +267,11 @@ public static IQueryable<TType> NotCacheable<TType>(this IQueryable<TType> query
/// <typeparam name="TType">Entity type.</typeparam>
/// <param name="query">The input EF query.</param>
/// <returns>Provides functionality to evaluate queries against a specific data source.</returns>
public static IQueryable<TType> NotCacheable<TType>(this DbSet<TType> query) where TType : class
public static IQueryable<TType> NotCacheable<TType>(this DbSet<TType> query)
where TType : class
{
sanityCheck(query);

return query.TagWith(IsNotCachableMarker);
}

Expand All @@ -259,9 +282,9 @@ private static void sanityCheck<TType>(IQueryable<TType> query)
throw new ArgumentNullException(nameof(query));
}

if (!(query.Provider is EntityQueryProvider))
if (query.Provider is not EntityQueryProvider)
{
throw new NotSupportedException("`Cacheable` method is designed only for relational EF Core queries.");
Debug.WriteLine("`Cacheable` method is designed only for relational EF Core queries.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Entity Framework Core Second Level Caching Library.</Description>
<VersionPrefix>4.3.1</VersionPrefix>
<VersionPrefix>4.4.0</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.1;netstandard2.0;net462;netcoreapp3.1;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -63,8 +63,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<PackageReference Include="AsyncKeyedLock" Version="[6.3.4,)"/>
<None Include="../../README.md" Link="README.md" Pack="true" PackagePath="/" Visible="false" />
<PackageReference Include="AsyncKeyedLock" Version="[6.3.4,)"/>
<None Include="../../README.md" Link="README.md" Pack="true" PackagePath="/" Visible="false"/>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System"/>
Expand Down

0 comments on commit 0622bb2

Please sign in to comment.