question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Caching and SqlCacheDependencies (or other second-level cache solution) as a built-in Feature

See original GitHub issue

Most 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:

  1. Triggers + Polling informed the application when changes occurred to a table that was being cached
  2. 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:open
  • Created a year ago
  • Reactions:12
  • Comments:28 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
ajcvickerscommented, Mar 31, 2022

@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.

2reactions
ardaliscommented, Mar 31, 2022

@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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate Second-Level Cache
Basically, it acts as a bridge between Hibernate and cache providers. In this article, we'll use Ehcache, a mature and widely used cache,...
Read more >
Hibernate Second-Level Cache Explained
The second-level cache is enabled by plugging in a third-party caching technology to expand the built-in caching capabilities in Hibernate.
Read more >
NCache 4.6 Programmer's Guide
NCache is a flexible and feature rich 100% .NET caching solution that provides high performance and scalability to handle any transaction load.
Read more >
Caching
Optimizes second-level cache operations to minimize writes, at the cost of more frequent reads. Providers typically set this appropriately.
Read more >
Chapter 27. NHibernate.Caches
Web.Caching like SysCache, but can use SqlCacheDependencies to invalidate cache regions when data in an underlying SQL Server table or query changes. Query ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found