switch to enable/disable cache, added dedicated RedmineClientInterface

This commit is contained in:
2024-09-13 04:40:28 +02:00
parent e9c4f4bca6
commit 3ff3ddfdc5
5 changed files with 34 additions and 32 deletions

View File

@@ -1,5 +1,10 @@
services: services:
Magdev\RedmineBundle\Client\RedmineClient: Magdev\RedmineBundle\Client\RedmineClient:
arguments: ['@redmine_api.cache', '%magdev_redmine.url%', '%magdev_redmine.apiKey%', '%magdev_redmine.cache.ttl%'] arguments:
- '@redmine_api.cache'
- '%magdev_redmine.url%'
- '%magdev_redmine.apiKey%'
- '%magdev_redmine.cache.ttl%'
- '%magdev_redmine.cache.enabled%'
redmine.api: '@Magdev\RedmineBundle\Client\RedmineClient' redmine.api: '@Magdev\RedmineBundle\Client\RedmineClient'

View File

@@ -6,14 +6,13 @@ use Redmine\Client\Psr18Client;
use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory; use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory;
use Redmine\Api; use Redmine\Api;
use Redmine\Client\Client;
use Redmine\Http\HttpClient; use Redmine\Http\HttpClient;
use Redmine\Http\Request; use Redmine\Http\Request;
use Redmine\Http\Response; use Redmine\Http\Response;
use Symfony\Contracts\Cache\ItemInterface; use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface; use Symfony\Contracts\Cache\TagAwareCacheInterface;
final class RedmineClient implements Client, HttpClient final class RedmineClient implements RedmineClientInterface, HttpClient
{ {
private ?Psr18Client $client = null; private ?Psr18Client $client = null;
@@ -21,11 +20,21 @@ final class RedmineClient implements Client, HttpClient
private TagAwareCacheInterface $cache, private TagAwareCacheInterface $cache,
private string $url, private string $url,
private string $apiKey, private string $apiKey,
private int $ttl private int $ttl,
private bool $cacheEnabled
) {} ) {}
public function call(string $api, string $method, array $arguments = []): mixed public function call(string $api, string $method, array $arguments = []): mixed
{ {
if (!$this->cacheEnabled) {
$client = $this->getClient()->getApi($api);
return \call_user_func_array(
[$client, $method],
$arguments
);
}
$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 {
@@ -33,7 +42,10 @@ final class RedmineClient implements Client, HttpClient
$item->tag([$api, $method]); $item->tag([$api, $method]);
$client = $this->getClient()->getApi($api); $client = $this->getClient()->getApi($api);
$value = \call_user_func_array([$client, $method], $arguments); $value = \call_user_func_array(
[$client, $method],
$arguments
);
return $value; return $value;
}); });

View File

@@ -0,0 +1,11 @@
<?php
namespace Magdev\RedmineBundle\Client;
use Redmine\Client\Client;
interface RedmineClientInterface extends Client
{
public function call(string $api, string $method, array $arguments = []): mixed;
public function getClient(): ?Psr18Client;
}

View File

@@ -17,6 +17,7 @@ final class MagdevRedmineExtension extends Extension
{ {
$config = $this->processConfiguration(new Configuration(), $configs); $config = $this->processConfiguration(new Configuration(), $configs);
$container->setParameter('magdev_redmine.cache.enabled', $config['cache']['enabled']);
$container->setParameter('magdev_redmine.cache.ttl', $config['cache']['ttl']); $container->setParameter('magdev_redmine.cache.ttl', $config['cache']['ttl']);
$container->setParameter('magdev_redmine.url', $config['url']); $container->setParameter('magdev_redmine.url', $config['url']);
$container->setParameter('magdev_redmine.apiKey', $config['apiKey']); $container->setParameter('magdev_redmine.apiKey', $config['apiKey']);

View File

@@ -1,27 +0,0 @@
<?php
namespace Magdev\RedmineBundle\Enum;
enum ApiName
{
case attachment;
case group;
case custom_fields;
case issue;
case issue_category;
case issue_priority;
case issue_relation;
case issue_status;
case membership;
case news;
case project;
case query;
case role;
case time_entry;
case time_entry_activity;
case tracker;
case user;
case version;
case wiki;
case search;
}