OpenID-Connect-PHP icon indicating copy to clipboard operation
OpenID-Connect-PHP copied to clipboard

Added simple cache PSR-16

Open mabuonomo opened this issue 5 years ago • 5 comments

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 avatar Jan 29 '20 11:01 mabuonomo

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?

mattfawcett avatar Feb 05 '20 16:02 mattfawcett

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);
}

mabuonomo avatar Feb 05 '20 16:02 mabuonomo

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.

pendenga avatar Sep 08 '20 15:09 pendenga

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

mabuonomo avatar Sep 09 '20 09:09 mabuonomo

I think this maintainer is MIA.

mvdobrinin avatar Nov 30 '20 06:11 mvdobrinin

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

THX a lot

DeepDiver1975 avatar Apr 22 '24 09:04 DeepDiver1975