Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Oct 14, 2024
1 parent ef23fea commit d238d1b
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions module/VuFind/src/VuFind/ILS/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1293,24 +1293,18 @@ public function callIlsWithFailover($methodName, $params)
/**
* Get data for an ILS method from shared or session cache
*
* @param string $methodName The name of the called method.
* @param array $params Array of passed parameters.
* @param array $cacheSettings Cache settings
*
* @return ?array
*/
protected function getCachedData($methodName, $params)
protected function getCachedData(array $cacheSettings): ?array
{
$cacheLifeTime = $this->cacheLifeTime[$methodName] ?? null;
$cacheStorage = $this->cacheStorage[$methodName] ?? null;
if (!$cacheLifeTime || !$cacheStorage) {
return null;
}
$cacheKey = $methodName . md5(serialize($params));
if ('shared' === $cacheStorage) {
$cacheKey = $cacheSettings['key'];
if ('shared' === $cacheSettings['storage']) {
return $this->getSharedCachedData($cacheKey);
}
if ($this->sessionCache && ($entry = $this->sessionCache[$cacheKey] ?? null)) {
if (time() - $entry['ts'] <= $cacheLifeTime) {
if (time() - $entry['ts'] <= $cacheSettings['lifeTime']) {
return $entry['payload'];
}
unset($this->sessionCache[$cacheKey]);
Expand All @@ -1321,21 +1315,15 @@ protected function getCachedData($methodName, $params)
/**
* Put data for an ILS method to shared or session cache.
*
* @param string $methodName The name of the called method.
* @param array $params Array of passed parameters.
* @param mixed $data Data to cache
* @param array $cacheSettings Cache settings
* @param array $data Data to cache
*
* @return void
*/
protected function putCachedData($methodName, $params, $data): void
protected function putCachedData(array $cacheSettings, array $data): void
{
$cacheLifeTime = $this->cacheLifeTime[$methodName] ?? null;
$cacheStorage = $this->cacheStorage[$methodName] ?? null;
if (!$cacheLifeTime || !$cacheStorage) {
return;
}
$cacheKey = $methodName . md5(serialize($params));
if ('shared' === $cacheStorage) {
$cacheKey = $cacheSettings['key'];
if ('shared' === $cacheSettings['storage']) {
$this->putSharedCachedData($cacheKey, $data);
return;
}
Expand All @@ -1347,6 +1335,25 @@ protected function putCachedData($methodName, $params, $data): void
}
}

/**
* Get cache settings for a method
*
* @param string $methodName The name of the called method.
* @param array $params Array of passed parameters.
*
* @return ?array
*/
protected function getCacheSettings($methodName, $params): ?array
{
$lifeTime = $this->cacheLifeTime[$methodName] ?? null;
$storage = $this->cacheStorage[$methodName] ?? null;
if (!$lifeTime || !$storage) {
return null;
}
$key = $methodName . md5(serialize($params));
return compact('lifeTime', 'storage', 'key');
}

/**
* Default method -- pass along calls to the driver if available; return
* false otherwise. This allows custom functions to be implemented in
Expand All @@ -1363,13 +1370,16 @@ protected function putCachedData($methodName, $params, $data): void
*/
public function __call($methodName, $params)
{
$cacheSettings = $this->getCacheSettings($methodName, $params);
// Note: The actual data is cached in an array so that we can differentiate
// between a missing cache entry and null as a valid value.
if ($cached = $this->getCachedData($methodName, $params)) {
if ($cacheSettings && ($cached = $this->getCachedData($cacheSettings))) {
return $cached['data'];
}
$data = $this->callIlsWithFailover($methodName, $params);
$this->putCachedData($methodName, $params, compact('data'));
if ($cacheSettings) {
$this->putCachedData($cacheSettings, compact('data'));
}
return $data;
}
}

0 comments on commit d238d1b

Please sign in to comment.