Skip to content

Commit

Permalink
Merge branch 'subhh-local' of github.com:subhh/vufind into subhh-local
Browse files Browse the repository at this point in the history
  • Loading branch information
Maus committed Mar 27, 2024
2 parents 85c4e46 + 88ec3b3 commit ab3919c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 16 deletions.
1 change: 1 addition & 0 deletions module/VuFind/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@
'VuFind\QRCode\Loader' => 'VuFind\QRCode\LoaderFactory',
'VuFind\Recommend\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'VuFind\Record\Cache' => 'VuFind\Record\CacheFactory',
'VuFind\Record\Cache\Strategy' => 'VuFind\Record\Cache\RecordCacheDbStrategyFactory',
'VuFind\Record\FallbackLoader\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'VuFind\Record\Loader' => 'VuFind\Record\LoaderFactory',
'VuFind\Record\Router' => 'VuFind\Service\ServiceWithConfigIniFactory',
Expand Down
30 changes: 17 additions & 13 deletions module/VuFind/src/VuFind/Record/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ class Cache implements \Laminas\Log\LoggerAwareInterface
protected $cacheConfig;

/**
* Database table used by cache
*
* @var \VuFind\Db\Table\Record
* @var Cache\RecordCacheStrategyInterface
*/
protected $recordTable;
protected $strategy;

/**
* Record driver plugin manager
Expand All @@ -91,10 +89,10 @@ class Cache implements \Laminas\Log\LoggerAwareInterface
public function __construct(
RecordFactory $recordFactoryManager,
Config $config,
Record $recordTable
Cache\RecordCacheStrategyInterface $strategy
) {
$this->cacheConfig = $config;
$this->recordTable = $recordTable;
$this->strategy = $strategy;
$this->recordFactoryManager = $recordFactoryManager;

$this->setContext(Cache::CONTEXT_DEFAULT);
Expand All @@ -113,7 +111,7 @@ public function createOrUpdate($recordId, $source, $rawData)
{
if (isset($this->cachableSources[$source])) {
$this->debug("Updating {$source}|{$recordId}");
$this->recordTable->updateRecord($recordId, $source, $rawData);
$this->strategy->update($recordId, new Cache\RecordCacheEntry($source, $rawData));
}
}

Expand All @@ -128,13 +126,15 @@ public function createOrUpdate($recordId, $source, $rawData)
public function lookup($id, $source)
{
$this->debug("Checking {$source}|{$id}");
$record = $this->recordTable->findRecord($id, $source);
$record = $this->strategy->get($id, $source);
$this->debug(
"Cached record {$source}|{$id} "
. ($record !== false ? 'found' : 'not found')
);
try {
return $record !== false ? [$this->getVuFindRecord($record)] : [];
if ($record) {
return $this->getVuFindRecord($record);
}
} catch (\Exception $e) {
$this->logError(
'Could not load record {$source}|{$id} from the record cache: '
Expand All @@ -161,7 +161,11 @@ public function lookupBatch($ids, $source)

$this->debug("Checking $source batch: " . implode(', ', $ids));
$vufindRecords = [];
$cachedRecords = $this->recordTable->findRecords($ids, $source);
$cachedRecords = [];
foreach ($ids as $id) {
$cachedRecords[] = $this->strategy->get($id, $source);
}
$cachedRecords = array_filter($cachedRecords);
foreach ($cachedRecords as $cachedRecord) {
try {
$vufindRecords[] = $this->getVuFindRecord($cachedRecord);
Expand Down Expand Up @@ -273,10 +277,10 @@ public function isCachable($source)
*
* @return \VuFind\RecordDriver\AbstractBase
*/
protected function getVuFindRecord($cachedRecord)
protected function getVuFindRecord(Cache\RecordCacheEntry $cachedRecord)
{
$source = $cachedRecord['source'];
$doc = unserialize($cachedRecord['data']);
$source = $cachedRecord->source;
$doc = unserialize($cachedRecord->data);

// Solr records are loaded in special-case fashion:
if ($source === 'VuFind' || $source === 'Solr') {
Expand Down
29 changes: 29 additions & 0 deletions module/VuFind/src/VuFind/Record/Cache/RecordCacheDbStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace VuFind\Record\Cache;

use VuFind\Db\Table\Record;

final class RecordCacheDbStrategy implements RecordCacheStrategyInterface
{
/** @var Record */
private $recordTable;

public function __construct (Record $recordTable)
{
$this->recordTable = $recordTable;
}

public function update (string $recordId, RecordCacheEntry $record) : void
{
$this->recordTable->updateRecord($recordId, $record->source, $record->data);
}

public function get (string $recordId, string $source) : ?RecordCacheEntry
{
if ($record = $this->recordTable->findRecord($recordId, $source)) {
return new RecordCacheEntry($record['source'], $record['data']);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace VuFind\Record\Cache;

use Interop\Container\ContainerInterface;

use Laminas\ServiceManager\Factory\FactoryInterface;

final class RecordCacheDbStrategyFactory implements FactoryInterface
{
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
) {
if (!empty($options)) {
throw new \Exception('Unexpected options passed to factory.');
}

$table = $container->get(\VuFind\Db\Table\PluginManager::class)->get('Record');
return new RecordCacheDbStrategy($table);
}
}
19 changes: 19 additions & 0 deletions module/VuFind/src/VuFind/Record/Cache/RecordCacheEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace VuFind\Record\Cache;

final class RecordCacheEntry
{
/** @var string */
public $source;

/** @var mixed */
public $data;

/** @param mixed[] $data */
public function __construct (string $source, $data)
{
$this->source = $source;
$this->data = $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace VuFind\Record\Cache;

interface RecordCacheStrategyInterface
{
public function update (string $recordId, RecordCacheEntry $record) : void;
public function get (string $recordId, string $source) : ?RecordCacheEntry;
}
3 changes: 2 additions & 1 deletion module/VuFind/src/VuFind/Record/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ public function __invoke(
if (!empty($options)) {
throw new \Exception('Unexpected options passed to factory.');
}

return new $requestedName(
$container->get(\VuFind\RecordDriver\PluginManager::class),
$container->get(\VuFind\Config\PluginManager::class)->get('RecordCache'),
$container->get(\VuFind\Db\Table\PluginManager::class)->get('Record')
$container->get('VuFind\Record\Cache\Strategy')
);
}
}
9 changes: 8 additions & 1 deletion module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,14 @@ public function getSource()
*/
public function getSubtitle()
{
return $this->fields['title_sub'] ?? '';
if (array_key_exists('title_sub', $this->fields)) {
if (is_array($this->fields['title_sub'])) {
$subtitle = $this->fields['title_sub'][0];
} else {
$subtitle = $this->fields['title_sub'];
}
}
return $subtitle ?? '';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,12 @@ protected function getRecordFactoryManager(): \PHPUnit\Framework\MockObject\Mock
*/
protected function getRecordCache()
{
$table = $this->getRecordTable();
$strategy = new \VuFind\Record\Cache\RecordCacheDbStrategy($table);
$recordCache = new Cache(
$this->getRecordFactoryManager(),
$this->getConfig(),
$this->getRecordTable()
$strategy
);

return $recordCache;
Expand Down

0 comments on commit ab3919c

Please sign in to comment.