VahidN / EFSecondLevelCache.Core

Entity Framework Core Second Level Caching Library
Apache License 2.0
326 stars 50 forks source link

InvalidateCacheDependencies for .Include queries #10

Closed szavoda closed 7 years ago

szavoda commented 7 years ago

Hi -

I am trying to understand a little about invalidating cache entries in my scenario. I have a Customer entity that ".Includes" several related entities such as Address and Phone during a GetCustomer operation.

I have been noticing that when I add / update a Phone record, for instance, a subsequent GetCustomer call does not reflect the new / changed records.

Is there a best practices way that you can recommend to invalidate the cache of the related object?

Thanks!

Shawn

VahidN commented 7 years ago
szavoda commented 7 years ago

In my Customer -> Phone example above, a change to the Phone entity does not show the Customer Entity in the changedEntityNames variable. I can see in the tests where you are testing for my scenario, however.

I wonder if I have set something up improperly with the entity so that the context does not recognize the relationship.

I want to avoid invalidating the entire cache. Temporarily, I am going to pass an optional parameter to SaveChanges() that will include the additional entities to invalidate the cache for.

VahidN commented 7 years ago

Please provide a failing test to study it, something like the mentioned tests.

VahidN commented 7 years ago

Tip: EF Core has the ignored includes concept too. More info

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFQuerying;Trusted_Connection=True;")
        .ConfigureWarnings(warnings => warnings.Throw(CoreEventId.IncludeIgnoredWarning));
}
VahidN commented 7 years ago

Tip-2: How to find the detected cache dependencies of a query:

var debugInfo = new EFCacheDebugInfo();
var list = context.Products.Cacheable(debugInfo).ToList();
var cacheDependencies = debugInfo2.EFCacheKey.CacheDependencies;
lock[bot] commented 4 years ago

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.