.Net core SQL cache dependency

1.8k Views Asked by At

I am trying to implement caching in ASP.Net core app. Currently What I have implemented is In Memory caching using IMemoryCache but what I want is cache should get invalidated if corresponding record in SQL server 2016 gets changed. One way I found it SQLCacheDependency but it is not a part of .Net Core as per this link

Any thoughts on this ?

2

There are 2 best solutions below

0
VMAtm On BEST ANSWER

Right now this class hasn't been migrated to the .Net Core, so you can't easily manage out such logic. Probably it will be a part of .Net Standard 2.0, so you either implement some temporary solution with CancellationChangeToken with direct sql database checks (which is very inefficient) or wait for update.

2
Philip P. On

You could add CancellationChangeToken and then call Cancel method on it, this should evict your record from the in-memory cache. On top of it, there is a PostEvictionDelegate callback available to you (to fire additional actions after the cache record has been evicted). You can read more here: https://learn.microsoft.com/en-us/aspnet/core/performance/caching/memory