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

Added simple cache PSR-16 #195

Closed
wants to merge 1 commit into from

Conversation

mabuonomo
Copy link

@mabuonomo mabuonomo commented Jan 29, 2020

This PR added simple cache PSR-16, it need to use a cacheable fetch of the jwks keys and document openid-configuration

Issue #194

List of common tasks a pull request require complete

  • Changelog entry is added or the pull request don't alter library's functionality

@mabuonomo mabuonomo requested a review from jumbojett February 2, 2020 20:21
@mattfawcett
Copy link

mattfawcett commented Feb 5, 2020

I was looking to add something similar. I think it could probably be made to be more efficient though because:

  • If the item is already is in cache - it makes 2 calls to the cache server has and then get.
  • If the item is not in cache - it makes 3 calls to the cache server has, set and then get

Assuming the cache is likely running on some other server, fewer calls would be best to avoid latency adding up. I think at most there only ever needs to be 2 calls, first attempt to get, if that returns nothing then set it after pulling the value. As we already have the value at that point, a call to get is no longer needed.

Something like this is what I'm thinking

private function fetchCacheableURL($url, $ttl = 3600)
{
    try {
        if($this->cache !== null) {
            $key = md5($url);

            $contents = $this->cache->get($key);

            if (is_null($contents)) {
                $contents = $this->fetchURL($url);

                $this->cache->set($key, $contents, $ttl);
            }

            return $contents;
        }
    } catch (\Psr\SimpleCache\InvalidArgumentException $ex){

    }

    return $this->fetchURL($url);
}

Thoughts?

@mabuonomo
Copy link
Author

I agree, only two things to change

  • use === null indeed is_null
  • set a default value on get call
private function fetchCacheableURL($url, $ttl = 3600)
{
    try {
        if($this->cache !== null) {
            $key = md5($url);

            $contents = $this->cache->get($key, null);

            if ($contents === null) {
                $contents = $this->fetchURL($url);

                $this->cache->set($key, $contents, $ttl);
            }

            return $contents;
        }
    } catch (\Psr\SimpleCache\InvalidArgumentException $ex){

    }

    return $this->fetchURL($url);
}

@pendenga
Copy link

pendenga commented Sep 8, 2020

I'd like to see this PR go through. Anything I can do to help?

Also, I was thinking about cache invalidation. I'm using FusionAuth and they have webhooks that can fire when keys are updated. In the webhook target on my implementation I could call a function on this client to invalidate the cache if it were available.

@mabuonomo
Copy link
Author

mabuonomo commented Sep 9, 2020

hi @pendenga I never got a reply from @jumbojett :)

@mvdobrinin
Copy link

I think this maintainer is MIA.

@DeepDiver1975
Copy link
Collaborator

can be achieved with sub-classing as well -> closing

THX a lot

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.

5 participants