Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key Prefix #37

Open
c5racing opened this issue Mar 2, 2019 · 3 comments
Open

Key Prefix #37

c5racing opened this issue Mar 2, 2019 · 3 comments

Comments

@c5racing
Copy link

c5racing commented Mar 2, 2019

I'm looking for an EF Caching solution to use in our solution. We have a multi-tenant platform so I'd like to prefix our cache key with a tenant key. Does the solution allow for such a thing or extendable to do so?

@moozzyk
Copy link
Owner

moozzyk commented Mar 2, 2019

I think you need to define what you mean by "multi-tenant platform". Is it that:

  • each customer has its own dedicated database but they all have the same schema
  • data for different customers are store in the same database

If this is the former - the key contains database name so if you have multiple databases (the first bullet point) results for one database will not conflict with results for the same query with the same params for a different database.

If this is the latter and you have just one database for all customers then likely each query will contain the customer id. Cache keys contain the query along with the parameter values, so again results for the queries that differ by parameter values will be stored as separate cache entries (see below).

Currently, customizing key creation is not possible and I have no plans to enable this.

For the reference - here is how cache keys are created:

private string CreateKey()
{
return
string.Format(
"{0}_{1}_{2}",
Connection.Database,
CommandText,
string.Join(
"_",
Parameters.Cast<DbParameter>()
.Select(p => string.Format("{0}={1}", p.ParameterName, p.Value))));
}

@c5racing
Copy link
Author

c5racing commented Mar 2, 2019

Thank you for the information. Data for different customers are stored in the same database. The query does contain the customer id; however, we're noticing that when one customer updates data in a table, it clears cache for all customers. It would be nice to specify a customerid so that it wouldn't be clearing all customer cache when a table update is done.

Another option would allow a configuration of the Key, such as allowing a full connectionstring vs. just the database name. This way, I can set the Application Name to be unique amongst customers.

@moozzyk
Copy link
Owner

moozzyk commented Mar 2, 2019

Unfortunately this is not the matter of how key is generated. The cache is not trying to understand queries and updates to the database. When results are cached EFCache only saves information about what tables were used to retrieve the data. If data in any of these tables was modified the cached entries are invalidated.
I always recommend looking at access patterns and using caching only for tables that do not change very frequently. I think it might be hard in your case because access patterns for the same tables might be different depending on the customer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants