One appdomain, two db connections, only one of those to cache

May 27, 2016 at 5:10 AM
I have an application with a DB. I would like to cache this, as the DB is only changed by my application.

However, I also use an inhouse libabry that goes to a separate DB and I must not cache this data. How can I achieve this?

Thanks,
Rasmus
May 29, 2016 at 6:41 AM
EFCache can only cache results from queries sent to database by EF. Also, the cache is configured in the Configuration class. Finally, the cache uses store entity set names as the key. I don't know how your other library works but if it does not use EF or does not the same configuration where EFCache is wired up as in the application results won't be cached. If the databases you're connecting to have different schemas/table names names you can prevent results from selected tables/entity sets from being cached by returning you can override from the CachingPolicy class and return false from CanBeCached method depending on the entity set.
If however you use EF in the inhouse library, you share the configuration and the schemas of both databases are the same and you want to cache only results for one of the connection strings you may be out of luck. You might want to take a look at these threads that might be useful:

https://efcache.codeplex.com/discussions/654261

http://efcache.codeplex.com/discussions/581951

Thanks,
Pawel
May 29, 2016 at 8:13 AM
Pawel, thanks for your comprehensive response. I have tested, and the library's queries are cached by EFCache so apparently it qualifies for all the conditions you listed.

I am not sure what this means exactly or how to control 'does not the same configuration where EFCache is wired up as in the application'. Can you elaborate a bit? THey are both wired up with connections strings in the same web.config, if that is the 'configuration' referred to.

The library does not share db schema with my primary application, so I could filter on table names in CanBeCached. But this would be fairly fragile, as a subsequent update to the library could change this without me knowing.

Thanks for the links but I am not sure if they can help me. I might be out of luck, as you state, or at least need to modify EFCache locally to include connection string in cache key.

Thanks,
Rasmus
May 29, 2016 at 7:40 PM
If you are using different databases then it should not be that to excluded or entity sets from one database automatically - you would need to read the model and select all the EntitySets something like this:
((IObjectContextAdapter)ctx).ObjectContext.MetadataWorkspace.GetEntityContainer("CodeFirstDatabase",DataSpace.SSpace).EntitySets.Select(e => e.Name))
One thing to think is whether you really need/want to cache everything. Maybe you should actually review what you want to cache and include only these entity sets?

Thanks,
Pawel
May 29, 2016 at 8:28 PM
I will consider that. Looking at this again, and not only with this issue in mind, it might be best not to cache all and everything.

Thanks for your feedback!

Cheers,
Rasmus