made ttl configurable

This commit is contained in:
2024-09-13 03:13:06 +02:00
parent a859a4a3ec
commit 30b61b5064
4 changed files with 33 additions and 23 deletions

View File

@@ -16,7 +16,8 @@ final class RedmineClient
public function __construct( public function __construct(
private TagAwareCacheInterface $cache, private TagAwareCacheInterface $cache,
private string $url, private string $url,
private string $apiKey private string $apiKey,
private int $ttl
) {} ) {}
public function call(string $api, string $method, array $arguments = []): mixed public function call(string $api, string $method, array $arguments = []): mixed
@@ -24,7 +25,7 @@ final class RedmineClient
$cacheKey = sprintf('%s_%s_%s', $api, $method, sha1(serialize($arguments))); $cacheKey = sprintf('%s_%s_%s', $api, $method, sha1(serialize($arguments)));
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($api, $method, $arguments): array { return $this->cache->get($cacheKey, function (ItemInterface $item) use ($api, $method, $arguments): array {
$item->expiresAfter(3600); $item->expiresAfter($this->ttl);
$item->tag([$api, $method]); $item->tag([$api, $method]);
$client = $this->getClient()->getApi($api); $client = $this->getClient()->getApi($api);

View File

@@ -14,26 +14,33 @@ final class Configuration implements ConfigurationInterface
{ {
$treeBuilder = new TreeBuilder('magdev_redmine'); $treeBuilder = new TreeBuilder('magdev_redmine');
$rootNode = $treeBuilder->getRootNode(); $rootNode = $treeBuilder->getRootNode();
$rootNode->fixXmlConfig('connection') $rootNode->children()
->children() ->scalarNode('ttl')
->arrayNode('connections')
->useAttributeAsKey('name')
->normalizeKeys(false)
->arrayPrototype()
->children()
->scalarNode('url')
->isRequired() ->isRequired()
->info('Base URL of the Redmine instance') ->info('TTL for cached api calls')
->end() ->end()
->scalarNode('apikey') ->end();
->isRequired()
->info('Redmine API-Key') #$rootNode->fixXmlConfig('connection')
->end() # ->children()
->end() # ->arrayNode('connections')
->end() # ->useAttributeAsKey('name')
->end() # ->normalizeKeys(false)
->scalarNode('default_connection')->end() # ->arrayPrototype()
; # ->children()
# ->scalarNode('url')
# ->isRequired()
# ->info('Base URL of the Redmine instance')
# ->end()
# ->scalarNode('apikey')
# ->isRequired()
# ->info('Redmine API-Key')
# ->end()
# ->end()
# ->end()
# ->end()
# ->scalarNode('default_connection')->end()
#;
return $treeBuilder; return $treeBuilder;
} }

View File

@@ -17,6 +17,8 @@ final class MagdevRedmineExtension extends Extension
{ {
$config = $this->processConfiguration(new Configuration(), $configs); $config = $this->processConfiguration(new Configuration(), $configs);
$container->setParameter('magdev_redmine.ttl', $config['ttl']);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml'); $loader->load('services.yaml');
} }

View File

@@ -1,3 +1,3 @@
services: services:
Magdev\RedmineBundle\Client\RedmineClient: Magdev\RedmineBundle\Client\RedmineClient:
arguments: ['@redmine_api.cache', '%env(REDMINE_URL)%', '%env(REDMINE_APIKEY)%'] arguments: ['@redmine_api.cache', '%env(REDMINE_URL)%', '%env(REDMINE_APIKEY)%', '%magdev_redmine.ttl%']