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

Preferences model are locked to single user because of model caching #32

Closed
TheRND opened this issue Sep 1, 2020 · 7 comments
Closed

Comments

@TheRND
Copy link

TheRND commented Sep 1, 2020

Now admin user can't set preferences for different users, because Preferences model constructor has userId parameter and it's instance cached then. So getPreferencesModel with different userId is simply ignored, which is not correct. I'm suggesting you to remove userId from constructor and move it to method parameters, or respect userId constructor parameter while caching model.

@gnello
Copy link
Owner

gnello commented Sep 4, 2020

Hi @TheRND, thanks for pointing that out!
I think is better remove userId from constructor and move it to method parameters, as you said.

I'll fix it as soon as I can.

@gnello
Copy link
Owner

gnello commented Sep 5, 2020

Hi @TheRND,
I finally decided to not cache the model, keeping the "userId" parameter in the constructor. In this way no breaking changes were introduced.

Thanks again for pointing this out!

@gnello gnello closed this as completed Sep 5, 2020
@TheRND
Copy link
Author

TheRND commented Sep 8, 2020

The better way, probably, is to cache model, but implement setter method and explicitly set userId on retrieval in Dirver::getPreferencesModel($userId)?

@gnello
Copy link
Owner

gnello commented Sep 13, 2020

Oh that could be a good idea, you mean something like this:

 /**
     * @param $userId
     * @return PreferenceModel
     */
    public function getPreferenceModel($userId)
    {
        if (!isset($this->models[PreferenceModel::class][$userId])) {
            $this->models[PreferenceModel::class][$userId] = new PreferenceModel($this->container['client'], $userId);
        }

        return $this->models[PreferenceModel::class][$userId];
    }

so every instance of a user id is cached. I wonder, however, if it does not overload the memory where there are many retrieval with different user ids.

@TheRND
Copy link
Author

TheRND commented Sep 13, 2020

Not exactly, I meant something like this:

/**
     * @param $userId
     * @return PreferenceModel
     */
    public function getPreferenceModel($userId)
    {
        if (!isset($this->models[PreferenceModel::class])) {
            $this->models[PreferenceModel::class] = new PreferenceModel($this->container['client']); // Remove userid from constructor and move it into setter
        }

        return $this->models[PreferenceModel::class]->setUserId($userId); // Transient setter
    }

So we've got single instance cached and kept getPreferenceModel($userId) method signature for backward compatibility

@gnello
Copy link
Owner

gnello commented Sep 18, 2020

Great! I like it, I will implement your solution this weekend.

@gnello
Copy link
Owner

gnello commented Sep 20, 2020

Done 😊

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

2 participants