Skip to content

Comments

Add caching layer for TenantFinder#627

Closed
moisish wants to merge 2 commits intospatie:mainfrom
moisish:tenant-finder-caching
Closed

Add caching layer for TenantFinder#627
moisish wants to merge 2 commits intospatie:mainfrom
moisish:tenant-finder-caching

Conversation

@moisish
Copy link
Contributor

@moisish moisish commented Feb 13, 2026

Summary

Tenant resolution via TenantFinder hits the database on every request. This PR adds an opt-in caching decorator that wraps any TenantFinder implementation and caches the resolved tenant.

What's included

  • CachingTenantFinder — a decorator that wraps the configured TenantFinder, caches the result, and delegates to the inner finder on cache miss. Supports configurable TTL, cache store, and forever-cache (ttl: 0). Null results are intentionally not cached.
  • CachesTenantFinderResults — interface exposing forget(Request) and forgetByKey(string) for cache invalidation, allowing other packages to type-hint without depending
    on the concrete class.
  • Config-driven — disabled by default. Users enable it and optionally swap the class for custom cache key strategies (e.g. subdomain-based resolution):
'tenant_finder_cache' => [
    'enabled' => false,
    'class' => CachingTenantFinder::class,
    'store' => null,
    'ttl' => 300, // seconds, 0 = forever
],
  • Extensible — override resolveCacheKey() or cachePrefix() for custom cache key derivation:
 class SubdomainCachingTenantFinder extends CachingTenantFinder
 {
     protected function resolveCacheKey(Request $request): string
     {
         return $this->cachePrefix() . explode('.', $request->getHost())[0];
     }
 }

@masterix21
Copy link
Collaborator

Hey @moisish, thanks for the PR!

Since TenantFinder is fully swappable via config, you can add a caching layer in your own implementation without needing changes to the package itself — keeping things flexible per project. Also, the test suite is currently failing, so we can't merge this as-is.

Thanks again for the contribution, we appreciate it!

@masterix21 masterix21 closed this Feb 20, 2026
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

Successfully merging this pull request may close these issues.

2 participants