Support Caching and SqlCacheDependencies (or other second-level cache solution) as a built-in Feature
See original GitHub issueMost web applications are “read mostly” meaning they query data much more frequently than they change it. This means they benefit immensely from caching. Back in 2005, ASP.NET introduced the SqlCacheDependency, which supported to modes of operation:
- Triggers + Polling informed the application when changes occurred to a table that was being cached
- App subscribes to SQL Server and is notified when changes would impact a given query. Provides row/column level cache invalidation. This solution is far more efficient and works well with web farms.
This article briefly describes the feature: https://docs.microsoft.com/en-us/previous-versions/aspnet/ms178604(v=vs.100)
These features provided dramatic performance improvements with minimal effort 17 years ago but have never been carried forward or made compatible with EF (nor EF Core) as far as I’m aware. I’ve requested this feature periodically since EF was created but so far without success.
I would like to be able to enable caching on a per-entity basis as part of EF configuration. This caching should automatically be updated when the underlying data changes, using one of the above techniques or some novel approach. I don’t need every caching feature under the sun (durations, sliding windows, etc etc) - I just want to be able to reduce the number of needlessly repetitive queries on lookup tables and other read-mostly data.
Why not do it myself?
I’m unaware of any hooks that allow changes to whether, when, an how EF chooses to issue queries to its data store. This is data-level caching, below the level of entities and change-tracking. If there are hooks that someone can use to add a SqlCacheDependency
to a SqlCommand
used by EF please show me and I’ll gladly create a POC for doing this myself. I just don’t think that low level implementation has ever been exposed for third party extensions.
Issue Analytics
- State:
- Created a year ago
- Reactions:12
- Comments:28 (12 by maintainers)
@ardalis The interesting thing about this (and other second-level cache-like things) is that they were super-hot 15 years ago, but I’ve been waiting for someone to ask for it on EF Core and, as far as I am aware, this is the first time it has been asked for. I don’t really have an explanation for why this is, but it certainly isn’t something people are clamoring for anymore.
@roji Thanks, I’ll take a look at command interceptors and see if I can get some kind of sample working. Would love to see this in the box for all customers to benefit from, though!