Cache for multiple databases.

Aug 14, 2016 at 12:42 PM
Hello, I was trying to find previous questions of this sort, but was unable to find a clear answer.
I want to have a separate cache per database context. InMemoryCache is fine, but copying your example, that goes something liek this:
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());


            var cachingPolicy = CachePolicy;

            Loaded +=
              (sender, args) => args.ReplaceService<DbProviderServices>(
                (s, _) =>
                    return new CachingProviderServices(s, transactionHandler,
As far as I can tell, it leaves no room for separate caches for separate databases.
The issue I have is that I have more than one database full of same table and schema names, and I have only one application that connects to any of them whenever it needs to. I don't want data from one cached when I'm trying to do a query on another.
So, how do I make a separate cache for each context?
Aug 14, 2016 at 1:33 PM
Unfortunately this can't be achieved at the moment. Here are some relevant threads that contain more details:

Aug 14, 2016 at 1:42 PM
I think I'll have to modify the source, so I can implement a custom key for the cache.
Aug 15, 2016 at 3:43 PM
This what I thought about doing - extend the key to contain a piece of information identifying the database. I thought about just hashing the connection string but felt a bit fragile.

Aug 16, 2016 at 1:55 AM
Or maybe expose the key generation via some virtual method. Make it a little more OO.
Aug 19, 2016 at 5:35 PM
Key generation is an implementation detail the user should not be concerned with. I confirm it's an issue that different database are not distinguished but I don't see how allowing people to generate their own keys fixes this without opening a pandora box.