Skip to content

Commit

Permalink
Add Github client cache decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Apr 21, 2024
1 parent a8e74a5 commit 8436c41
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 41 deletions.
22 changes: 14 additions & 8 deletions app/app/src/Application/Bootloader/GithubBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Application\Bootloader;

use App\Github\CacheableClient;
use App\Github\Client;
use App\Github\ClientInterface;
use App\Github\WebhookGate;
Expand All @@ -16,14 +17,19 @@ final class GithubBootloader extends Bootloader
public function defineSingletons(): array
{
return [
ClientInterface::class => static fn(CacheStorageProviderInterface $cache) => new Client(
client: new \GuzzleHttp\Client([
'base_uri' => 'https://api.github.com/',
'headers' => [
'Accept' => 'application/vnd.github.v3+json',
],
]),
cache: $cache,
ClientInterface::class => static fn(
CacheStorageProviderInterface $cache,
) => new CacheableClient(
client: new Client(
client: new \GuzzleHttp\Client([
'base_uri' => 'https://api.github.com/',
'headers' => [
'Accept' => 'application/vnd.github.v3+json',
],
]),
),
cache: $cache->storage('github'),
ttl: 300,
),

WebhookGate::class => static fn(
Expand Down
6 changes: 5 additions & 1 deletion app/app/src/Endpoint/Http/Controller/GithubWebhookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Endpoint\Http\Controller;

use App\Github\CacheableClient;
use App\Github\ClientInterface;
use App\Github\Event\NewRelease;
use App\Github\Event\RepositoryStarred;
Expand Down Expand Up @@ -40,10 +41,13 @@ public function __invoke(
};

if ($event !== null) {
$client->clearCache($input->data('repository.full_name'));
$this->events->dispatch($event);
}

if ($client instanceof CacheableClient) {
$client->clearCache();
}

return $response->create(200);
}

Expand Down
55 changes: 55 additions & 0 deletions app/app/src/Github/CacheableClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Github;

use Psr\SimpleCache\CacheInterface;

final readonly class CacheableClient implements ClientInterface
{
public function __construct(
private ClientInterface $client,
private CacheInterface $cache,
private int $ttl = 300,
) {
}

public function getStars(string $repository): int
{
$cacheKey = $this->getCacheKey($repository, __METHOD__);
if ($this->cache->has($cacheKey)) {
return $this->cache->get($cacheKey);
}

$stars = $this->client->getStars($repository);

$this->cache->set($cacheKey, $stars, $this->ttl);

return $stars;
}

public function getLastVersion(string $repository): string
{
$cacheKey = $this->getCacheKey($repository, __METHOD__);
if ($this->cache->has($cacheKey)) {
return $this->cache->get($cacheKey);
}

$version = $this->client->getLastVersion($repository);

$this->cache->set($cacheKey, $version, $this->ttl);

return $version;
}

public function clearCache(): void
{
$this->cache->clear();
}

private function getCacheKey(string $repository, string $prefix): string
{
return "{$prefix}:{$repository}";
}
}
31 changes: 1 addition & 30 deletions app/app/src/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,16 @@
namespace App\Github;

use GuzzleHttp\Psr7\Request;
use Psr\SimpleCache\CacheInterface;
use Spiral\Cache\CacheStorageProviderInterface;

final readonly class Client implements \App\Github\ClientInterface
final readonly class Client implements ClientInterface
{
private CacheInterface $cache;

public function __construct(
private \Psr\Http\Client\ClientInterface $client,
CacheStorageProviderInterface $cache,
private int $ttl = 300,
) {
$this->cache = $cache->storage('github');
}

public function getStars(string $repository): int
{
$cacheKey = $this->getCacheKey($repository, __METHOD__);
if ($this->cache->has($cacheKey)) {
return $this->cache->get($cacheKey);
}

$response = $this->client->sendRequest(
new Request(
'GET',
Expand All @@ -35,18 +23,12 @@ public function getStars(string $repository): int
);

$data = \json_decode($response->getBody()->getContents(), true);
$this->cache->set($cacheKey, $data['stargazers_count'], $this->ttl);

return $data['stargazers_count'];
}

public function getLastVersion(string $repository): string
{
$cacheKey = $this->getCacheKey($repository, __METHOD__);
if ($this->cache->has($cacheKey)) {
return $this->cache->get($cacheKey);
}

$response = $this->client->sendRequest(
new Request(
'GET',
Expand All @@ -55,18 +37,7 @@ public function getLastVersion(string $repository): string
);

$data = \json_decode($response->getBody()->getContents(), true);
$this->cache->set($cacheKey, $data['tag_name'], $this->ttl);

return $data['tag_name'];
}

private function getCacheKey(string $repository, string $prefix): string
{
return "{$prefix}:{$repository}";
}

public function clearCache(string $repository): void
{
$this->cache->clear();
}
}
2 changes: 0 additions & 2 deletions app/app/src/Github/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ interface ClientInterface
public function getStars(string $repository): int;

public function getLastVersion(string $repository): string;

public function clearCache(string $repository): void;
}

0 comments on commit 8436c41

Please sign in to comment.