Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Cache prefix mode for separating tenant caches #1014

Merged
merged 163 commits into from
Apr 24, 2023

Conversation

abrardev99
Copy link
Contributor

closes #531

This PR adds support for cache prefix mode for separating tenant caches using a bootstrapper that can be enabled. The bootstrapper simply adds the prefix for cache keys.

Note: It does not work for the file driver.

Usage

enable bootstrapper in the tenancy.php config.

'bootstrappers' => [
        Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class,
        // Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
        Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
        Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
        Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
        Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper::class, // prefix cache keys
        // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
    ],

That's it. Your cache keys are prefixed now.

The cache will behave like this.

cache()->put('key', 'original-value');

tenancy()->initialize($tenant1);
cache('key') // returns null;
cache()->put('key', 'value-for-tenant1');

tenancy()->initialize($tenant2);
cache('key') // returns null;
cache()->put('key', 'value-for-tenant2');

tenancy()->end();
cache('key') // returns 'original-value';

tenancy()->initialize($tenant1);
cache('key') // returns 'value-for-tenant1'

tenancy()->initialize($tenant2);
cache('key') // // returns 'value-for-tenant2'

assets/config.php Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Nov 22, 2022

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.96%. Comparing base (dfd3972) to head (07c1d77).
Report is 6 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1014      +/-   ##
============================================
+ Coverage     89.71%   89.96%   +0.25%     
- Complexity      575      591      +16     
============================================
  Files           138      139       +1     
  Lines          1769     1814      +45     
============================================
+ Hits           1587     1632      +45     
  Misses          182      182              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stancl stancl linked an issue Nov 24, 2022 that may be closed by this pull request
assets/config.php Outdated Show resolved Hide resolved
assets/config.php Outdated Show resolved Hide resolved
src/CacheManager.php Outdated Show resolved Hide resolved
@stancl
Copy link
Member

stancl commented Apr 24, 2023

Everything seems to be completed here, so a summary of the changes:

  • A new cache bootstrapper is added (prefix). This supports all cache drivers, not just Redis
  • The original approach to cache tenancy is completely unchanged, however it is not recommended anymore and will likely be deprecated in the next major version (v5)
  • The prefix bootstrapper supports all approaches to dependency injection
    • Injecting CacheManager and manually fetching the cache store/repository every time it needs to be used (this is identical to calling cache() / Cache::)
    • Injecting CacheManager and storing a specific store/repository in a property
      • Tenancy will update the prefix of these repositories even if they're persisted anywhere in memory
  • The tags bootstrapper doesn't support any dependency injection properly:
    • The actual tenancy logic is in the CacheManager (__call()), not the repositories, so they cannot be injected. All calls must go through the CacheManager
    • The bootstrapper replaces the CacheManager in the container, so it cannot be injected
      • If a specific request happened entirely in the tenant context, without switching back to the central context, you could in theory inject the CacheManager. It will just not update when you revert back to the central context or switch to another tenant
      • For this reason, it's recommended to avoid using any cache-related dependency injection with the tags approach

@stancl stancl merged commit bd9bbe8 into archtechx:master Apr 24, 2023
@stancl stancl changed the title Cache prefix mode for separating tenant caches [4.x] Cache prefix mode for separating tenant caches Apr 24, 2023
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.

Cache prefix mode for the cache bootstrapper
4 participants