romegasoftware/Multitenancy

Middleware hits database everytime?

m-maharjan opened this issue · 3 comments

Hi, I realized that middleware queries database each time for a route. Instead of that can we like put the tenant info in session or something like that? Please suggest me its pros and cons.

Hi, well if you want to do it by session you can create you own middlware which extends the RomegaDigital\Multitenancy\Middleware\TenantMiddleware. Then you would be able to write something that looks to the session (after the session is set) instead of the database every time.

Regarding the pros and cons every route quering:

  • Pros:
    -- Always user data up to date if its belongs to the tenant
  • Cons:
    -- Every request multiple queries to the database.

A solutions might be that you check if its the session if so than do a Sweeping Lottery. So when the lottery is succesfull that you would check if the user still belongs to the tenant. This way you decrease the amount of queries.

A good approach may to to cache the list of users and update the cache on new user creation. Depends on the specifics of your situation (as always) though.

The officially recommended way would be to create your own middleware that extends or replaces the middleware provided. It can easily be altered as your use case requires. This functionality falls outside the scope of this package.

Thanks for the suggestion!