From 9f78b4ce83fcdc03a3f2feeb4b0de47fbe9372fa Mon Sep 17 00:00:00 2001 From: Maccabee Levine Date: Fri, 6 Dec 2024 03:03:14 +0000 Subject: [PATCH] Refactor memcached into CacheManager for Turnstile --- config/vufind/RateLimiter.yaml | 9 +++++---- module/VuFind/src/VuFind/Cache/Manager.php | 20 +++++++++++++++++++ .../RateLimiter/RateLimiterManagerFactory.php | 14 ++----------- .../Turnstile/TurnstileFactory.php | 15 +++++++++----- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/config/vufind/RateLimiter.yaml b/config/vufind/RateLimiter.yaml index e817543df4c..a9c515fca2a 100644 --- a/config/vufind/RateLimiter.yaml +++ b/config/vufind/RateLimiter.yaml @@ -40,11 +40,12 @@ Storage: #redis_version : 6 #redis_standalone : true - # Namespace for Turnstile result cache (default is Turnstile) - # turnstileNamespace: Turnstile + turnstileOptions: + # Namespace for Turnstile result cache (default is Turnstile) + # namespace: Turnstile - # Time-to-live (seconds) for Turnstile result cache (default is 86400, i.e. 1 day) - # turnstileTtl: 86400 + # Time-to-live (seconds) for Turnstile result cache (default is 86400, i.e. 1 day) + # ttl: 86400 # Policies define the actual rate limiting settings. The request is checked against # the list of policies, and the first matching policy is used. diff --git a/module/VuFind/src/VuFind/Cache/Manager.php b/module/VuFind/src/VuFind/Cache/Manager.php index 7f224bfad88..d308749ddda 100644 --- a/module/VuFind/src/VuFind/Cache/Manager.php +++ b/module/VuFind/src/VuFind/Cache/Manager.php @@ -410,4 +410,24 @@ protected function createFileCache($cacheName, $dirName, $overrideOpts = []) ], ]; } + + // other than Redis + /** + * Create an in-memory cache + * + * @param array $storageConfig See Storage in RateLimiter.yaml + * + * @return StorageInterface + */ + public function createInMemoryCache(array $storageConfig): StorageInterface + { + $adapter = $storageConfig['adapter'] ?? 'memcached'; + $options = $storageConfig['options']; + if ('memcached' === strtolower($adapter)) { + $options['servers'] ??= 'localhost:11211'; + } + $settings = compact('adapter', 'options'); + $laminasCache = $this->factory->createFromArrayConfiguration($settings); + return $laminasCache; + } } diff --git a/module/VuFind/src/VuFind/RateLimiter/RateLimiterManagerFactory.php b/module/VuFind/src/VuFind/RateLimiter/RateLimiterManagerFactory.php index 43b7a022d39..cadffe4379b 100644 --- a/module/VuFind/src/VuFind/RateLimiter/RateLimiterManagerFactory.php +++ b/module/VuFind/src/VuFind/RateLimiter/RateLimiterManagerFactory.php @@ -150,9 +150,9 @@ protected function createCache(array $config, string $namespaceSuffix): StorageI return $this->createRedisCache($storageConfig); } + $cacheManager = $this->getService(\VuFind\Cache\Manager::class); if ('vufind' === $adapterLc) { // Use cache manager for "VuFind" cache (only for testing purposes): - $cacheManager = $this->getService(\VuFind\Cache\Manager::class); $laminasCache = $cacheManager->getCache('object', $storageConfig['options']['namespace']); // Fake the capabilities to include static TTL support: $eventManager = $laminasCache->getEventManager(); @@ -170,17 +170,7 @@ function ($event) use ($laminasCache) { } ); } else { - if ('memcached' === $adapterLc) { - $storageConfig['options']['servers'] ??= 'localhost:11211'; - } - - // Laminas cache: - $settings = [ - 'adapter' => $adapter, - 'options' => $storageConfig['options'], - ]; - $laminasCache = $this->getService(\Laminas\Cache\Service\StorageAdapterFactory::class) - ->createFromArrayConfiguration($settings); + $laminasCache = $cacheManager->createInMemoryCache($storageConfig); } return new CacheStorage(new CacheItemPoolDecorator($laminasCache)); diff --git a/module/VuFind/src/VuFind/RateLimiter/Turnstile/TurnstileFactory.php b/module/VuFind/src/VuFind/RateLimiter/Turnstile/TurnstileFactory.php index af30d5c5a7c..af00084ac2a 100644 --- a/module/VuFind/src/VuFind/RateLimiter/Turnstile/TurnstileFactory.php +++ b/module/VuFind/src/VuFind/RateLimiter/Turnstile/TurnstileFactory.php @@ -29,6 +29,7 @@ namespace VuFind\RateLimiter\Turnstile; +use Laminas\Cache\Storage\StorageInterface; use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Laminas\ServiceManager\Exception\ServiceNotFoundException; use Laminas\ServiceManager\Factory\FactoryInterface; @@ -90,13 +91,17 @@ public function __invoke( * * @return ?StorageInterface */ - protected function createTurnstileCache(array $config): \Laminas\Cache\Storage\StorageInterface + protected function createTurnstileCache(array $config): StorageInterface { - $storageOptions = $config['Storage']['options'] ?? []; - $storageOptions['namespace'] = $storageOptions['turnstileNamespace'] ?? 'Turnstile'; + $storageConfig = $config['Storage']; + $storageConfig['options']['namespace'] = $storageConfig['turnstileOptions']['namespace'] ?? 'Turnstile'; + $storageConfig['options']['ttl'] = $storageConfig['turnstileOptions']['ttl'] ?? 60 * 60 * 24; $cacheManager = $this->getService(\VuFind\Cache\Manager::class); - $cache = $cacheManager->getCache('object', $storageOptions['namespace']); - $cache->getOptions()->setTtl($storageOptions['turnstileTtl'] ?? 60 * 60 * 24); + + // FOR NOW -- must change to allow redis also + $storageConfig['adapter'] = 'memcached'; + + $cache = $cacheManager->createInMemoryCache($storageConfig); return $cache; } }