Skip to content

Commit

Permalink
Implement adapter based caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj committed Jul 15, 2024
1 parent 6efce77 commit 5e8b6c9
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 20 deletions.
32 changes: 13 additions & 19 deletions module/VuFind/src/VuFind/Record/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 @@ -58,12 +57,8 @@ class Cache implements \Laminas\Log\LoggerAwareInterface
*/
protected $cacheConfig;

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

/**
* Record driver plugin manager
Expand All @@ -84,15 +79,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,
Record $recordTable
Cache\AdapterInterface $adapter
) {
$this->cacheConfig = $config;
$this->recordTable = $recordTable;
$this->adapter = $adapter;
$this->recordFactoryManager = $recordFactoryManager;

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

Expand All @@ -126,13 +120,13 @@ public function createOrUpdate($recordId, $source, $rawData)
public function lookup($id, $source)
{
$this->debug("Checking {$source}|{$id}");
$record = $this->recordTable->findRecord($id, $source);
$record = $this->adapter->get($source, $id);
$this->debug(
"Cached record {$source}|{$id} "
. ($record !== false ? 'found' : 'not found')
);
try {
return $record !== false ? [$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 @@ -159,10 +153,11 @@ public function lookupBatch($ids, $source)

$this->debug("Checking $source batch: " . implode(', ', $ids));
$vufindRecords = [];
$cachedRecords = $this->recordTable->findRecords($ids, $source);
foreach ($cachedRecords as $cachedRecord) {
foreach ($ids as $id) {
$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 @@ -270,10 +265,9 @@ public function isCachable($source)
*
* @return \VuFind\RecordDriver\AbstractBase
*/
protected function getVuFindRecord($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);
}

}
8 changes: 7 additions & 1 deletion module/VuFind/src/VuFind/Record/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,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\Db\Table\PluginManager::class)->get('Record')
new Cache\MemcachedAdapter($memcache)
// new Cache\BlackholeAdapter()
// new Cache\DatabaseAdapter(
// $container->get(\VuFind\Db\Table\PluginManager::class)->get('Record')
// )
);
}
}

0 comments on commit 5e8b6c9

Please sign in to comment.