Use TTL for in-memory cache to support long-lived instances

This commit is contained in:
lukas committed 2026-10-14 16:03:21 +00:00
1 parent d52d7c3f2d
commit 62ad60f3df
3 files changed
+63 -11

No files matched your search

+37 -10
View File
@@ -35,9 +35,9 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
private $client;
private $prefix;
private $options;
private $options = [];
private $cache;
private $objects;
private $objects = [];
private $defaultReader;
private $customCacheDefaultRequest;
@@ -245,12 +245,16 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
$uriString = (string)$coid;
if (isset($this->objects[$uriString]))
// Return from in-memory cache if it exists
return $this->objects[$uriString];
$this->removeExpiredFromInMemoryCache();
$ts = microtime(true);
if (isset($this->objects[$uriString])) {
// Return from in-memory cache if it exists
$this->logInfoWithTime('Returning previous node for <'.$uriString.'>.', $ts);
return $this->objects[$uriString][1];
}
if (isset($this->options['static_config_path'])) {
$location = realpath($this->options['static_config_path'].DIRECTORY_SEPARATOR.
$coid->getHost().str_replace('/', DIRECTORY_SEPARATOR, $coid->getPath())
@@ -287,8 +291,8 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
}
$document = JsonLD::getDocument($object);
$this->objects[$uriString] = $document->getGraph()->getNode($uriString);
return $this->objects[$uriString];
$this->objects[$uriString] = [ time(), $document->getGraph()->getNode($uriString) ];
return $this->objects[$uriString][1];
}
/**
@@ -325,7 +329,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
if (!COIDParser::isValidCOID($iri)) continue;
if ($iri->getHost() != $namespaceCoid->getHost()) continue;
$this->objects[$object->getId()] = $object;
$this->objects[$object->getId()] = [ time(), $object ];
$this->putIntoCache($object->getId(), $object, $this->options['cache_ttl']);
$allIris[] = $iri;
}
@@ -366,7 +370,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
if (!COIDParser::isValidCOID($iri)) continue;
if ($iri->getHost() != $namespaceCoid->getHost()) continue;
$this->objects[$object->getId()] = $object;
$this->objects[$object->getId()] = [ time(), $object ];
$this->putIntoCache($object->getId(), $object, $this->options['cache_ttl']);
$allIris[] = $iri;
}
@@ -441,7 +445,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
$document = JsonLD::getDocument((string)$response->getBody());
$containerNode = $document->getGraph()->getNode('co-namespace-members://'.$namespaceCoid->getHost());
$reader = new NodeReader([ 'prefixes' => [ 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#' ]]);
$reader = new NodeReader([ 'prefixes' => [ 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#' ]]);
$allIris = $reader->getAllValuesIRI($containerNode, 'rdfs:member');
$this->logInfoWithTime('Fetched object list with <'.$type.'> for <'.$namespaceCoid->getHost().'> from Core API ['.$response->getStatusCode().'].', $ts);
@@ -560,4 +564,27 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
return $this->getCloudObject($this->assertAuthenticatingNamespaceAndGetId());
}
/**
* Remove expired objects from the in-memory cache. Does not affect external cache.
*/
public function removeExpiredFromInMemoryCache() {
$expiryThreshold = time() - $this->options['cache_ttl'];
foreach ($this->objects as $uriString => $entry) {
if ($entry[0] < $expiryThreshold) {
unset($this->objects[$uriString]);
}
}
return $this;
}
/**
* Remove all entries from the in-memory cache. Does not affect external cache.
*/
public function clearInMemoryCache() {
$this->objects = [];
return $this;
}
}