Closed seroche closed 4 years ago
You should put the Cacheable()
method at the end of the chain:
var users = await _context.Set<Post>().OrderByDescending(x => x.Id).Cacheable().ToListAsync();
// NOTE: It's better to add the Cacheable()
method before the materialization methods such as ToList()
or FirstOrDefault()
to cover the whole expression tree.
I agree with you. However it's very convenient to add the Cacheable()
in the DbContext
. It allows me to control the caching mechanism from one single place.
Example:
public class CoreDbContext : DbContext
{
public EFCachedDbSet<Event> Events => Set<Event>().Cacheable(CacheExpirationMode.Absolute, TimeSpan.FromHours(1));
public EFCachedDbSet<Reservation> Reservations => Set<Reservation>().Cacheable(CacheExpirationMode.Absolute, TimeSpan.FromHours(1));
public EFCachedDbSet<Format> Formats => Set<Format>().Cacheable(CacheExpirationMode.Absolute, TimeSpan.FromDays(7));
public EFCachedDbSet<Category> Categories => Set<Category>().Cacheable(CacheExpirationMode.Absolute, TimeSpan.FromDays(7));
public EFCachedDbSet<Client> Clients => Set<Client>().Cacheable();
public EFCachedDbSet<Centre> Centres => Set<Centre>().Cacheable();
public EFCachedDbSet<Industry> Industries => Set<Industry>().Cacheable();
public EFCachedDbSet<User> Users => Set<User>().Cacheable();
public EFCachedDbSet<Profile> Profiles => Set<Profile>().Cacheable();
}
Shall I understand that EFCachedDbSet
is not the recommended approach to cache data?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related problems.
Summary of the issue
When I try to order a result set obtained through an
EFCachedDbSet
I get the exceptionEnvironment
Example code/Steps to reproduce:
Output: