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

Cache issues #334

Closed
sclubricants opened this issue Apr 12, 2021 · 10 comments · Fixed by #386
Closed

Cache issues #334

sclubricants opened this issue Apr 12, 2021 · 10 comments · Fixed by #386

Comments

@sclubricants
Copy link
Contributor

sclubricants commented Apr 12, 2021

I'm noticing that some of the methods don't allow a null array to be cached. In the group model if a user returns no groups then the empty array should be cached. To fix you can check if cached value !is_array() as follows. I'm sure there are many places this could be fixed.

  public function getGroupsForUser(int $userId)
    { 
        if (! is_array($found = cache("{$userId}_groups")))
        {
            $found = $this->builder()
                ->select('auth_groups_users.*, auth_groups.name, auth_groups.description')
                ->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups.id', 'left')
                ->where('user_id', $userId)
                ->get()->getResultArray();

            cache()->save("{$userId}_groups", $found, 300);
        }

        return $found;
    }
@sfadschm
Copy link
Contributor

+1 for this.
E.g.:

if (! $found = cache("{$userId}_permissions"))

If the user does not have any permissions, cache() will return array(0) { }, but !array(0) { } will return true and thus ignore the cache and reload the permissions from the database.

This can bring along some (relatively) heavy load espacially in loops.

@sfadschm
Copy link
Contributor

if (! is_array($found = cache("{$userId}_groups")))

I am not sure if this is the intended way, however, it should work out.
Looking at the cache()->get() implementation for e.g. the FileHandler:

return is_array($data) ? $data['data'] : null;

I would think applying an is_null() check is more appropriate.

@MGatner
Copy link
Collaborator

MGatner commented Jun 21, 2021

Good catch! I would suggest something like this:

if (null !== $found = cache("{$userId}_permissions")) 

Would you submit a PR for these changes? It sounds like the may be needed in multiple places.

@sfadschm
Copy link
Contributor

On its way :)

@sfadschm
Copy link
Contributor

Should be null === $found .... though, I think?

@MGatner
Copy link
Collaborator

MGatner commented Jun 21, 2021

Yes! Sorry, I didn't look at the actual code, just the preview above.

@sclubricants
Copy link
Contributor Author

sclubricants commented Jun 22, 2021

This is kind of beside the point but would anybody have interest in having the cache time moved to a config file?

cache()->save("{$user->id}_groups", $cacheGroup, 300);

cache()->save("{$user->id}_groups", $cacheGroup, $config->time);

@MGatner
Copy link
Collaborator

MGatner commented Jun 22, 2021

The framework recently added a default TTL value to the Cache Config. I think it would make sense for this library to use that (with the current as a fallback) so devs could change it centrally or on-the-fly.

@sfadschm
Copy link
Contributor

I would think so, too.
Optimally have a $ttl in the AuthConfig (defaulting to 300 to keep the current state) that defaults to the frameworks new value if it is completely removed.
Reading your two posts again, I think thats exactly what you said 😆

@MGatner
Copy link
Collaborator

MGatner commented Jun 23, 2021

Yes, agree. I think: this library should have its own Config option but should use \App\Config\Cache::$ttl as a fallback.

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 a pull request may close this issue.

3 participants