Skip to content

Commit

Permalink
Refactor memcached into CacheManager for Turnstile
Browse files Browse the repository at this point in the history
  • Loading branch information
maccabeelevine committed Dec 6, 2024
1 parent 11a777d commit 9f78b4c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
9 changes: 5 additions & 4 deletions config/vufind/RateLimiter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions module/VuFind/src/VuFind/Cache/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 2 additions & 12 deletions module/VuFind/src/VuFind/RateLimiter/RateLimiterManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 9f78b4c

Please sign in to comment.