diff --git a/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php b/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php index f0a08cf791cff3af3effcadb329c1b1c99eac678..460c3dcd6d8a71d6f80ccfa34051790121262061 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php +++ b/module/VuFind/src/VuFind/ILS/Driver/AbstractBase.php @@ -27,9 +27,6 @@ */ namespace VuFind\ILS\Driver; -use VuFind\Cache\KeyGeneratorTrait; -use Zend\Cache\Storage\StorageInterface; - /** * Default ILS driver base class. * @@ -43,22 +40,6 @@ use Zend\Cache\Storage\StorageInterface; */ abstract class AbstractBase implements DriverInterface { - use KeyGeneratorTrait; - - /** - * Cache for storing ILS data temporarily (e.g. patron blocks) - * - * @var StorageInterface - */ - protected $cache = null; - - /** - * Lifetime of cache (in seconds). - * - * @var int - */ - protected $cacheLifetime = 30; - /** * Driver configuration * @@ -66,18 +47,6 @@ abstract class AbstractBase implements DriverInterface */ protected $config = []; - /** - * Set a cache storage object. - * - * @param StorageInterface $cache Cache storage interface - * - * @return void - */ - public function setCacheStorage(StorageInterface $cache = null) - { - $this->cache = $cache; - } - /** * Set configuration. * @@ -92,72 +61,4 @@ abstract class AbstractBase implements DriverInterface { $this->config = $config; } - - /** - * Helper function for fetching cached data. - * Data is cached for up to $this->cacheLifetime seconds so that it would be - * faster to process e.g. requests where multiple calls to the backend are made. - * - * @param string $key Cache entry key - * - * @return mixed|null Cached entry or null if not cached or expired - */ - protected function getCachedData($key) - { - // No cache object, no cached results! - if (null === $this->cache) { - return null; - } - - $fullKey = $this->getCacheKey($key); - $item = $this->cache->getItem($fullKey); - if (null !== $item) { - // Return value if still valid: - if (time() - $item['time'] < $this->cacheLifetime) { - return $item['entry']; - } - // Clear expired item from cache: - $this->cache->removeItem($fullKey); - } - return null; - } - - /** - * Helper function for storing cached data. - * Data is cached for up to $this->cacheLifetime seconds so that it would be - * faster to process e.g. requests where multiple calls to the backend are made. - * - * @param string $key Cache entry key - * @param mixed $entry Entry to be cached - * - * @return void - */ - protected function putCachedData($key, $entry) - { - // Don't write to cache if we don't have a cache! - if (null === $this->cache) { - return; - } - $item = [ - 'time' => time(), - 'entry' => $entry - ]; - $this->cache->setItem($this->getCacheKey($key), $item); - } - - /** - * Helper function for removing cached data. - * - * @param string $key Cache entry key - * - * @return void - */ - protected function removeCachedData($key) - { - // Don't write to cache if we don't have a cache! - if (null === $this->cache) { - return; - } - $this->cache->removeItem($this->getCacheKey($key)); - } } diff --git a/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php b/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..8e297550522d351d7b5a32a9fbd754e4e180584c --- /dev/null +++ b/module/VuFind/src/VuFind/ILS/Driver/CacheTrait.php @@ -0,0 +1,141 @@ +<?php +/** + * Trait for ILS drivers using cache. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2007. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package ILS_Drivers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:plugins:ils_drivers Wiki + */ +namespace VuFind\ILS\Driver; + +use VuFind\Cache\KeyGeneratorTrait; +use Zend\Cache\Storage\StorageInterface; + +/** + * Default ILS driver base class. + * + * @category VuFind + * @package ILS_Drivers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:plugins:ils_drivers Wiki + * + * @SuppressWarnings(PHPMD.NumberOfChildren) + */ +trait CacheTrait +{ + use KeyGeneratorTrait; + + /** + * Cache for storing ILS data temporarily (e.g. patron blocks) + * + * @var StorageInterface + */ + protected $cache = null; + + /** + * Lifetime of cache (in seconds). + * + * @var int + */ + protected $cacheLifetime = 30; + + /** + * Set a cache storage object. + * + * @param StorageInterface $cache Cache storage interface + * + * @return void + */ + public function setCacheStorage(StorageInterface $cache = null) + { + $this->cache = $cache; + } + + /** + * Helper function for fetching cached data. + * Data is cached for up to $this->cacheLifetime seconds so that it would be + * faster to process e.g. requests where multiple calls to the backend are made. + * + * @param string $key Cache entry key + * + * @return mixed|null Cached entry or null if not cached or expired + */ + protected function getCachedData($key) + { + // No cache object, no cached results! + if (null === $this->cache) { + return null; + } + + $fullKey = $this->getCacheKey($key); + $item = $this->cache->getItem($fullKey); + if (null !== $item) { + // Return value if still valid: + if (time() - $item['time'] < $this->cacheLifetime) { + return $item['entry']; + } + // Clear expired item from cache: + $this->cache->removeItem($fullKey); + } + return null; + } + + /** + * Helper function for storing cached data. + * Data is cached for up to $this->cacheLifetime seconds so that it would be + * faster to process e.g. requests where multiple calls to the backend are made. + * + * @param string $key Cache entry key + * @param mixed $entry Entry to be cached + * + * @return void + */ + protected function putCachedData($key, $entry) + { + // Don't write to cache if we don't have a cache! + if (null === $this->cache) { + return; + } + $item = [ + 'time' => time(), + 'entry' => $entry + ]; + $this->cache->setItem($this->getCacheKey($key), $item); + } + + /** + * Helper function for removing cached data. + * + * @param string $key Cache entry key + * + * @return void + */ + protected function removeCachedData($key) + { + // Don't write to cache if we don't have a cache! + if (null === $this->cache) { + return; + } + $this->cache->removeItem($this->getCacheKey($key)); + } +} diff --git a/module/VuFind/src/VuFind/ILS/Driver/DAIA.php b/module/VuFind/src/VuFind/ILS/Driver/DAIA.php index f932d2306af75370a7d7c0c78fb1c965b30086d4..06b7d6ee10b9524fb6daf0e8cf694bd6d9015234 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/DAIA.php +++ b/module/VuFind/src/VuFind/ILS/Driver/DAIA.php @@ -51,6 +51,7 @@ use Zend\Log\LoggerAwareInterface as LoggerAwareInterface; class DAIA extends AbstractBase implements HttpServiceAwareInterface, LoggerAwareInterface { + use CacheTrait; use \VuFindHttp\HttpServiceAwareTrait; use \VuFind\Log\LoggerAwareTrait; diff --git a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php index c070a12b1be92827d1dac7d5b39525b568c69f4a..c6e44ca76b333021146b45b56e5b17b08e0865f0 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php +++ b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php @@ -49,6 +49,7 @@ use Zend\Log\LoggerInterface; class KohaILSDI extends \VuFind\ILS\Driver\AbstractBase implements \VuFindHttp\HttpServiceAwareInterface, \Zend\Log\LoggerAwareInterface { + use CacheTrait; use \VuFindHttp\HttpServiceAwareTrait; use \VuFind\Log\LoggerAwareTrait; diff --git a/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php b/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php index 051de58e90fd003db7fffbb4826dc7ea0fa65023..b7ff9cfb3f4e0c387c20f3ca806ddc1749fa042f 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php +++ b/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php @@ -45,6 +45,7 @@ use Zend\Log\LoggerAwareInterface; class SierraRest extends AbstractBase implements TranslatorAwareInterface, HttpServiceAwareInterface, LoggerAwareInterface { + use CacheTrait; use \VuFind\Log\LoggerAwareTrait { logError as error; } diff --git a/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php b/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php index e8e251619b42fe7a30149ebc2c58965b878b58a5..b14a77097e69a83ddb71f98c1af9ba9b2018a534 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php +++ b/module/VuFind/src/VuFind/ILS/Driver/VoyagerRestful.php @@ -50,6 +50,7 @@ use VuFind\Exception\ILS as ILSException; */ class VoyagerRestful extends Voyager implements \VuFindHttp\HttpServiceAwareInterface { + use CacheTrait; use \VuFindHttp\HttpServiceAwareTrait; /**