Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created May 11, 2023 18:03
Show Gist options
  • Save dcomartin/fc3654f90f498e1f4288980630f5a59d to your computer and use it in GitHub Desktop.
Save dcomartin/fc3654f90f498e1f4288980630f5a59d to your computer and use it in GitHub Desktop.
public class CacheRepository<T> : IRepository<T>
{
private readonly IMemoryCache _cache;
private readonly EfRepository<T> _repository;
public CacheRepository(IMemoryCache cache, EfRepository<T> repository)
{
_cache = cache;
_repository = repository;
}
public async Task<List<T>> List(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
return await _memoryCache.GetOrCreateAsync(specification.CacheKey, async entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(15);
return await _repository.List(specification, cancellationToken);
});
}
// The rest of the implementation....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment