mikebronner / laravel-model-caching

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

how to *not* keep keys separate when using multiple database connections? #267

Closed vesper8 closed 5 years ago

vesper8 commented 5 years ago

I recently converted one of my projects that was using a single database to using multiple databases. However this is not because of multi-tenancy it's more of an organizational preference

I read this

*Multiple Database Connections*
Thanks to @dtvmedia for suggestion this feature. This is actually a more robust solution than cache-prefixes.

Keeping keys separate for multiple database connections is automatically handled. This is especially important for multi-tenant applications, and of course any application using multiple database connections.

which I think is causing me a problem. Not sure what to make of it. I have relationships that cross database connections. For example one DB has a users table and that user has relationships with models in other databases. Ever since moving to multiple databases, the cache is no longer cleared when I insert new items, previously it was working fine

I tried adding a single shared prefix on my "CachedModel" class that all my models except my User model extend

protected $cachePrefix = 'ns-api';

But that didn't resolve my issue.

Would appreciate some guidance on how to make this work. Thanks!

mikebronner commented 5 years ago

Hi @vesper8, sorry for the delay in my response (I have been tied up in other projects).

I think this could be an issue with how the keys are generated. I will double-check that the relationships also include the database information.

vesper8 commented 5 years ago

thank you, please let me know what you find out, in the meantime I've had to disable the package but I would really like to use it again

mikebronner commented 5 years ago

@vesper8 Still thinking about how to implement this. This is not actually a bug, but intended functionality, but I see how this could break things. Basically what we would need to do is have a switch that eliminates the database from being tracked in the cache keys. I'll try to add this in a way that works with the default functionality, but lets you override it if needed.

vesper8 commented 5 years ago

great! I understand that it's an edge case.. not many people are in my situation dealing with a single project that uses 5 different databases. Would be great if you could make it work without breaking anything : ) thanks!

mikebronner commented 5 years ago

@vesper8 Give 0.6.1 a try and let me know how it goes.