How to force cache to update after insert and updates ?

May 28, 2016 at 9:22 PM
Hi there, First of all, great library.

But when I add a new record to my table, EFCache doesnt know so I always get an old copy of the table.

Is there a way for the cache to be automaticly updated on updates and deletes

(I use webforms, cshtml files and .net 4.6.2 )
Coordinator
May 29, 2016 at 5:50 AM
If configured correctly EFCache does clear results for EntitySets affected by Inserts and Updates. Make sure you configured EFCache as shown here: https://efcache.codeplex.com/ (the transactionHandler and interceptors are responsible for clearing the cache). Note that if update your database outside of EF pipeline (e.g. you send a SQL statement directly - for instance using SqlCommand) you will bypass EF pipeline and the results won't be cleared. Finally if you use MemoryCache it is local to the machine so if the database is updated from a different machine the cache won't be invalidated (because it's local). In this scenario you should consider using cache that would be common. For instance you could cache using Redis - a Redis provider for EFCache already exists: https://www.nuget.org/packages/EFCache.Redis/

Thanks,
Pawel
May 29, 2016 at 9:28 AM
Thank you for fast response.

I don't use any insert or updates outside EF.
I already have the following lines on initializing the cache
        CacheTransactionHandler transactionHandler = new CacheTransactionHandler(mc);
        AddInterceptor(transactionHandler);
So how is the transactionHandler is supposed to know when the records get updated?
Should I inform it somehow manually?
Or should I configure it to fetch updates from the database?

Do you have an example? I think this looks like a common issue for many users.
May 29, 2016 at 10:00 AM
Edited May 29, 2016 at 11:14 AM
I am using the initialization code on efcache.homeplex.com page.
I also tried adding this line to my web.config
  <entityFramework  codeConfigurationType="MySite.CachedEfConfiguration, MySite">
  </entityFramework>
I see it enters the CacheTransactionHandler line, but it still is not aware of deletes or updates in my tables

I am using EF 6.1.3
Coordinator
May 29, 2016 at 6:32 PM
The transaction handler is plugged in as an interceptor. Whenever EF commits a transaction it invokes the transaction handler. SaveChanges always runs in a transaction. One scenario that does not work is TransactionScope - if you are using TransactionScope things won't work. There was a PR for that but it was never finished. If you are not doing anything special and things still don't work create a bug and include a repro. To my knowledge the cache is invalidated correctly so without a repro it is hard to tell why you see the problem.

Thanks,
Pawel
Dec 10, 2016 at 6:26 AM
Hi
I also have this problem but I'm using structure map library to use ioc and unit of work pattern to access the db. Does any wrong operation with structure map or unit of work pattern?
There is another point: after some minutes the memory was updates and all records got last changes.