Add ability to inject a Dynamic Value into the Second Level Cache key
Description
Environment
Attachments
Activity

taber loveless October 24, 2017 at 1:18 AM
I believe you are correct that I could create a 'shim' ICacheProvider to sit between NH and my chosen CacheProvider - Ill have to experiment with setting the regions and see how well it works out.. thank you for your input!

taber loveless October 24, 2017 at 12:36 AMEdited
Yes, a big part of what drives this is MultiTenancy

Frédéric Delaporte October 23, 2017 at 10:28 PM
Thinking about it again, it looks like you probably use some tricks anyway to change the connection string in your custom connection provider, maybe something like a thread local or async local variable, or any other contextual storage.
You should be able to do the same for having a "dynamic" region prefix with a custom ICacheProvider
which would "patch" the region and delegate the remaining work to a normal cache provider of your choice.
In fact all this looks a lot like a need of multi-tenancy. This exists in Hibernate, but it is not port to NHibernate. So maybe the feature you miss is "just" a port of multi-tenancy to NHibernate.
By the way, this issue does no more look like an issue, it is now quite more a support thread. Time to cease, there is nothing to be done on providers from here I believe.

taber loveless October 23, 2017 at 9:53 PM
yeah, so query regions wont help solve my issue... thanks for your replies, this helps greatly in understanding where the 'friction' lies between what I need and the way NH/Cache providers currently work and clarifies your initial response for me
Ill have to enter the feature req on Github, maybe after I put together a rudimentary implementation for NH Devs to review and improve.

Frédéric Delaporte October 23, 2017 at 6:49 PM
Cache region for queries is settable on the `IQuery` interface directly, so you can set it to whatever you want dynamically. But this affects only the identifiers yielded by the query, the entities will still be hydrated from their mapping cache region.
Details
Details
Assignee

Reporter

Id like the ability to specify a Dynamic Value to be included in an Entities second level cache Key. I suspect that the Dynamic Value will need to be inserted at the beginning of the Key value to support:
1) Separating different entities having the same EntityName + PK + Namespace + Version.
2) Ability to sub-partition a cache region
3) Ability to expire/clear/remove entire sub-partitions or unique Entities having a specific Dynamic value the Key startsWith
The reason is to support our multi-tenant application (using a redis cache) where we utilize separate databases for large customers while we keep most other accounts withing a single database. The application uses a custom "NH.Connection.DriverConnectionProvider" to appropriately set the connection string for which database will be used to fulfill the queries.
Simple example of two unique "person" entities having the same PK are actually unique between different databases and the application cache needs to reference as such:
DataBase1.Person.1
DataBase2.Person.1
Currently we are using a custom built cache provider but I would like to push this functionality deeper and utilize the NH Caching abilities and not sure if this is the best way depending on how NH interacts with the Second Level Cache so please let me know if you have any question regarding the approach