mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Single database multi-tennant with different admin and user queries - will cache invalidate correctly? #257

Closed booni3 closed 5 years ago

booni3 commented 5 years ago

@mikebronner this is a follow on from #187.

As you suggested, I removed the cache prefix and things do seem to be working as expected. I just wanted to confirm one thing with you though as this is initially how I thought I started and only added the prefix to try and resolve my issues.

My Use Case

I have a multi-tenant DB but the admin (who has access to all tenants) makes the majority of the changes within the database. A where` tenant_id` = '1233232' is automatically appended to all queries, so therefore the admin and the tenant will always make different queries to the database.

For example:

// Admin User Performs Update

update `order_addresses` set `full_name` = 'name' where `id` = '27279152-e30e-4353-9d6e-3b9e2ab61204' and `tenant_id` = '1233232'

// Tenant User Performs Update

update `order_addresses` set `full_name` = 'name' where `id` = '27279152-e30e-4353-9d6e-3b9e2ab61204'

Similar on the select, one will have the where `tenant_id` = '1233232' appended.

Can you confirm that even though the query in this case is different the model cache will still be invalidated? I feel a bit stupid writing this as I am sure this must be the case, but am also pretty sure I did have issues previously.

If so, i'll get upgraded to 5.8 and get your package up and running again!

Thanks a lot.

mikebronner commented 5 years ago

Hi @booni3! Yes, the model cache will be invalidated, regardless of tenant.

(That's a good point. I should make sure that the cache is invalidated at a database level, not across all databases, for apps that have multi-tenant setups where each tenant is in its own database.)

I would think you would want the queries the other way around, though, such that the Tenant queries always have the tenant_id clause, and the admin queries have no tenant_id. That way the admin can see all records from all tenants. It seems to me that the way you have it now the tenant can see records from any tenant? This is not relevant in an update query, but much more so in select queries.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.