OpenID-Connect-PHP
OpenID-Connect-PHP copied to clipboard
Added simple cache PSR-16
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
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 thenget
. - If the item is not in cache - it makes 3 calls to the cache server
has
,set
and thenget
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?
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);
}
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.
hi @pendenga I never got a reply from @jumbojett :)
I think this maintainer is MIA.
can be achieved with sub-classing as well -> closing
THX a lot