mirror of
https://github.com/stancl/tenancy-docs.git
synced 2025-12-12 10:14:03 +00:00
I spent some time to find out how to manually invalidate the cache, when during testing I found out this is already handled in the base Tenant model. Adding this small paragraph might help others.
1.2 KiB
1.2 KiB
| title | extends | section |
|---|---|---|
| Cached tenant lookup | _layouts.documentation | content |
Cached lookup
If you're using multiple databases, you may want to avoid making a query to the central database on each tenant request — for tenant identification. Even though the queries are very simple, the app has to establish a connection with the central database which is expensive.
To avoid this, you may enable caching on the tenant resolvers (all in the Stancl\Tenancy\Resolvers namespace):
DomainTenantResolver(also used for subdomain identification)PathTenantResolverRequestDataTenantResolver
On each of these classes, you may set the following static properties:
// TenancyServiceProvider::boot()
use Stancl\Tenancy\Resolvers;
// enable cache
DomainTenantResolver::$shouldCache = true;
// seconds, 3600 is the default value
DomainTenantResolver::$cacheTTL = 3600;
// specify some cache store
// null resolves to the default cache store
DomainTenantResolver::$cacheStore = 'redis';
Cache invalidation
Updating and saving a Tenant model's attributes will cause the cached entry for this model to be invalidated when DomainTenantResolver::$shouldCache is set to true.