Skip to content

Commit

Permalink
Merge branch 't/improved-caching' into subhh-local
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj committed Jul 15, 2024
2 parents ab3919c + 5e8b6c9 commit b43950c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 24 deletions.
34 changes: 12 additions & 22 deletions module/VuFind/src/VuFind/Record/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
namespace VuFind\Record;

use Laminas\Config\Config as Config;
use VuFind\Db\Table\Record as Record;
use VuFind\RecordDriver\PluginManager as RecordFactory;

/**
Expand Down Expand Up @@ -60,10 +59,8 @@ class Cache implements \Laminas\Log\LoggerAwareInterface
*/
protected $cacheConfig;

/**
* @var Cache\RecordCacheStrategyInterface
*/
protected $strategy;
/** @var Cache\AdapterInterface */
protected $adapter;

/**
* Record driver plugin manager
Expand All @@ -84,15 +81,14 @@ class Cache implements \Laminas\Log\LoggerAwareInterface
*
* @param RecordFactory $recordFactoryManager Record loader
* @param Config $config VuFind main config
* @param Record $recordTable Record Table
*/
public function __construct(
RecordFactory $recordFactoryManager,
Config $config,
Cache\RecordCacheStrategyInterface $strategy
Cache\AdapterInterface $adapter
) {
$this->cacheConfig = $config;
$this->strategy = $strategy;
$this->adapter = $adapter;
$this->recordFactoryManager = $recordFactoryManager;

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

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

$this->debug("Checking $source batch: " . implode(', ', $ids));
$vufindRecords = [];
$cachedRecords = [];
foreach ($ids as $id) {
$cachedRecords[] = $this->strategy->get($id, $source);
}
$cachedRecords = array_filter($cachedRecords);
foreach ($cachedRecords as $cachedRecord) {
$record = $this->adapter->get($source, $id);

try {
$vufindRecords[] = $this->getVuFindRecord($cachedRecord);
$vufindRecords[] = $this->getVuFindRecord($source, $record);
} catch (\Exception $e) {
$this->logError(
'Could not load record ' . $cachedRecord['source'] . '|'
Expand Down Expand Up @@ -277,10 +268,9 @@ public function isCachable($source)
*
* @return \VuFind\RecordDriver\AbstractBase
*/
protected function getVuFindRecord(Cache\RecordCacheEntry $cachedRecord)
protected function getVuFindRecord($source, $data)
{
$source = $cachedRecord->source;
$doc = unserialize($cachedRecord->data);
$doc = unserialize($data);

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

declare(strict_types=1);

namespace VuFind\Record\Cache;

interface AdapterInterface
{
public function put ($sourceId, $recordId, $data);
public function get ($sourceId, $recordId);
}
17 changes: 17 additions & 0 deletions module/VuFind/src/VuFind/Record/Cache/BlackholeAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace VuFind\Record\Cache;

final class BlackholeAdapter implements AdapterInterface
{
public function put ($sourceId, $recordId, $data)
{
}

public function get ($sourceId, $recordId)
{
return false;
}
}
31 changes: 31 additions & 0 deletions module/VuFind/src/VuFind/Record/Cache/DatabaseAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace VuFind\Record\Cache;

use VuFind\Db\Table\Record;

final class DatabaseAdapter implements AdapterInterface
{
/** @var Record */
private $records;

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

public function put ($sourceId, $recordId, $data)
{
$this->records->updateRecord($recordId, $sourceId, $data);
}

public function get ($sourceId, $recordId)
{
if ($record = $this->records->findRecord($recordId, $sourceId)) {
return $record['data'];
}
return false;
}
}
31 changes: 31 additions & 0 deletions module/VuFind/src/VuFind/Record/Cache/MemcachedAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace VuFind\Record\Cache;

use Memcached;

final class MemcachedAdapter implements AdapterInterface
{
/** @var Memcached */
private $memcache;

public function __construct (Memcached $memcache)
{
$this->memcache = $memcache;
}

public function put ($sourceId, $recordId, $data)
{
$key = $sourceId . '|' . $recordId;
$this->memcache->set($key, $data);
}

public function get ($sourceId, $recordId)
{
$key = $sourceId . '|' . $recordId;
return $this->memcache->get($key);
}

}
9 changes: 7 additions & 2 deletions module/VuFind/src/VuFind/Record/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ public function __invoke(
if (!empty($options)) {
throw new \Exception('Unexpected options passed to factory.');
}

$memcache = new \Memcached();
$memcache->addServer('localhost', 11211);
return new $requestedName(
$container->get(\VuFind\RecordDriver\PluginManager::class),
$container->get(\VuFind\Config\PluginManager::class)->get('RecordCache'),
$container->get('VuFind\Record\Cache\Strategy')
new Cache\MemcachedAdapter($memcache)
// new Cache\BlackholeAdapter()
// new Cache\DatabaseAdapter(
// $container->get(\VuFind\Db\Table\PluginManager::class)->get('Record')
// )
);
}
}

0 comments on commit b43950c

Please sign in to comment.