Skip to content

Commit

Permalink
Store the response cache under idenfitiers of all resources in the co…
Browse files Browse the repository at this point in the history
…llection

For the images and manifest modes the output is the same for all
resources within a described collection and the collection itself.
Therefore it makes sense to cache the output using ids of all described
resources and the collection itself.

(closes #4)
  • Loading branch information
zozlak committed Oct 26, 2024
1 parent 8283566 commit 4af2824
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "^8.1",
"psr/log": "*",
"acdh-oeaw/arche-diss-cache": "^0.7",
"acdh-oeaw/arche-diss-cache": "^0.8",
"zozlak/logging": "^1.0"
},
"autoload": {
Expand Down
9 changes: 5 additions & 4 deletions src/acdhOeaw/arche/iiifManifest/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ private function findFirstResource(): array {
$this->log?->info("resolved: $resolvedRes collection: $collectionRes first: $firstRes");

// for better caching
//$node = $this->meta->getNode();
//$graph->add(DF::quad($node, $this->schema->id, $firstRes));
//$graph->add(DF::quad($node, $this->schema->id, $collectionRes));
$node = $this->meta->getNode();
$graph->add(DF::quad($node, $this->schema->id, $firstRes));
$graph->add(DF::quad($node, $this->schema->id, $collectionRes));

return [$firstRes, $collectionRes];
}
Expand Down Expand Up @@ -179,6 +179,7 @@ private function getManifest(TermInterface $firstRes,
$graph = $this->meta->getDataset();

// first check if a collection doesn't have a custom manifest
/** @phpstan-ignore property.notFound */
$customManifest = $graph->getObjectValue(new QT($collectionRes, $this->schema->iiifManifest));
if (!empty($customManifest) && $customManifest !== $this->config->defaultIiifManifestUri) {
$data = @file_get_contents($customManifest);
Expand Down Expand Up @@ -271,7 +272,7 @@ private function getNextSbj(DatasetInterface $data, PT $collectionTmpl): TermInt
if ($graph->any($collectionTmpl->withSubject($i))) {
$sbj = $i;
// for better caching
//$graph->add(DF::quad($node, $this->schema->id, $i));
$graph->add(DF::quad($node, $this->schema->id, $i));
}
}
return $sbj;
Expand Down
49 changes: 46 additions & 3 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@

use quickRdf\DatasetNode;
use quickRdf\DataFactory as DF;
use termTemplates\PredicateTemplate as PT;
use quickRdfIo\Util as RdfIoUtil;
use acdhOeaw\arche\lib\dissCache\CachePdo;
use acdhOeaw\arche\lib\dissCache\CacheItem;
use acdhOeaw\arche\lib\dissCache\ResponseCache;
use acdhOeaw\arche\lib\dissCache\RepoWrapperInterface;
use acdhOeaw\arche\lib\Schema;
use acdhOeaw\arche\lib\SearchConfig;
use acdhOeaw\arche\lib\RepoResourceInterface;
use acdhOeaw\arche\lib\dissCache\ResponseCacheItem;
use acdhOeaw\arche\iiifManifest\Resource as IiifResource;
Expand All @@ -52,6 +58,14 @@ static public function setUpBeforeClass(): void {
self::$schema = new Schema(self::$cfg->iiifManifest->schema);
}

public function setUp(): void {
parent::setUp();

foreach (glob('/tmp/cachePdo*') as $i) {
unlink($i);
}
}

public function testModeImage(): void {
$headers = ['Location' => 'https://loris.acdh.oeaw.ac.at/13/info.json'];
$expected = new ResponseCacheItem("Redirect to https://loris.acdh.oeaw.ac.at/13/info.json", 302, $headers, false);
Expand Down Expand Up @@ -97,12 +111,41 @@ public function testModeManifest(): void {

public function testHasIiifManifest(): void {
// not a iiif-manifest file but we currenlty have none in ARCHE
$expected = json_decode(file_get_contents(__DIR__.'/data_hasIiifManifest.json'), true);
$expected = json_decode(file_get_contents(__DIR__ . '/data_hasIiifManifest.json'), true);
$this->checkOutput($expected, $this->getOutput(self::COLLECTION_URL, IiifResource::MODE_MANIFEST, __DIR__ . '/meta_hasIiifManifest.ttl'));
}

private function checkOutput(array $expected,
ResponseCacheItem $actual): void {
public function testCacheWholeCollection(): void {
$cfg = self::$cfg->dissCacheService;
$sc = new SearchConfig();
$sc->metadataMode = $cfg->metadataMode;
$sc->metadataParentProperty = $cfg->parentProperty;
$sc->resourceProperties = $cfg->resourceProperties;
$sc->relativesProperties = $cfg->relativesProperties;

$graph = new DatasetNode(DF::namedNode(self::RESOURCE_URL));
$graph->add(RdfIoUtil::parse(__DIR__ . '/meta.ttl', new DF(), 'text/turtle'));
$repoRes = $this->createStub(RepoResourceInterface::class);
$repoRes->method('getGraph')->willReturn($graph);
$repoRes->method('getIds')->willReturnCallback(fn() => $graph->listObjects(new PT(self::$schema->id))->getValues());
$repo = $this->createStub(RepoWrapperInterface::class);
$repo->method('getResourceById')->willReturn($repoRes);
$repo->method('getModificationTimestamp')->willReturn(PHP_INT_MAX);

$db = new CachePdo('sqlite:/tmp/cachePdo_db.sqlite', 'iiifManifest');
$clbck = fn($res, $param) => IiifResource::cacheHandler($res, $param, self::$cfg->iiifManifest);
$cache = new ResponseCache($db, $clbck, 0, 0, [$repo], $sc);

$cache->getResponse(['images'], self::RESOURCE_URL);
$resDbItem = $db->get(self::RESOURCE_URL);
$collDbItem = $db->get(self::COLLECTION_URL);
$res2DbItem = $db->get('https://arche.acdh.oeaw.ac.at/api/11');
$this->assertInstanceOf(CacheItem::class, $resDbItem);
$this->assertEquals($collDbItem, $collDbItem);
$this->assertEquals($collDbItem, $res2DbItem);
}

private function checkOutput(array $expected, ResponseCacheItem $actual): void {
$this->assertEquals(200, $actual->responseCode);
$this->assertEquals(['Content-Type' => 'application/json'], $actual->headers);
$this->assertFalse($actual->hit);
Expand Down

0 comments on commit 4af2824

Please sign in to comment.