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

Dynamically set cache_key is not being used in multi-tenant setup #1129

Closed
vannut opened this issue Jun 18, 2019 · 4 comments
Closed

Dynamically set cache_key is not being used in multi-tenant setup #1129

vannut opened this issue Jun 18, 2019 · 4 comments

Comments

@vannut
Copy link

vannut commented Jun 18, 2019

I've got a multi-tenant app with a database per tenant.

To make sure the cached permissions don't get transferred among the different tenants i want the cache-keys to be tenant-dependent.

eg: 01ddj7nhva0t1f2ke0j7r6g19s_permissions instead of spatie.permissions.cache

At runtime a middleware (first one in the web-group) checks the tennant existence, connects the database and changes some config() variables on the fly. Among these is the cache-key for this tenants permissions

config([
    'app.url'=> 'https://'.$this->tenant->fqdn,
    ...
    'permission.cache.key' => $this->tenant->id.'_permissions'
]);

When I look in telescope at the cache keys I don't see the specified tenant aware cache key; but the key statically set.

In a testController is have this:

return [
   'cache_key' => config('permission.cache.key'),
   'can' => auth()->user()->can('manage-users')
]

Which returns the correct cache_key as a config parameter.

{
"cache_key": "01ddj...7r6g19s_permissions",
"can": "false"
}

As I use the package to retrieve the ability to manage-users the cache gets hit and in Telescope I see the hit being registered. But its registered with the key defined in the file permission.php instead of the dynamically set key.

I'm almost sure I'm missing something here. Any ideas why the newly set config variable is not being used?

@vannut vannut changed the title Dynamically cache_key is not being used in multi-tenant setup Dynamically set cache_key is not being used in multi-tenant setup Jun 18, 2019
@drbyte
Copy link
Collaborator

drbyte commented Jun 19, 2019

Are you using hyn/multi-tenant? If so, I wonder if you're running into the issue discussed here where the cache is already instantiated early and needs overriding with some namespace trickery?
tenancy/multi-tenant#693

@vannut
Copy link
Author

vannut commented Jun 19, 2019

I'm actually not using that package. I read that issue and this one and in the end found this post.

Tl;dr; Because Telescope initiates the cache very early the change of config() vars is not picked up. Redis implementation of the cache has a setPrefix() method. So in the post above it gives a workaround to set the prefix of the redis store dynamically.

So the problem lies with the 'early' init of the cache store, and not with the package :)

@drbyte drbyte closed this as completed Jun 30, 2019
@scriptmint
Copy link

Did you find any solution for this? I am facing same issue.

@drbyte
Copy link
Collaborator

drbyte commented Jan 14, 2020

Did you find any solution for this? I am facing same issue.

When I was testing with stancl/tenancy I had no issues with this.

Are you seeing the problem "with Telescope"? Is it actually "real" independent of Telescope?

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

No branches or pull requests

3 participants