@@ -23,6 +23,9 @@ class TenancyServiceProvider extends ServiceProvider
2323 public static bool $ registerForgetTenantParameterListener = true ;
2424 public static bool $ migrateFreshOverride = true ;
2525
26+ /** @internal */
27+ public static Closure |null $ adjustCacheManagerUsing = null ;
28+
2629 /* Register services. */
2730 public function register (): void
2831 {
@@ -80,8 +83,32 @@ public function register(): void
8083 return new Commands \Seed ($ app ['db ' ]);
8184 });
8285
86+ // todo0 check how the caching in the facade affects our logic here
87+ // todo0 check what happens if globalCache is injected - it may be
88+ // problematic if it's injected before adjustCacheManagerUsing
89+ // was used
90+
8391 $ this ->app ->bind ('globalCache ' , function ($ app ) {
84- return new CacheManager ($ app );
92+ // We create a separate CacheManager to be used for "global" cache -- cache that
93+ // is always central, regardless of the current context.
94+ //
95+ // Importantly, we use a regular binding here, not a singleton. Thanks to that,
96+ // any time we resolve this cache manager, we get a *fresh* instance -- an instance
97+ // that was not affected by any scoping logic.
98+ //
99+ // This works great for cache stores that are *directly* scoped, like Redis or
100+ // any other tagged or prefixed stores, but it doesn't work for the database driver.
101+ //
102+ // When we use the DatabaseTenancyBootstrapper, it changes the default connection,
103+ // and therefore the connection of the database store that will be created when
104+ // this new CacheManager is instantiated again.
105+ $ manager = new CacheManager ($ app );
106+
107+ if (static ::$ adjustCacheManagerUsing !== null ) {
108+ (static ::$ adjustCacheManagerUsing )($ manager );
109+ }
110+
111+ return $ manager ;
85112 });
86113 }
87114
0 commit comments