Skip to content

Commit

Permalink
Add remaining read-only implementation to respect caches
Browse files Browse the repository at this point in the history
  • Loading branch information
Psypher9 authored and rezalas committed Apr 7, 2023
1 parent 7e8db88 commit 6c41fd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/DefaultCachedRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DefaultCachedRepository<TEntity, TContext> : RepositoryBase<TEntity
public DefaultCachedRepository(TContext dbFactory) : base(dbFactory)
{
}


public override async Task<IEnumerable<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
Expand All @@ -34,6 +35,22 @@ public override async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken c
return cachedEntities;
}

public override async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
if (cachedEntities.Any() == false)
await RefreshCache(cancellationToken);

return cachedEntities.FirstOrDefault(predicate.Compile());
}

public override async Task<bool> Exists(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
if (cachedEntities.Any() == false)
await RefreshCache(cancellationToken);

return cachedEntities.Any(predicate.Compile());
}

public async Task RefreshCache(CancellationToken cancellationToken = default)
{
cachedEntities = await base.GetAllAsync(cancellationToken);
Expand Down
12 changes: 8 additions & 4 deletions src/Interfaces/IReadOnlyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@ namespace BasicRepos
public interface IReadOnlyRepository<T> where T : class
{
/// <summary>
/// Allows for the internal entities to be queried with any type specifications
/// Allows for the internal entities to be queried with any type specifications.
/// Bypasses entity caches
/// </summary>
/// <returns>A facade for the internal <see cref="DbSet{TEntity}"/></returns>
IQueryable<T> Query();

/// <summary>
/// Allows for the internal entities to be queried with any type specifications
/// Allows for the internal entities to be queried with any type specifications.
/// Bypasses entity caches
/// </summary>
/// <returns>A facade for the internal <see cref="DbSet{TEntity}"/></returns>
IQueryable<T> Query(Expression<Func<T, bool>> predicate);

/// <summary>
/// Allows for the internal entities to be queried with any type specifications
/// Allows for the internal entities to be queried with any type specifications.
/// Bypasses entity caches.
/// </summary>
/// <returns>A facade for the internal <see cref="DbSet{TEntity}"/></returns>
IQueryable<K> Query<K>() where K : T;

/// <summary>
/// Allows for the internal entities to be queried without any type specifications
/// Bypasses entity caches
/// </summary>
/// <returns>A facade for the internal <see cref="DbSet{TEntity}"/></returns>
/// <returns>A façade for the internal <see cref="DbSet{TEntity}"/></returns>
IQueryable RawQuery();

/// <summary>
Expand Down

0 comments on commit 6c41fd1

Please sign in to comment.